Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows Forms C#WMPLib在播放mp3文件时崩溃_C#_Windows_Forms_Mp3 - Fatal编程技术网

Windows Forms C#WMPLib在播放mp3文件时崩溃

Windows Forms C#WMPLib在播放mp3文件时崩溃,c#,windows,forms,mp3,C#,Windows,Forms,Mp3,我正在做一个简单的mp3播放器。我正在使用WMPLib播放mp3文件 我将在数据网格视图中显示所有曲目,在其中双击播放选定曲目。该程序播放正确的歌曲,但一旦我开始滚动或按下按钮相当快的歌曲停止播放 这是我用来播放单个曲目的方法: public static void playTrack(int t) { Library l = new Library(); WMPLib.WindowsMediaPlayer song = new WMPLib.WindowsMediaPlayer

我正在做一个简单的mp3播放器。我正在使用WMPLib播放mp3文件

我将在数据网格视图中显示所有曲目,在其中双击播放选定曲目。该程序播放正确的歌曲,但一旦我开始滚动或按下按钮相当快的歌曲停止播放

这是我用来播放单个曲目的方法:

public static void playTrack(int t)
{
    Library l = new Library();
    WMPLib.WindowsMediaPlayer song = new WMPLib.WindowsMediaPlayer();
    song.URL = l.myLibrary[t].Patch;
    song.controls.play();
}
当在单独的类中引发
dataGridView1\u CellContentDoubleClick
时,我调用上述方法

你们知道为什么会这样吗?
我需要使用多线程来修复它吗

我还认为我的程序的UI太“沉重”,因为我在自定义控件中使用了多个嵌套面板。我包括一个正在运行的应用程序的打印屏幕


好的,我找到了问题的答案。 只需将Library和WMPLib声明为我的方法之外的字段,如下所示:

static Library l;
        static WMPLib.WindowsMediaPlayer song; 
        public static void playTrack(int t)
        {
            l = new Library();
            song = new WMPLib.WindowsMediaPlayer();
            song.URL = l.myLibrary[t].Patch;
            song.controls.play();
        }

我希望这对某人有所帮助:)

WMP是一个非常昂贵的组件。您的机器将在所有这些对象的重量下发出嘎嘎声,GC运行的频率不足以清理它们。只创建WMPLib.WindowsMediaPlayer的一个实例,它可以播放您需要的任何歌曲。