本主题演示如何为 Windows Phone 7.5 创建一个即使应用程序不再在前台运行时也继续播放音频的 Windows Phone 应用程序。有两种类型的后台音频应用程序。一个播放本地媒体,一个播放流媒体。本文介绍如何实现使用音频播放代理播放本地媒体的 Windows Phone 应用程序。

提示:

有关后台音频应用程序的体系结构说明,请参阅 Windows Phone 的后台音频概述。此概述还包含实现您的应用时要遵循的最佳做法。



“Windows Phone 音频播放代理”。然后,从您的应用程序中引用该后台代理。
提示:

有关在 Windows Phone 应用程序中播放后台音频的更多信息,请从 Windows Phone 的代码示例页面下载后台音频播放器示例后台音频流转化器示例

重要说明:

本文需要使用 Windows Phone SDK。请参阅安装 Windows Phone SDK

创建 Windows Phone 后台音频应用程序

  1. 通过选择“文件|新项目...”菜单命令创建一个新项目。

  2. 将显示“新建项目”对话框。展开“Visual C#”模板,然后选择“Silverlight for Windows Phone”模板。

  3. 选择“Windows Phone 应用程序”模板。根据需要填写“名称”,然后单击“确定”

  4. 选择 Windows Phone 7.1 作为目标版本,然后单击“确定”

  5. “解决方案资源管理器”中,右键单击“解决方案”节点,然后选择“添加 | 新项目...”

  6. “添加新项目”对话框中,单击“Windows Phone 音频播放代理”

  7. 根据需要填写“名称”,然后单击“确定”

  8. 现在,您的解决方案应该具有两个项目,即应用程序项目和后台代理项目。

    “添加引用...”
  9. “添加引用”对话框中,单击“项目”标签。选择我们之前创建的后台代理,然后单击“确定”

添加音频文件

创建用户界面

  1. 该应用程序具有一个非常简单的用户界面。在“解决方案资源管理器”中,双击“MainPage.xaml”以在 XAML 设计器/编辑器中将其打开。

  2. 将名为 TitlePanelStackPanel 元素替换为以下 XAML 代码:

    <!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="BACKGROUND AUDIO PLAYER" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="play a song" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel>
  • 将名为 ContentPanelGrid 元素替换为此 XAML 代码:

    <!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <StackPanel Orientation="Horizontal" Width="420" Margin="18,40,18,0" VerticalAlignment="Top"> <Button Content="prev" x:Name="prevButton" Height="140" Width="140" Click="prevButton_Click"/> <Button Content="play" x:Name="playButton" Height="140" Width="140" Click="playButton_Click"/> <Button Content="next" x:Name="nextButton" Height="140" Width="140" Click="nextButton_Click"/> </StackPanel> <TextBlock x:Name="txtCurrentTrack" Height="75" HorizontalAlignment="Left" Margin="12,193,0,0" VerticalAlignment="Top" Width="438" TextWrapping="Wrap" /> </Grid>
  • StackPanel 是用于排列按钮的非常方便的元素。在此示例中,我们通过相应地设置 Orientation 属性来水平排列按钮。

  • 设计器中的用户界面应该如下所示:

  • 修改用户界面的代码隐藏文件

    1. “解决方案资源管理器”中,右键单击“MainPage.xaml”,然后从上下文菜单中选择“查看代码”

    2. 向 MainPage.xaml.cs 文件的顶部添加以下 using 语句:

      using System.Windows.Navigation; using Microsoft.Phone.BackgroundAudio;
  • 向 MainPage.xaml.cs 的 MainPage 类中添加以下按钮单击事件处理程序代码:

    #region Button Click Event Handlers private void prevButton_Click(object sender, RoutedEventArgs e) { BackgroundAudioPlayer.Instance.SkipPrevious(); } private void playButton_Click(object sender, RoutedEventArgs e) { if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState) { BackgroundAudioPlayer.Instance.Pause(); } else { BackgroundAudioPlayer.Instance.Play(); } } private void nextButton_Click(object sender, RoutedEventArgs e) { BackgroundAudioPlayer.Instance.SkipNext(); } #endregion Button Click Event Handlers
  • 请注意,playButton_Click 处理程序实际上根据播放的当前状态在播放和暂停之间切换。

  • MainPage 类的构造函数中,添加 PlayStateChanged 事件的事件处理程序。

    new EventHandler(Instance_PlayStateChanged);
  • 相关文章: