gpt4 book ai didi

c# - Windows Phone 8.1 媒体元素

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:10 25 4
gpt4 key购买 nike

我目前有 2 个按钮,Audi TT RS 和 Audi R8当用户点击按钮 TT RS 时,它会播放一个 mp3,当用户点击按钮 R8 时,它会打开一个不同的 mp3 文件。

然而,这有效...如果用户单击其中一个,然后单击另一个。两个 mp3 文件都会打开,但听起来很糟糕。

这些是我的功能:

async void Audir8_Click(object sender, RoutedEventArgs e)
{
MediaElement mediaplayer = new MediaElement();
if (mediaplayer.CurrentState == MediaElementState.Playing)
{
mediaplayer.Stop();
}
// get folder app is installed to
var installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// get folders mp3 files are installed to.
var resourcesFolder = await installFolder.GetFolderAsync("Resources");
var mp3FilesFolder = await resourcesFolder.GetFolderAsync("mp3Files");
// open the mp3 file async
var audioFile = await mp3FilesFolder.GetFileAsync("audir8.mp3");
// var stream = await audioFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
using (var stream = await audioFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// play dat funky music
// MediaElement mediaplayer = new MediaElement();
mediaplayer.SetSource(stream, audioFile.ContentType);

var tcs = new TaskCompletionSource<bool>();
mediaplayer.CurrentStateChanged += (_, __) =>
{
if (mediaplayer.CurrentState != MediaElementState.Opening &&
mediaplayer.CurrentState != MediaElementState.Playing &&
mediaplayer.CurrentState != MediaElementState.Buffering)
// mediaplayer.CurrentState != MediaElementState.AcquiringLicense)
{
// Any other state should mean we're done playing
tcs.TrySetResult(true);
}
};
mediaplayer.Play();
await tcs.Task; // Asynchronously wait for media to finish
}
}

还有一个,其实是一样的..

 async void Audittrs_Click(object sender, RoutedEventArgs e)
{
MediaElement mediaplayer = new MediaElement();
if (mediaplayer.CurrentState == MediaElementState.Playing)
{
mediaplayer.Stop();
}
// get folder app is installed to
var installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// get folders mp3 files are installed to.
var resourcesFolder = await installFolder.GetFolderAsync("Resources");
var mp3FilesFolder = await resourcesFolder.GetFolderAsync("mp3Files");
// open the mp3 file async
var audioFile = await mp3FilesFolder.GetFileAsync("ttrs.mp3");
// var stream = await audioFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
using (var stream = await audioFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
// play dat funky music
// MediaElement mediaplayer = new MediaElement();
mediaplayer.SetSource(stream, audioFile.ContentType);

var tcs = new TaskCompletionSource<bool>();
mediaplayer.CurrentStateChanged += (_, __) =>
{
if (mediaplayer.CurrentState != MediaElementState.Opening &&
mediaplayer.CurrentState != MediaElementState.Playing &&
mediaplayer.CurrentState != MediaElementState.Buffering)
// mediaplayer.CurrentState != MediaElementState.AcquiringLicense)
{
// Any other state should mean we're done playing
tcs.TrySetResult(true);
}
};
mediaplayer.Play();
await tcs.Task; // Asynchronously wait for media to finish
}
}

我认为这不应该发生,因为我正在使用

MediaElement mediaplayer = new MediaElement();
if (mediaplayer.CurrentState == MediaElementState.Playing)
{
mediaplayer.Stop();
}

最佳答案

当您设置您的媒体播放器局部变量时,您正在创建一个新的媒体播放器,而不是重新使用您之前创建的那个。如果您重新使用之前创建的声音(比如在您的类中将其设为字段加属性),则 .Stop() 调用应该停止之前的声音。

如果执行此操作,则必须更改每个 ..._Click 方法的第一行以使用持久性 MediaElement。

例如,在你的类里面你可以这样做:

private MediaElement _mediaplayer;

public MediaElement Mediaplayer
{
get{
if( _mediaplayer == null )
{
_mediaplayer = new MediaElement();
}
return _mediaplayer;
}
}

然后在您的 ..._Click 方法中,换掉这个:

MediaElement mediaplayer = new MediaElement();

...为此:

MediaElement mediaplayer = this.Mediaplayer;

getter 应该完成剩下的工作,确保您始终对播放的各种声音使用相同的 MediaElement,从而允许您根据需要停止之前播放的声音。

关于c# - Windows Phone 8.1 媒体元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32614379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com