2014-03-01 46 views
0

我正在尝试做一些我认为会非常简单的事情,但它不能证明这一点。我想从我从API获取的URI播放声音剪辑。该URI为音频剪辑提供绝对URI。在Windows Phone 8中播放声音剪辑

我试过使用MediaElement组件,它的工作原理,除了在剪辑下载/播放时挂起UI。这意味着糟糕的用户体验,并且可能无法通过商店认证。

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

我不知道我有什么其他选择在了Windows Phone 8的应用程序,不会挂在UI

任何建议,欢迎播放声音剪辑。

谢谢

回答

0

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

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; 
} 
+0

感谢。虽然这不能解决无响应的用户界面(在下载示例时用户界面仍处于锁定状态),但它确实提供了更好的用户体验 – LDJ

相关问题