首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AxWindowsMediaPlayer剪辑有时无法播放

AxWindowsMediaPlayer剪辑有时无法播放
EN

Stack Overflow用户
提问于 2013-03-18 23:17:57
回答 2查看 1.1K关注 0票数 1

我有一个简单的单线程windows forms .NET 4.5应用程序,其中用户听口语(wav文件),然后选择代表单词的正确图片。

问题是,剪辑有时(非常罕见-大约1%的时间,完全随机)不会播放……

这是播放剪辑的方法:

代码语言:javascript
复制
    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...

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-19 01:25:51

看起来我解决了这个问题。实际上是文件的删除导致了这一点...

解决方案:

代码语言:javascript
复制
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;
}
票数 0
EN

Stack Overflow用户

发布于 2013-03-19 01:04:00

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

您可以尝试使用PlayStateChange事件来代替File.Delete(tempFile);

代码语言:javascript
复制
player.PlayStateChange += (snd, psce) => {
     switch (psce.newState)
    {
        case 1:    // Stopped (maybe use 12 =>  Last )
            File.Delete(tempFile);
            break;
        default:
            Debug.WriteLine(psce.newState);
            break;
    }
};

如果长时间保留player对象,则可能需要取消订阅事件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15480303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档