Csharp/C#教程:C#定义的MP3播放类实例分享

本文实例讲述了C#定义的MP3播放类。分享给大家供大家参考。具体分析如下:

这里使用C#定义一个MP3播放类,将Mp3文件作为资源文件包含到项目中,就可以播放mp3了

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.IO; usingSystem.Runtime.InteropServices; namespaceMp3Tool { publicClassMP3Player { [DllImport("winmm.dll")] staticexternInt32mciSendString(Stringcommand,StringBuilderbuffer,Int32bufferSize,IntPtrhwndCallback); ///<summary> ///temporaryrepositoryofmusicfiles ///</summary> privateStringm_musicPath=""; ///<summary> ///parentwindowhandle ///</summary> privateIntPtrm_Handle; ///<summary> ///CreateMp3playerclass ///</summary> ///<PARAMname="music">embeddedmusicfile</param> ///<PARAMname="path">temporarymusicfilepath</param> ///<PARAMname="Handle">parentwindowhandle</param> publicMP3Player(Byte[]Music,Stringpath,IntPtrHandle) { try { m_Handle=Handle; m_musicPath=Path.Combine(path,"temp.mp3"); FileStreamfs=newFileStream(m_musicPath,FileMode.Create); fs.Write(Music,0,music.Length); fs.Close(); } catch(Exception) { } } ///<summary> ///CreateMp3playerclass ///</summary> ///<PARAMname="musicPath">toplaythemp3filepath</param> ///<PARAMname="Handle">parentwindowhandle</param> publicMP3Player(StringmusicPath,IntPtrHandle) { m_musicPath=musicPath; m_Handle=Handle; } publicMP3Player(Byte[]Music,IntPtrHandle):this(Music,@"C:Windows",Handle) { } publicvoidOpen(Stringpath) { IF(path!="") { try { mciSendString("Open"+path+"aliasMedia",null,0,m_Handle); mciSendString("playMedia",null,0,m_Handle); } catch(Exception) { } } } publicvoidOpen() { Open(m_musicPath); } voidCloseMedia() { try { mciSendString("CloseALL",null,0,m_Handle); } catch(Exception) { } } } }

调用方法:

privatevoidMain() {   //loadmusic   MP3PlayerMP3=newMP3Player(Properties.Resources.music,Handle);   //musicstartplaying   mp3.Open(); }

希望本文所述对大家的C#程序设计有所帮助。

您可能感兴趣的文章:C#实现简单播放mp3的方法C#基于OLEDB获取Excel文件表结构信息的方法C#编程实现动态改变配置文件信息的方法C#获取文件相关信息的方法C#获取指定文件著作权信息的方法C#获取程序文件相关信息的方法C#获得文件属性信息的实现方法通过C#动态生成图书信息XML文件C#使用shell32获取文件属性的方法C#实现获取mp3Tag信息的方法

浅谈C++ 基类指针和子类指针的相互赋值

C#实现单件模式的三种常用方法

上述就是C#学习教程:C#定义的MP3播放类实例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906166.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐