音乐突然停止应用程序打(Music suddenly stops playing in applic

2019-10-29 04:15发布

我有这样的代码:

private void OtworzPlikButton_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.ShowDialog();
        string directory = ofd.FileName;
        WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
        wplayer.URL = directory;
        wplayer.controls.play(); 
    }

我越来越从打开文件对话框的文件目录,我使用wplayer开始播放音乐。 起初,它是工作不错,但在20-30秒后,突然音乐停止播放,我想不通为什么。 我玩的MP3文件。

Answer 1:

您应该创建的实例WindowsMediaPlayer中的一流水平范围(不内OtworzPlikButton_Click事件处理函数的范围)。

WMPLib.WindowsMediaPlayer wplayer;

private void OtworzPlikButton_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.ShowDialog();
    string directory = ofd.FileName;
    wplayer = new WMPLib.WindowsMediaPlayer();
    wplayer.URL = directory;
    wplayer.controls.play(); 
}


文章来源: Music suddenly stops playing in application