WPF 中没有带有 WAV 和媒体播放器的音频



我正在尝试将背景音乐添加到我的WPF程序中。我还希望其他声音在背景音乐"上"发生。我尝试使用SoundPlayer但是,这一次只能播放一段音频。

我现在正在尝试使用MediaPlayer但我无法播放任何音频。这是我的代码:

在我的ShellViewModel中,我开始背景音乐:

Sounds.StartBackgroundMusic()

在我的声音课上,我有以下内容:

private static MediaPlayer _backgroundMusic = new MediaPlayer();
public static void StartBackgroundMusic()
{
_backgroundMusic.Open(new Uri("pack://application:,,,/Assets/Sounds/backgroundmusic.wav"));
_backgroundMusic.MediaEnded += new EventHandler (BackgroundMusic_Ended);
_backgroundMusic.Play();
}
private static void BackgroundMusic_Ended(object sender, EventArgs e)
{
_backgroundMusic.Position = TimeSpan.Zero;
_backgroundMusic.Play();
}

由于我希望背景音乐连续循环,因此我使用此问题的答案来添加BackgroundMusic_Ended事件。有人可以帮忙说明为什么我的音频无法播放吗?

我试图重现该问题,但它按预期工作。

我在Visual Studio中创建了一个新的WPF应用程序,并在MainWindow.xaml.cs中使用以下代码:

using System;
using System.Windows;
using System.Windows.Media;
namespace WpfApp1
{
public partial class MainWindow : Window
{
private static MediaPlayer _backgroundMusic = new MediaPlayer();
public MainWindow()
{
InitializeComponent();
StartBackgroundMusic();
}
public static void StartBackgroundMusic()
{
_backgroundMusic.Open(new Uri(@"C:<path-to-sound-file>music.wav"));
_backgroundMusic.MediaEnded += new EventHandler(BackgroundMusic_Ended);
_backgroundMusic.Play();
}
private static void BackgroundMusic_Ended(object sender, EventArgs e)
{
_backgroundMusic.Position = TimeSpan.Zero;
_backgroundMusic.Play();
}
}
}

您是否尝试使用"常规"文件路径而不是"pack://..."来加载声音文件?当我使用错误的路径时,我没有得到任何声音,也没有错误消息或异常。

由于我无法使用MediaPlayer播放嵌入式资源,并且SoundPlayer一次只能播放一种声音,因此我使用了它们的组合并将嵌入式背景音乐资源保存到磁盘上,以便MediaPlayer可以播放它。我在这里做一个关于它的博客

这是我所做的:

我设置了我的SoundPlayer,因为这是两者中最简单的一个。我使用嵌入的资源创建了一个新的 SoundPlayer 对象

private static readonly SoundPlayer _soundOne = new SoundPlayer(WPF.Properties.Resources.soundOne);

现在MediaPlayer.我确保我的音频文件在 Visual Studio 中文件属性中的"生成操作"下设置为嵌入资源。现在我们已经完成了此操作,我们可以创建将嵌入的 WAV 文件保存到磁盘上的 %temp% 位置的方法:

public static void SaveMusicToDisk(){
//This sets up a new temporary file in the %temp% location called "backgroundmusic.wav"
using (FileStream fileStream = File.Create(Path.GetTempPath() + "backgroundmusic.wav")){
//This them looks into the assembly and finds the embedded resource
//inside the WPF project, under the assets folder
//under the sounds folder called backgroundmusic.wav
//PLEASE NOTE: this will be different to you
Assembly.GetExecutingAssembly().GetManifestResourceStream("WPF.Assets.Sounds.backgroundmusic.wav").CopyTo(fileStream);
}
}

我们通过创建一个新的 MediaPlayer 对象并使用临时文件位置播放音频来播放它:

//Create a new MediaPlayer object
private static readonly MediaPlayer _backgroundMusic = new MediaPlayer();
public static void StartBackgroundMusic(){
//Open the temp WAV file saved in the temp location and called "backgroundmusic.wav"
_backgroundMusic.Open(new Uri(Path.Combine(Path.GetTempPath(), "backgroundmusic.wav")));
//Add an event handler for when the media has ended, this way
//the music can be played on a loop
_backgroundMusic.MediaEnded += new EventHandler(BackgroundMusic_Ended);
//Start the music playing
_backgroundMusic.Play();
}

我的BackgroundMusic_Ended方法如下所示,只是确保音乐在完成后始终重新启动:

private static void BackgroundMusic_Ended(object sender, EventArgs e){
//Set the music back to the beginning
_backgroundMusic.Position = TimeSpan.Zro;
//Play the music
_backgroundMusic.Play();
}

然后我只需要担心处理程序关闭时处理对象并清理临时文件。

最新更新