播放WPF中的音频文件 [英] Playing audio files in WPF

查看:408
本文介绍了播放WPF中的音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发挥WPF音频文件和我目前使用以下code:

I am trying to play audio files in WPF and I am currently using the following code:

    FileTextBox.Text = selectedFileName;
    MediaPlayer mp = new MediaPlayer();
    mp.Open(new Uri(selectedFileName, UriKind.Relative ));
    mp.Play();

它的工作很好,但它不播放声音。我在做什么错了?

It's working well, except that it doesn't plays the sound. What am I doing wrong?

推荐答案

您MediaPlayer对象可能是被垃圾收集它有上场机会的文件,因为它具有局部范围之前。尝试使媒体播放器对象具有应用程序生命周期类的成员变量,看看这个修复它。

Your MediaPlayer object is probably being garbage collected before it has a chance to play the file because it has local scope. Try making the media player object a member variable of a class that has application lifetime and see if this fixes it.

这篇关于播放WPF中的音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆