【问题标题】:Play sound clip in Windows Phone 8在 Windows Phone 8 中播放声音片段
【发布时间】:2014-03-01 17:37:27
【问题描述】:

我正在尝试做一些我认为很简单的事情,但事实并非如此。我想从我从 API 获得的 URI 播放声音剪辑。 URI 提供音频剪辑的绝对 URI。

我尝试过使用 MediaElement 组件并且效果很好,但它会在下载/播放剪辑时挂起 UI。这意味着糟糕的用户体验,并且可能也无法通过商店认证。

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

我想知道在不会挂起 UI 的 windows phone 8 应用程序中播放声音剪辑还有哪些其他选项

欢迎提出任何建议。

谢谢

【问题讨论】:

    标签: xaml audio windows-phone-8 xna


    【解决方案1】:

    在网络或互联网上使用媒体文件会增加应用程序的延迟。在手机加载文件之前,您无法开始播放媒体。使用 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;
    }
    

    【讨论】:

    • 谢谢。虽然这并不能解决无响应的 UI(在下载示例时 UI 仍处于锁定状态),但它确实提供了更好的 UX
    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多