代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
winfrom 播放音乐
作者:
Dezai.CN
/ 发布于
2013/1/4
/
735
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Windows.Forms; namespace QiDian { public class mp3 { //定义API函数使用的字符串变量 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] private string Name = ""; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] private string durLength = ""; [MarshalAs(UnmanagedType.LPTStr, SizeConst = 128)] private string TemStr = ""; int ilong; //定义播放状态枚举变量 public enum State { mPlaying = 1, mPuase = 2, mStop = 3 }; //结构变量 public struct structMCI { //public bool bMut; //public int iDur; //public int iVol; //public int iBal; public string iName; public State state; public int iPos; }; public structMCI mc = new structMCI(); //取得播放文件属性 public string FileName { get { return mc.iName; } set { //ASCIIEncoding asc = new ASCIIEncoding(); try { TemStr = ""; TemStr = TemStr.PadLeft(127, Convert.ToChar(" ")); Name = Name.PadLeft(260, Convert.ToChar(" ")); mc.iName = value; ilong = APIClass.GetShortPathName(mc.iName, Name, Name.Length); Name = GetCurrPath(Name); //Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media"; Name = "open " + Convert.ToChar(34) + Name + Convert.ToChar(34) + " alias media"; ilong = APIClass.mciSendString("close all", TemStr, TemStr.Length, 0); ilong = APIClass.mciSendString(Name, TemStr, TemStr.Length, 0); ilong = APIClass.mciSendString("set media time format milliseconds", TemStr, TemStr.Length, 0); mc.state = State.mStop; } catch { } } } //播放 public void play() { TemStr = ""; TemStr = TemStr.PadLeft(127, Convert.ToChar(" ")); APIClass.mciSendString("play media", TemStr, TemStr.Length, 0); mc.state = State.mPlaying; } //停止 public void StopT() { TemStr = ""; TemStr = TemStr.PadLeft(128, Convert.ToChar(" ")); ilong = APIClass.mciSendString("close media", TemStr, 128, 0); ilong = APIClass.mciSendString("close all", TemStr, 128, 0); mc.state = State.mStop; } public void Puase() { TemStr = ""; TemStr = TemStr.PadLeft(128, Convert.ToChar(" ")); ilong = APIClass.mciSendString("pause media", TemStr, TemStr.Length, 0); mc.state = State.mPuase; } private string GetCurrPath(string name) { if (name.Length < 1) return ""; name = name.Trim(); name = name.Substring(0, name.Length - 1); return name; } //总时间 public int Duration { get { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); APIClass.mciSendString("status media length", durLength, durLength.Length, 0); durLength = durLength.Trim(); if (durLength == "") return 0; return (int)(Convert.ToDouble(durLength) / 1000f); } } //当前时间 public int CurrentPosition { get { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); APIClass.mciSendString("status media position", durLength, durLength.Length, 0); durLength = durLength.Trim(); mc.iPos = (int)(Convert.ToDouble(durLength) / 1000f); return mc.iPos; } } public bool IsEnd() { durLength = ""; durLength = durLength.PadLeft(128, Convert.ToChar(" ")); APIClass.mciSendString("status media length", durLength, durLength.Length, 0); durLength = durLength.Trim(); string curLength; curLength = ""; curLength = curLength.PadLeft(128, Convert.ToChar(" ")); APIClass.mciSendString("status media position", curLength, curLength.Length, 0); curLength = curLength.Trim(); if (curLength == durLength) { return true; } return false; } } public class APIClass { [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName( string lpszLongPath, string shortFile, int cchBuffer ); [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)] public static extern int mciSendString( string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback ); } 调用: mp3 mp3 = new mp3(); mp3.FileName = System.Windows.Forms.Application.StartupPath + "\\1.mp3"; mp3.play(); 第二种: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace QiDian { public class mp2 { #region - 属性 - [DllImport("winmm.dll")] static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback); /// <summary> /// 临时音乐文件存放处 /// </summary> private string m_musicPath = ""; /// <summary> /// 父窗体句柄 /// </summary> private IntPtr m_Handle; #endregion #region - 构造函数 - /// <summary> /// 创建Mp3播放类 /// </summary> /// <param name="music">嵌入的音乐文件</param> /// <param name="path">临时音乐文件保存路径</param> /// <param name="Handle">父窗体句柄</param> public mp2(Byte[] music, string path, IntPtr Handle) { try { CloseMedia(); m_Handle = Handle; m_musicPath = Path.Combine(path, "temp.mp3"); //using (FileStream fs = File.Create(m_musicPath)) using (FileStream fs = new FileStream(m_musicPath, FileMode.Create)) { fs.Write(music, 0, music.Length); fs.Flush(); fs.Close(); fs.Dispose(); } } catch (Exception) { } } /// <summary> /// 创建Mp3播放类 /// </summary> /// <param name="musicPath">要播放的mp3文件路径</param> /// <param name="Handle">父窗体句柄</param> public mp2(string musicPath, IntPtr Handle) { m_musicPath = musicPath; m_Handle = Handle; } public mp2(Byte[] music, IntPtr Handle) : this(music, @"C:\Windows\", Handle) { } #endregion #region - 播放音乐 - public void Open(string path) { if (path != "") { try { mciSendString("open " + path + " alias media", null, 0, m_Handle); mciSendString("play media", null, 0, m_Handle); } catch (Exception) { } } } public void Open() { Open(m_musicPath); } #endregion #region - 停止音乐播放 - public void CloseMedia() { try { mciSendString("close all", null, 0, m_Handle); } catch (Exception) { } } #endregion } } 调用: //加载音乐 mp2 mp3 = new mp2(Properties.Resources._1, Handle); //音乐开始播放 mp3.Open();
试试其它关键字
播放音乐
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3