音乐播放器在按下下一步按钮太快时会冻结一点

本文关键字:冻结 一点 播放器 下一步 按钮 音乐 | 更新日期: 2023-09-27 18:35:30

基于:

C# Windows 表单和使用 WMPLib


问题:

当按下"下一步"或"上一个"按钮太快时,应用程序会冻结一点(无法控制)。


守则:

private void next_event()
{
    _paused = false;
    if (list[current_index].Rows.Count != 0 && _playing == true)
    {
        if (option.random.Checked == true)
        {
            nextRandom(1);
        }
        else if (option.order.Checked == true)
        {
            if (list[current_index].Rows.IndexOf(_musicData[_NowPlaying]) == list[current_index].Rows.Count - 1)
            {
                setNowPlaying((Guid)list[current_index].Rows[0].Cells["Key"].Value);
            }
            else
            {
                setNowPlaying((Guid)list[current_index].Rows[list[current_index].Rows.IndexOf(_musicData[_NowPlaying]) + 1].Cells["Key"].Value);
            }
        }
        seek_bar.Value = 0;
        play();
    }
}
private void prev_event()
{
    _paused = false;
    if (list[current_index].Rows.Count != 0 && _playing == true)
    {
        if (option.random.Checked == true)
        {
            nextRandom(2);
        }
        else if (option.order.Checked == true)
        {
            if (list[current_index].CurrentRow.Index == 0)
            {
                setNowPlaying((Guid)list[current_index].Rows[list[current_index].Rows.Count - 1].Cells["Key"].Value);
            }
            else
            {
                setNowPlaying((Guid)list[current_index].Rows[list[current_index].Rows.IndexOf(_musicData[_NowPlaying]) - 1].Cells["Key"].Value);
            }

        }
        seek_bar.Value = 0;
        play();
    }
}
private void play() // play
{
    button3.Text = "Pause";
    dmp_status.Text = "Playing...";
    _paused = false;
    if (list[current_index].Rows.Count != 0)
    {
        if (File.Exists(_musicData[_NowPlaying].Cells["FileLocation"].Value.ToString()))
        {
            if (_playing == true) wmp.controls.stop();
            if(wmp != null) wmp.close();
            wmp = new WMPLib.WindowsMediaPlayer();
            wmp.PlayStateChange += new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(PlayStateChange);
            seek_bar.Value = 0;
            setNowPlayingLabel(_musicData[_NowPlaying]);
            if (!_musicData.ContainsKey(_LastPlaying)) _LastPlaying = Guid.Empty;
            setNowPlayingColor(_musicData[_LastPlaying],_musicData[_NowPlaying]);
            //wmp.URL = list[current_index].CurrentRow.Cells["FileLocation"].Value.ToString();
            wmp.URL = _musicData[_NowPlaying].Cells["FileLocation"].Value.ToString();
            wmp.controls.play();
            wmp.settings.rate = wmp_rate;
            wmp.settings.volume = volume_pos;
            if (_playing == false) _playing = true;
        }
        else 
        {
            next_event(); 
        }
    }
}

问题:

我很难弄清楚为什么他们在播放每首音乐之前需要延迟(冻结),当用户连续按下下一步按钮时,这会产生问题。

音乐播放器在按下下一步按钮太快时会冻结一点

我认为这是由于重新进入。当后续事件触发而第一个事件尚未完成时发生。要防止这种情况:

// add this line to your class member
private Object syncRoot = new Object();
// add this block to your next_event() method
if (!Monitor.TryEnter(syncRoot)) return;
try {
    // add your existing code here
}
finally {
    Monitor.Exit(syncRoot);
}