您的位置:首页 > 其它

windows mobile中利用WMPLib播放MP3文件(转载)

2009-07-23 11:34 645 查看
程序示例(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;

}

}



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ZOLoveGD/archive/2009/04/28/4133567.aspx

WMPLib的基本属性及方法见附录,完整的工程下载地址为:





附录

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



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ZOLoveGD/archive/2009/04/28/4133567.aspx





你好。朋友我现在在做跟你相似的东西。我现在在窗体上加了一个列表框,同时添加了MediaPlayer控件,我想做的是让视频播放时全屏,播放完毕后把列表框重新显示出来。因此我想在播放完成后触发player.PlayStateChange 事件,但是每次播放完视频后这个事件都不触发。能不能帮忙分析一下是什么原因。多谢。你好,针对你说的问题我有如下想法,觉得有用的话可以试试:1)看看是不是没有注册player.PlayStateChange事件player.PlayStateChange = new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);2)如果已经注册的话,在player.PlayStateChange关联的函数中设断点看看会不会进入相应的函数 private void Player_PlayStateChange(int NewState){}








我的附加:

我们可以用最简单的方式来使用:



private WMPLib.WindowsMediaPlayer WMP;

WMP = new WMPLib.WindowsMediaPlayer();
        // internal playlist holder
        
        // initial player settings
        WMP.uiMode = "invisible";
        WMP.enableContextMenu = false;
        WMP.settings.autoStart = false;
        // windows media player volume control affects the media player
        // only. we prefer to control volume via the wave out master volume
        // we set the player internal volume to max.
        WMP.settings.volume = 100;
        // create a dummy playlist - this will be managed and manipulated
        // when user loads/plays playlists
       
        // add internal event handlers
        WMP.URL = @"/My Documents/music/qiyueqiriqing.mp3";
           WMP.controls.play();








我的补充::



还有简单的方式可以使用WMP控件,这就是增加播放器的com控件



在.NET的winform里面,没有托管的音乐播放器,API只能播放W***格式,对于MP3等形式的音频文件,就要依赖于 MediaPlayer里,嘿嘿

使用的方法:

在toolbox上点右键,选择“选择项目(Choose Items)”,切到COM页,找到 Windows Media Player, 勾选,确定

在toolbox上,把刚才加入的MediaPlayer控件,拖放到Winform上

代码:

axWindowsMediaPlayer1.URL = "文件路径,支持网络路径";
axWindowsMediaPlayer1.Ctlcontrols.play();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: