我正在使用 C#,我正在尝试让我的按钮单击,将我导航到新页面,以播放声音。问题是我听不到播放的声音,因为媒体元素在上一页中。有没有办法播放声音,但在所有页面中都能听到它?这是我的代码:
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
this.Frame.Navigate(typeof(Game));
select.Play();
}
选择是我的声音.wav,如果我没有导航代码,它会播放....
像这样播放从一种形式到另一种形式的声音文件应该可以正常工作。我将文件放在项目内的"声音"文件夹中
using Windows.ApplicationModel;
using Windows.Storage;
private async void ButtonPlay_Click(object sender, RoutedEventArgs e)
{
MediaElement snd = new MediaElement();
StorageFolder folder = await Package.Current.InstalledLocation.GetFolderAsync("Sounds");
StorageFile file = await folder.GetFileAsync("Alarm01.wav");
var stream = await file.OpenAsync(FileAccessMode.Read);
snd.SetSource(stream, file.ContentType);
snd.Play();
this.Frame.Navigate(typeof(Game));
}
这样,
我遇到的第一次打开应用程序时声音无法播放的问题得到了解决。 :)
using Windows.ApplicationModel;
using Windows.Storage;
MediaElement snd = new MediaElement();
private async void ButtonPlay_Click(object sender, RoutedEventArgs e)
{
StorageFolder folder = await Package.Current.InstalledLocation.GetFolderAsync("Sounds");
StorageFile file = await folder.GetFileAsync("Alarm01.wav");
var stream = await file.OpenAsync(FileAccessMode.Read);
snd.SetSource(stream, file.ContentType);
this.Frame.Navigate(typeof(Game));
}