锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 流媒体开源 / 媒体播放器类
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。

媒体播放器类

Introduction 介绍

I little while ago, I found myself writing a media player, and consequently was looking for an easy way to play MP3, Wav, midi, and other file types as well. I came across the WindowsMediaPlayerClass. It can be found in the COM library WMP.dll. However, it provided no features for fading or cross-fading songs. So, I wrote a more advanced wrapper class for it that uses WindowsMediaPlayerClass objects to fade and cross-fade songs.

不久前,我发现自己写一个媒体播放器,因此寻找一种简单的方式播放MP3,Wav,midi和其他文件类型。 我遇到的 WindowsMediaPlayerClass 。 它可以在COM库WMP.dll里找到 。 然而,它没有提供特性来支持歌曲淡入淡出。 所以,我写了一个更高级的包装类 WindowsMediaPlayerClass来支持此特性。

Using the code代码使用

To use my code, download the source files and add the Media Player.cs file to your existing/new project. Then you must add a reference to the COM library WMPLib. Its description is Windows Media Player, and the file name is wmp.dll. Finally, make sure the reference's attribute Embed Interop Types is set to false. This is necessary for VS2010, I am not sure about earlier versions.

要使用我的代码,下载源文件并添加Meia Player.cs 文件到现有/新项目。 那么你必须添加一个引用COM库WMPLib。 它的描述是Windows媒体播放器,文件名是wmp.dll。 最后,确保引用的属性 嵌入的互操作类型 设置为false。 这对于VS2010是必要的,关于早期版本我不确定。

The class uses two WindowsMediaPlayerClass objects to play files and to enable cross-fading. When you are using the class, it appears only as if there is one player though. With the exception of events, which I will describe later.

这个类使用了两个 WindowsMediaPlayerClass 对象来播放文件和启用跨歌曲淡出淡入。 当你使用类,似乎只有一个播放器。 事件异常处理稍后我将描述。

The code has five main methods: Play, Pause, Stop, Resume, and Fade. These are the only public methods. Play is fairly basic; if the player is paused, it resumes play. If the player is stopped, it plays the file specified. Depending on whether you have set fade to true, the player will either just use player one for playing without fading or cross-fading. However, if you want fading/cross-fading, the player alternates between the two internal players, fading them in and out as is relevant.

有五个主要方法的代码: 播放 , 暂停 , 停止 , 恢复 , 淡入 。 这些是唯一的公共方法。 播放是相当基本的,如果播放器是暂停,它可恢复播放。 如果播放器停止,它指定的文件。 取决于你是否有淡出为true,播放器将只使用播放器打不淡出或同时淡出淡入。 然而,如果你想要消失/同时淡出淡入,播放器之间的交替两个内部的播放器,他们是相关的。

Pause simply pauses the current internal player. If a fade is specified, it starts the fade process. Stop does the same thing except it stops the internal player completely, making it lose its current position. Also, Stop will set the player state to stopped so the first subsequent call to Play will play the new specified file. The first call after Pause will make the player continue playing the current track from the last position. The first call to Play after Pause will call Resume, so Resume and Play can be used interchangeably. The final method Fade simply starts a fade from the current volume to a new specified volume.

暂停 只是暂停当前的内部的播放器。 如果指定一个淡出,它开始消退过程。 停止 做同样的事,除非它停止内部播放器完全,使它失去它的当前位置。 同时, 停止 将这名播放器状态停止第一个后续调用 播放 将指定的新文件。 后的第一个电话 暂停 会让播放器继续播放当前跟踪从最后一个位置。 第一次调用 播放 后 暂停 将调用 的简历 ,所以 的简历 和 播放 可以互换使用。 最后的方法 淡出 只是开始淡出当前卷到一个新的指定卷。

The main code that is useful is Crossfade, which fades one song up and another down in a set period of time, thus creating a linear cross-fade effect. The code uses a timer ticking every 500 milliseconds to adjust the volumes of the two internal players by a small amount, specified before the cross-fade starts, each time a tick event occurs. The volume may be changed during a fade/cross-fade; however, a sudden jump in volume would be heard at the end of the fade/cross-fade if you do. It is advisable, therefore, not to change the volume during a fade/cross-fade.

主要是有用的代码 淡入淡出 消失了一首歌,另一个在规定的一段时间内,从而创建一个线性同时淡出淡入的效果。 代码使用一个计时器定时每500毫秒调整卷的两个内部由少量播放器,同时淡出淡入开始之前指定,每次tick事件发生。 体积可能会改变在淡出/同时淡出淡入;然而,突然跳体积能听见淡入/淡出入年底如果你。 因此,建议不要改变音量在淡入/淡出入。

The first chunk of code in CrossfadeTimer_Tick determines whether it is the start of a cross-fade. If it is, then it starts the new player at volume 0, ready to fade up. The code is as follows: 第一块代码 CrossfadeTimer_Tick 决定是否同时淡出淡入的开始。 如果是,那么它开始新播放器卷0,准备消失了。 的代码如下:

 if (CrossfadeTotalRunTime <= 0)
    {
    switch (StoppingPlayer)
    {
    case Players.Player1:
    PlayingPlayer = Players.Player2;
    Player2.volume = 0;
    Player2.URL = Player2Song;
    Player2.play();
    break;
   case Players.Player2:
    PlayingPlayer = Players.Player1;
    Player1.volume = 0;
    Player1.URL = Player1Song;
    Player1.play();
    break;
    }
    }

The next chunk runs while the cross-fade is occurring. It adjusts the volume of both the stopping and playing players, fading the playing up and stopping down. This is the cross-fade. 下一个块同时淡出淡入。 它调整停止音量和播放音量,淡入的播放音量增加,停止的降低。 这是同时淡出淡入。

 else if(CrossfadeTotalRunTime < (CrossfadeTime * 1000))
    {
    switch (PlayingPlayer)
    {
    case Players.Player1:
    {
    Player1.volume = (int)(CrossfadeVolumeAdjustment * 
    ((float)CrossfadeTotalRunTime / 1000));
    Player2.volume = (int)(Volume - (CrossfadeVolumeAdjustment * 
    (float)(CrossfadeTotalRunTime / 1000)));
    }
    break; 
     case Players.Player2:
    {
    Player1.volume = (int)(Volume - (CrossfadeVolumeAdjustment * 
    ((float)CrossfadeTotalRunTime / 1000)));
    Player2.volume = (int)(CrossfadeVolumeAdjustment * 
    ((float)CrossfadeTotalRunTime / 1000));
    }
    break;
    }
    }

The final chunk runs when the fade is over. It does not stop the stopping player, but lets it run till its media ends. Also, it makes sure the player's volume of the internal player is the same as the volume set by the user. This is the reason a change in volume during a fade/cross-fade will result in a sudden jump at the end of the fade. The final volume used is just set; if it is different, there is no smooth fade between the two volumes. To implement that, one would increase the apparent length of the cross-fade/fade, and would be harder to implement than not changing the volume in the first place.

最后一块运行时淡出结束。 它不停止将停止的播放器,但让它运行的媒体结束。 此外,它确保播放器的体积和用户设置的内部播放器是一样的体积。 这个体积变化的原因在淡入/淡出入将导致突然跳结束时消失。 最后的体积是集使用;如果它是不同的,没有两卷之间的顺利消退。 来实现,一个明显的长度会增加同时淡出淡入/淡出,并将比不改变更难实现卷放在第一位。

 else if (CrossfadeTotalRunTime >= (CrossfadeTime * 1000))
    {
    CrossfadeTimer.Enabled = false;
    CrossfadeTimer.Stop();
    InCrossfade = false; 
     switch (PlayingPlayer)
    {
    case Players.Player1:
    Player1.volume = Volume;
    Player2.volume = 0;
    break;
    case Players.Player2:
    Player2.volume = Volume;
    Player1.volume = 0;
    break;
    }
    }

The final thing to note is that events such as media-end are fired even if externally you have only just called Play. This is because separate events are fired for each player, but externally, they appear as one player, which sounds confusing, but is useful because, if say, you are playing a playlist of cross-fading tracks, you will still get the media-end event even after the next track has started playing. This allows easier programming of play counts and similar song attributes.

最后要注意的是,事件如media-end解雇即使外部你才刚刚被称为 播放 。 这是因为每个播放器单独的事件被解雇,但外部,他们表现为一个播放器,这听起来让人困惑,但十分有用,因为,假如说,你播放同时淡出淡入曲目的播放列表,你仍然会media-end事件即使下一个跟踪已经开始播放。 这使得更容易编程的计数和类似的属性。

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内