【问题标题】:Mp3 play class play songs in list but wait till one finishes before continuingMp3 播放类播放列表中的歌曲,但要等到一首完成后再继续
【发布时间】:2012-07-14 22:47:51
【问题描述】:

我目前正在处理 tutorial 使用 c# 和 winforms 播放 mp3 文件,但我添加了一个 datagridview 来列出歌曲,现在当我点击网格中的一首歌曲时,它播放歌曲就好了,但它只播放那一首歌,我想做的是在歌曲播放完毕后继续播放列表中的下一首歌曲。我已经尝试了 Thread.Sleep 与 audiolenght 作为时间,但这只是阻止整个应用程序工作,直到它完成睡眠,这根本不是我想要的,我对 winforms 有点陌生,所以如果有人可以指导我我需要更改以使其正常工作,我将非常感激。这是我到目前为止的代码:

private void dgvTracks_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        PlayFiles(e.RowIndex);
    }
    public void PlayFiles(int index)
    {
        try
        {
            int eof = dgvTracks.Rows.Count;
            for (int i = index; index <= eof; i++)
            {
                if (File.Exists(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString()))
                {
                    PlayFile(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString());
                    Application.DoEvents();
                    Thread.Sleep(TimeSpan.FromSeconds(mplayer.AudioLength));
                }
                else
                {
                    Exception a = new Exception("File doesn't exists");
                    throw a;
                }

            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, ex.Message, MessageBoxButtons.OK);
        }
    }
    public void PlayFile(string filename)
    {
        mplayer.Open(filename);
        mplayer.Play();
    }

【问题讨论】:

    标签: c# winforms multithreading datagridview thread-sleep


    【解决方案1】:

    我假设mplayerSystem.Windows.Media.MediaPlayer 的一个实例。

    您可以订阅MediaPlayer.MediaEnded 事件,并使用事件处理程序开始播放下一个文件。您需要将当前播放的索引存储在某处。

    ...
    
    int currentPlayIndex = -1;
    
    ...
    
    mplayer.MediaEnded += OnMediaEnded;
    
    ...
    
    private void OnMediaEnded(object sender, EventArgs args)
    {
        // if we want to continue playing...
        PlayNextFile();
    }
    
    ...
    
    public void PlayNextFile()
    {
        PlayFiles(currentPlayIndex + 1);
    }
    
    public void PlayFiles(int index)  
    {  
        try  
        {  
            currentPlayIndex = -1;
    
            int eof = dgvTracks.Rows.Count;  
            for (int i = index; index <= eof; i++)  
            {  
                if (File.Exists(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString()))  
                {  
                    currentPlayIndex = i;  // <--- save index
    
                    PlayFile(dsStore.Tables["Track"].Rows[i]["Filepath"].ToString());  
                    Application.DoEvents();  
                    Thread.Sleep(TimeSpan.FromSeconds(mplayer.AudioLength));  
                }  
                else  
                {  
                    Exception a = new Exception("File doesn't exists");  
                    throw a;  
                }  
            }  
        }  
        catch (Exception ex)  
        {  
            MessageBox.Show(ex.Message, ex.Message, MessageBoxButtons.OK);  
        }  
    }  
    

    【讨论】:

      【解决方案2】:

      StopClose 在播放新音频之前先播放。例如,如果您使用MediaPlayer.Play

      using System.Windows.Media;
      
      MediaPlayer mplayer = new MediaPlayer();
      
      public void PlayFile(string filename)
      {
          mplayer.Stop();
          mplayer.Close();
          mplayer.Open(filename);
          mplayer.Play();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        相关资源
        最近更新 更多