利用WMPLib在Windows mobile下播放Mp3
程序示例(C#智能设备应用程序):
必须添加对wmp.dll的引用(项目->添加引用->windows/system32/wmp.dll)。
源文件:
//CMediaControl.cs:定义了控制媒体播放的类CMediaControl。 using System.Collections.Generic; using System; using System.Windows.Forms; public class CMediaControl { private WMPLib.WindowsMediaPlayer player; private String curFileName; //Current media file name. private bool isPaused; //Is current state pause? private double curPos; //Current time position, use to restore play from pause status. public CMediaControl() { player = new WMPLib.WindowsMediaPlayer(); player.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange); player.MediaError += new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError); curFileName = "" ; isPaused = false ; curPos = 0.0; } private void Player_PlayStateChange( int NewState) { if (WMPLib.WMPPlayState.wmppsPaused == (WMPLib.WMPPlayState)NewState) { isPaused = true ; } else if (WMPLib.WMPPlayState.wmppsPlaying == (WMPLib.WMPPlayState)NewState) { if (isPaused) { isPaused = false ; player.controls.currentPosition += curPos; curPos = 0; } } //else if(WMPLib.WMPPlayState.wmppsStopped == (WMPLib.WMPPlayState)NewState) //{ // isPaused = false; //} } private void Player_MediaError( object pMediaObject) { MessageBox.Show( "Cannot play media file." ); } public int OpenMediaFile(String fileName) { int ret = 0; //Validate fileName //Filename legal curFileName = fileName; player.URL = curFileName; return ret; } public int SetVolumn( int vol) { int ret = 0; //Validate vol if ((vol < 0) || (vol >100)) { //Volumn is highest or lowest. return ret; } //vol legal player.settings.volume = vol; return ret; } public int GetVolumn() { int ret = 0; ret = player.settings.volume; return ret; } public int MediaPlay() { int ret = 0; player.controls.play(); return ret; } public int MediaPause() { int ret = 0; player.controls.pause(); curPos = player.controls.currentPosition; return ret; } public int MediaStop() { int ret = 0; player.controls.stop(); return ret; } public double MediaGetPosition() { double ret = 0; if (WMPLib.WMPPlayState.wmppsPlaying != player.playState) { return ret; } double curPos = player.controls.currentPosition; double totalLen = player.currentMedia.duration; ret = (curPos / totalLen) * 100; return ret; } public string MediaGetInfo() { string mediaInfo = "" ; //mediaInfo += player.currentMedia.name; //mediaInfo += "@"; mediaInfo += "Title: " + player.currentMedia.getItemInfo( "Title" ); mediaInfo += "@" ; mediaInfo += "Author: " + player.currentMedia.getItemInfo( "Author" ); mediaInfo += "@" ; mediaInfo += "Description: " + player.currentMedia.getItemInfo( "Description" ); mediaInfo += "@" ; mediaInfo += "Duration: " + player.currentMedia.getItemInfo( "Duration" ); mediaInfo += "@" ; mediaInfo += "FileType: " + player.currentMedia.getItemInfo( "FileType" ); mediaInfo += "@" ; mediaInfo += "FileSize: " + player.currentMedia.getItemInfo( "FileSize" ); return mediaInfo; } public int MediaFastForward( int tick) { int ret = 0; //player.controls.fastForward(); player.controls.currentPosition += tick; //MessageBox.Show("Don't support this function!"); return ret; } public int MediaFastReverse( int tick) { int ret = 0; //player.controls.fastReverse(); player.controls.currentPosition -= tick; //MessageBox.Show("Don't support this function!"); return ret; } } |
附录
WMPLib的基本属性及方法
URL: (String); 指定媒体位置,本机或网络地址
uiMode:(String); 播放器界面模式,可为Full, Mini, None, Invisible
playState:(integer); 播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪
enableContextMenu:(Boolean); 启用/禁用右键菜单
fullScreen:boolean; (是否全屏显示)
[controls] wmp.controls //播放器基本控制
controls.play; 播放
controls.pause; 暂停
controls.stop; 停止
controls.currentPosition:double; 当前进度
controls.currentPositionString:string; 当前进度,字符串格式。如“00:23”
controls.fastForward; 快进
controls.fastReverse; 快退
controls.next; 下一曲
controls.previous; 上一曲
[settings] wmp.settings //播放器基本设置
settings.volume:integer; 音量,0-100
settings.autoStart:Boolean; 是否自动播放
settings.mute:Boolean; 是否静音
settings.playCount:integer; 播放次数
[currentMedia] wmp.currentMedia //当前媒体属性
currentMedia.duration:double; 媒体总长度
currentMedia.durationString:string; 媒体总长度,字符串格式。如“03:24”
currentMedia.getItemInfo(const string); 获取当前媒体信息"Title"=媒体标题,"Author"=艺术 家,"Copyright"=版权信息,"Description"=媒体内容描述,"Duration"=持续时间(秒),"FileSize"=文件大小,"FileType"=文件类型,"sourceURL"=原始地址
currentMedia.setItemInfo(const string); 通过属性名设置媒体信息
currentMedia.name:string; 同 currentMedia.getItemInfo("Title")
[currentPlaylist] wmp.currentPlaylist //当前播放列表属性
currentPlaylist.count:integer; 当前播放列表所包含媒体数
currentPlaylist.Item[integer]; 获取或设置指定项目媒体信息,其子属性同wmp.currentMedia
Q Q: 121866673
来源: http://zxlovenet.cnblogs.com
声明: 本文原创发表于博客园,作者为初行本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
消息:此博客已停止更新,想了解最新博客更新请关注我的新博客 Noogel's notes
推荐: 推荐使用为知笔记(WizNote),它是电脑、手机、平板上都能用的云笔记软件,还可以分类管理和共享资料,使用我的邀请码注册:https://note.wiz.cn/i/06102d9e
打赏: 如果您觉得文章对您的工作有帮助,请小额打赏我一些,鼓励我写出更好的文章!

微信打赏

支付宝打赏
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述