单击事件上的Windows Phone 8.1音乐

时间:2015-09-15 20:42:33

标签: c# wpf windows-phone-8.1

我正在尝试在点击按钮时播放mp3文件,我正在尝试使用他所做的功能:How can I play mp3 files stored in my Windows Phone 8.1 solution? 但它说效果不好,它说

choise.type_value

代码:

Error   1   The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.    c:\users\halt\documents\visual studio 2013\Projects\HDCAR\HDCAR\MainPage.xaml.cs    55  35  HDCAR

1 个答案:

答案 0 :(得分:3)

您需要将其设为async void方法:

async void Alfa4c_Click(object sender, RoutedEventArgs e)
{
   // Your code...

这允许您在方法中使用await

请注意,您可能还需要正确处理您的信息流。这需要一些额外的工作:

    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 += (o,e) =>
        {
            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
    }
}