在Windows Phone 8中播放声音片段(Play sound clip in Windows Phone 8)

编程入门 行业动态 更新时间:2024-10-26 20:35:02
在Windows Phone 8中播放声音片段(Play sound clip in Windows Phone 8)

我正在尝试做一些我认为非常简单的事情,但它没有证明这一点。 我想从我从API获取的URI中播放声音片段。 URI为音频剪辑提供绝对URI。

我尝试过使用MediaElement组件,但是在下载/播放剪辑时它会挂起UI。 这意味着用户体验不佳,也可能无法通过商店认证。

我也尝试过XNA框架中的SoundEffect类,但是抱怨绝对的URI - 它似乎只适用于相对链接,因此不够。

我想知道我在Windows手机8应用程序中播放声音片段有什么其他选择,不会挂起UI

欢迎任何建议。

谢谢

I'm trying to do something that I thought would be pretty simple, but its not proving that way. I want to play a sound clip from a URI that I'm obtaining from an API. The URI provides an absolute URI to the audio clip.

I've tried using the MediaElement component and that works, except it hangs the UI while the clip is downloading/playing. This means a poor user experience and probably wouldn't get past store certification either.

I've also tried the SoundEffect class from the XNA framework, but that complains about an absolute URI – it seems this only works with relative links and thus wont suffice.

I'm wondering what other options I have for playing a sound clip in a windows phone 8 app that wont hang the UI

Any suggestions welcomed.

Thanks

最满意答案

在网络或Internet上使用媒体文件会增加应用程序的延迟。 在手机加载文件之前,您无法开始播放媒体。 使用MediaElement.MediaOpened确定媒体何时就绪,然后调用.Play();

当然,您需要让用户知道媒体正在下载。 我的示例使用SystemTray ProgressIndicator向用户显示消息。

XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel> <Button x:Name='PlayButton' Click='PlayButton_Click' Content='Play Media' /> <MediaElement x:Name='media1' MediaOpened='Media1_MediaOpened' AutoPlay='False' /> </StackPanel> </Grid>

private void Media1_MediaOpened(object sender, RoutedEventArgs e) {
  // MediaOpened event occurs when the media stream has been
  // validated and opened, and the file headers have been read.

  ShowProgressIndicator(false);
  media1.Play();
}

private void PlayButton_Click(object sender, RoutedEventArgs e) {
  // the SystemTray  has a ProgressIndicator 
  // that you can use to display progress during async operations.
  SystemTray.ProgressIndicator = new ProgressIndicator();
  SystemTray.ProgressIndicator.Text = "Acquiring media - OverTheTop.mp3 ";

  ShowProgressIndicator(true);

  // Get the media
  media1.Source =
    new Uri(@"http://freesologuitar.com/mps/DonAlder_OverTheTop.mp3",
              UriKind.Absolute);
}

private static void ShowProgressIndicator(bool isVisible) {
  SystemTray.ProgressIndicator.IsIndeterminate = isVisible;
  SystemTray.ProgressIndicator.IsVisible = isVisible;
}

Using media files on a network or the Internet is going to add latency to the app. You can't start playing the media until the phone has loaded the file. Use the MediaElement.MediaOpened to determine when the media is ready, then call .Play();

Of course, you need to let the users know that the media is downloading. My example uses the SystemTray ProgressIndicator to show the user a message.

XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel> <Button x:Name='PlayButton' Click='PlayButton_Click' Content='Play Media' /> <MediaElement x:Name='media1' MediaOpened='Media1_MediaOpened' AutoPlay='False' /> </StackPanel> </Grid>

CODE

private void Media1_MediaOpened(object sender, RoutedEventArgs e) {
  // MediaOpened event occurs when the media stream has been
  // validated and opened, and the file headers have been read.

  ShowProgressIndicator(false);
  media1.Play();
}

private void PlayButton_Click(object sender, RoutedEventArgs e) {
  // the SystemTray  has a ProgressIndicator 
  // that you can use to display progress during async operations.
  SystemTray.ProgressIndicator = new ProgressIndicator();
  SystemTray.ProgressIndicator.Text = "Acquiring media - OverTheTop.mp3 ";

  ShowProgressIndicator(true);

  // Get the media
  media1.Source =
    new Uri(@"http://freesologuitar.com/mps/DonAlder_OverTheTop.mp3",
              UriKind.Absolute);
}

private static void ShowProgressIndicator(bool isVisible) {
  SystemTray.ProgressIndicator.IsIndeterminate = isVisible;
  SystemTray.ProgressIndicator.IsVisible = isVisible;
}

                    
                     
          

更多推荐

本文发布于:2023-07-09 14:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1087156.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   声音   Phone   Windows   sound

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!