C#为什么不能播放下一首歌曲?通过Windows Media Player

friendan 2013-05-21 05:48:42
我在窗体中,添加了Windows Media Player控件,正常点击播放时,是
可以播放音乐的,现在我想要实现的功能是顺序播放每一首歌曲,
我的主要代码如下:


//播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪
if(1==(int)this.SongPlayer.playState)
{
if (t.Equals("顺序播放"))
{
int row = (this.PlaySongRow + 1) % this.lstSong.Items.Count;
this.lstSong.Items[row].Selected = true;
this.SongPlayer.URL = this.lstSong.Items[row].SubItems[3].Text;
SongPlayer.Ctlcontrols.play(); //播放歌曲
ChangeSongColor();

//去掉以下这行代码后可以播放下一首歌曲,
//将其注释掉后就停止播放了。
MessageBox.Show(this.lstSong.Items[row].SubItems[3].Text);
}
}


-------------------------------------------------------------------------------------
//去掉以下这行代码后可以播放下一首歌曲,
//将其注释掉后就停止播放了。
MessageBox.Show(this.lstSong.Items[row].SubItems[3].Text);
--------------------------------------------------------------------------------------
我该如何修改代码来实现播放下一首歌曲的功能呢?
难道真的要弹出一个对话框吗?
我试过了单独用一个线程来播放,但是这时不是播放下一首,而是播放下一首的下一首了。

用线程播放时的代码如下:



//播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪
if(1==(int)this.SongPlayer.playState)
{
if (t.Equals("顺序播放"))
{
int row = (this.PlaySongRow + 1) % this.lstSong.Items.Count;
this.lstSong.Items[row].Selected = true;
this.SongPlayer.URL = this.lstSong.Items[row].SubItems[3].Text;

Thread thd = new Thread (delegate() {
SongPlayer.Ctlcontrols.play(); //播放歌曲
});
thd.Start();
ChangeSongColor();

//去掉以下这行代码后可以播放下一首歌曲,
//将其注释掉后就停止播放了。
//MessageBox.Show(this.lstSong.Items[row].SubItems[3].Text);
}
}








...全文
912 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sea000sea 2013-10-06
  • 打赏
  • 举报
回复
引用 5 楼 friendan 的回复:
问题解决了,我使用了一个定时器来判断歌曲的播放状态, 感觉Windows Media Player的播放状态改变事件不是很好用, 老是出现问题,无奈之下只好用定时器了,知道有更好的方法的盆友, 望告知一二。 我最终的代码如下:

//定时器,用时实现播放模式
        private void timerPlay_Tick(object sender, EventArgs e)
        {
            string t = this.cmbPlayType.SelectedItem.ToString();
            
            //播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪
            if (1 == (int)this.SongPlayer.playState&&bIsPlay)
            {
                if (t.Equals("顺序播放"))
                {
                    int row = (this.PlaySongRow + 1) % this.lstSong.Items.Count;
                    this.lstSong.Items[row].Selected = true;
                    this.SongPlayer.URL = this.lstSong.Items[row].SubItems[3].Text;
                    this.SongPlayer.Ctlcontrols.play();
                    ChangeSongColor();
                }
            }   
        }
我用的另一个方法,也是在网上搜索到的,跟你分享。 foreach (var item in lSong) { var songItem = as_MusicPlay.newMedia(item); as_MusicPlay.currentPlaylist.appendItem(songItem); } as_MusicPlay.Ctlcontrols.play();
friendan 2013-05-21
  • 打赏
  • 举报
回复
问题解决了,我使用了一个定时器来判断歌曲的播放状态, 感觉Windows Media Player的播放状态改变事件不是很好用, 老是出现问题,无奈之下只好用定时器了,知道有更好的方法的盆友, 望告知一二。 我最终的代码如下:

//定时器,用时实现播放模式
        private void timerPlay_Tick(object sender, EventArgs e)
        {
            string t = this.cmbPlayType.SelectedItem.ToString();
            
            //播放状态,1=停止,2=暂停,3=播放,6=正在缓冲,9=正在连接,10=准备就绪
            if (1 == (int)this.SongPlayer.playState&&bIsPlay)
            {
                if (t.Equals("顺序播放"))
                {
                    int row = (this.PlaySongRow + 1) % this.lstSong.Items.Count;
                    this.lstSong.Items[row].Selected = true;
                    this.SongPlayer.URL = this.lstSong.Items[row].SubItems[3].Text;
                    this.SongPlayer.Ctlcontrols.play();
                    ChangeSongColor();
                }
            }   
        }
friendan 2013-05-21
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
        //using System.Runtime.InteropServices;

        [DllImport("user32.dll", EntryPoint = "MessageBox")]
        public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType); 

        ...
        MessageBox(0, this.lstSong.Items[row].SubItems[3].Text, "caption", 0);
        ...
这个还是弹出对话框,真的不想这样,有谁顺序播放歌曲时弹出MessageBox的
threenewbee 2013-05-21
  • 打赏
  • 举报
回复
        //using System.Runtime.InteropServices;

[DllImport("user32.dll", EntryPoint = "MessageBox")]
public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType);

...
MessageBox(0, this.lstSong.Items[row].SubItems[3].Text, "caption", 0);
...
friendan 2013-05-21
  • 打赏
  • 举报
回复
引用 1 楼 zhoukang0916 的回复:
用线程干嘛?这个东西根本在这里用不着。。
因为我不想弹出个对话框呀,求解决方法。
PandaIT 2013-05-21
  • 打赏
  • 举报
回复
用线程干嘛?这个东西根本在这里用不着。。

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧