C# AxWindowsMediaPlayer剪辑有时无法播放

C# AxWindowsMediaPlayer剪辑有时无法播放,c#,.net,windows,media-player,C#,.net,Windows,Media Player,我有一个简单的单线程WindowsForms.NET4.5应用程序,用户可以在其中收听语音单词(wav文件),然后选择代表单词的正确图片 问题是,该剪辑有时(很少-约1%的时间和完全随机)不会播放 这是播放剪辑的方法: public static void PlayWordAudio(Word word, AxWMPLib.AxWindowsMediaPlayer player) { string tempFile = Path.GetTempFileName()

我有一个简单的单线程WindowsForms.NET4.5应用程序,用户可以在其中收听语音单词(wav文件),然后选择代表单词的正确图片

问题是,该剪辑有时(很少-约1%的时间和完全随机)不会播放

这是播放剪辑的方法:

    public static void PlayWordAudio(Word word, AxWMPLib.AxWindowsMediaPlayer player)
    {
        string tempFile = Path.GetTempFileName() + ".wav";

        MemoryStream stream = new MemoryStream(word.Audio);

        using (Stream fileStream = File.OpenWrite(tempFile))
        {
            stream.WriteTo(fileStream);
        }

        player.URL = tempFile;

        File.Delete(tempFile);
    }
有人能提出解决这个问题的办法吗?也许我不应该在方法末尾删除文件?但是临时文件会堆积起来


我使用的是Windows 7…

我想删除文件的速度比播放文件的速度快

你能用这个代替
File.Delete(tempFile)吗利用事件


如果将播放器对象保留很长时间,您可能必须取消订阅该事件。

我猜文件被删除的速度比播放的速度快

你能用这个代替
File.Delete(tempFile)吗利用事件


如果长时间保留玩家对象,您可能必须取消订阅活动。

看来我解决了这个问题。。。事实上是文件的删除导致了这个

解决方案:

public static void PlayWordAudio(Word word, AxWMPLib.AxWindowsMediaPlayer player)
    {
        string tempFile = Path.GetTempFileName() + ".wav";

        MemoryStream stream = new MemoryStream(word.Audio);

        using (Stream fileStream = File.OpenWrite(tempFile))
        {
            stream.WriteTo(fileStream);
        }

        player.URL = tempFile;

        RunDelayed(5000, File.Delete, tempFile); //if we delete file immediately then clip sometimes would not be played
    }        

    public delegate void DelayedFuncion(string param);

    public static void RunDelayed(int delay, DelayedFuncion function, string param = null)
    {
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        DelayedArgs args = new DelayedArgs() { delayedFunction = function, param = param };
        timer.Tag = args;
        timer.Tick += TimerElapsed;            
        timer.Interval = delay;

        timer.Start();
    }

    private static void TimerElapsed(object sender, EventArgs e)
    {
        System.Windows.Forms.Timer timer = sender as System.Windows.Forms.Timer;
        timer.Stop();
        DelayedArgs args = timer.Tag as DelayedArgs;
        args.delayedFunction(args.param);
    }


    class DelayedArgs
{
    public Util.DelayedFuncion delayedFunction;
    public string param;
}

看来我解决了这个问题。。。事实上是文件的删除导致了这个

解决方案:

public static void PlayWordAudio(Word word, AxWMPLib.AxWindowsMediaPlayer player)
    {
        string tempFile = Path.GetTempFileName() + ".wav";

        MemoryStream stream = new MemoryStream(word.Audio);

        using (Stream fileStream = File.OpenWrite(tempFile))
        {
            stream.WriteTo(fileStream);
        }

        player.URL = tempFile;

        RunDelayed(5000, File.Delete, tempFile); //if we delete file immediately then clip sometimes would not be played
    }        

    public delegate void DelayedFuncion(string param);

    public static void RunDelayed(int delay, DelayedFuncion function, string param = null)
    {
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        DelayedArgs args = new DelayedArgs() { delayedFunction = function, param = param };
        timer.Tag = args;
        timer.Tick += TimerElapsed;            
        timer.Interval = delay;

        timer.Start();
    }

    private static void TimerElapsed(object sender, EventArgs e)
    {
        System.Windows.Forms.Timer timer = sender as System.Windows.Forms.Timer;
        timer.Stop();
        DelayedArgs args = timer.Tag as DelayedArgs;
        args.delayedFunction(args.param);
    }


    class DelayedArgs
{
    public Util.DelayedFuncion delayedFunction;
    public string param;
}

您必须发布一些您正在使用的代码,以便我们能够帮助您。您必须发布一些您正在使用的代码,以便我们能够帮助您。我尝试了您的解决方案,但我发现:访问路径“C:\\Users\\David\\AppData\\Local\\Temp\\tmp5F17.tmp.wav”被拒绝。在文件上。删除行。。。不知道为什么。。可能文件仍在播放,因此未发布。如果我使用12而不是1,文件不会被删除。。。我下面的解决方案有效,所以我将使用它。不管怎样,Thanx-我没有想到这种可能性--也许它可以让它工作..我尝试了你的解决方案,但我得到了这样的结果:对路径“C:\\Users\\David\\AppData\\Local\\Temp\\tmp5F17.tmp.wav”的访问被拒绝。在文件上。删除行。。。不知道为什么。。可能文件仍在播放,因此未发布。如果我使用12而不是1,文件不会被删除。。。我下面的解决方案有效,所以我将使用它。不管怎样,Thanx——我没有想到这种可能性——也许有可能让它发挥作用。。