我想我的应用程序一起播放四个声音,我有四个MediaElements。当我运行app时,四个声音中只有三个发挥最佳效果。
MediaElement sound1 = new MediaElement { Source = new Uri("ms-appx:///Sounds/sound1.mp3"), AutoPlay = false };
MediaElement sound2 = new MediaElement { Source = new Uri("ms-appx:///Sounds/sound2.mp3"), AutoPlay = false };
MediaElement sound3 = new MediaElement { Source = new Uri("ms-appx:///Sounds/sound3.mp3"), AutoPlay = false };
MediaElement backgroundMusic = new MediaElement { Source = new Uri("ms-appx:///Sounds/backgroundMusic .mp3"), AutoPlay = true, Volume = 1, IsLooping = true };
backgroundMusic必须始终播放,并且点击按钮后必须播放sound1,sound2,sound3。
答案 0 :(得分:2)
MediaElements类一次只播放一个声音,您必须使用SoundEffect类。
1st:将参考(Microsofot.Xna.Framework)添加到您的项目中。
第二:你需要在你的page.xaml
中添加这两个类using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
第3名:试试这段代码。
public partial class Testing : PhoneApplicationPage
{
SoundEffect Sound1;
SoundEffect Sound2;
public Testing()
{
InitializeComponent();
// Load the sound file
StreamResourceInfo Sound1 = Application.GetResourceStream(new Uri("Assets/sound1.mp3", UriKind.Relative));
StreamResourceInfo Sound2 = Application.GetResourceStream(new Uri("Assets/sound1.mp3", UriKind.Relative));
Sound1 = SoundEffect.FromStream(Sound1.Stream);
Sound2 = SoundEffect.FromStream(Sound2.Stream);
Microsoft.Xna.Framework.FrameworkDispatcher.Update();
}
private void Sound1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Sound1.Play();
Sound2.Play();
}
}
确保添加声音文件设置为内容,这只是选择歌曲并转到其属性,你会发现那里的构建动作将其值更改为"内容&#34 ;