×
Namespaces

Variants
Actions

Wp7从休眠/墓碑状态恢复音频流

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon Multimedia.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata

兼容于
文章
翻译:
最后由 hamishwillee 在 17 Jul 2013 编辑

本文演示在wp7上如何从休眠/墓碑状态恢复音频流。

简介

在这篇文章中,我们将展示当应用程序从休眠/墓碑状态回来时我们如何才能恢复音频流。在常规情况下,当我们按下设备上的Windows键,应用程序调用Application_Deactivated()事件,并进入休眠/墓碑状态。然后当我们按back键时回到应用程序,但它保存当前状态。在这种情况下,我们希望播放流式音频时,我们在设备上按Windows键,音频将停止播放,应用程序进入将休眠/墓碑状态。现在当我们按下设备上的back键,我们希望应用程序将运行而音频流将从上次停止的地方继续播放。

AudioResumeWP7.png

实现

让我们创建一个空wp7项目,要实现这个场景,首先我们将在mainpage.xaml中添加MediaElement来播放音频流。

<MediaElement Name="mediaElement" BufferingTime="0:0:5" AutoPlay="True" Margin="419,1,1,611" />

一个ProgressBar显示音频播放进度,两个Button (播放和暂停),和一个文本块TextBlock被添加来显示音频剩余时间

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >
<MediaElement Name="mediaElement" BufferingTime="0:0:5" AutoPlay="True" Margin="419,1,1,611" />
<ProgressBar IsHitTestVisible="True" IsIndeterminate="True" Height="3" HorizontalAlignment="Right" Margin="0,235,0,0" Name="progressBarAPlayerPage" VerticalAlignment="Top" Width="460" />
<TextBlock Height="30" HorizontalAlignment="Center" TextAlignment="Center" Margin="0,199,0,0" Name="progressBarTextAPlayerPage" Text="loading" Foreground="White" VerticalAlignment="Top" Width="444" />
<ProgressBar Height="60" Name="progressBar" Margin="12,256,6,337" />
<Button Content="play" Visibility="Collapsed" Height="72" HorizontalAlignment="Left" Margin="12,353,0,0" Name="play_btn" VerticalAlignment="Top" Width="438" Click="play_btn_Click" />
<Button Content="pause" Visibility="Collapsed" Height="72" HorizontalAlignment="Left" Margin="12,353,0,0" Name="pause_btn" VerticalAlignment="Top" Width="438" Click="pause_btn_Click" />
<TextBlock Height="58" Foreground="White" HorizontalAlignment="Left" Margin="226,146,0,0" Name="songTime" Text="" TextAlignment="Center" VerticalAlignment="Top" Width="224" Style="{StaticResource PhoneTextTitle1Style}" FontFamily="Segoe WP SemiLight" FontSize="42" />
<TextBlock Height="58" HorizontalAlignment="Left" Margin="12,146,0,0" Name="textBlock1" Text="Track Time :" VerticalAlignment="Top" Width="208" Style="{StaticResource PhoneTextTitle1Style}" FontFamily="Segoe WP SemiLight" FontSize="42" />
</Grid>

当我们启动应用程序,首先我们检查网络连接并在playsong()函数中设置mediaElement.Source到一个远程mp3文件。

void playSong()
{
play_btn.Opacity = 0.5;
pause_btn.Opacity = 0.5;
play_btn.IsHitTestVisible = false;
pause_btn.IsHitTestVisible = false;
songTime.Text = "00.00";
 
mediaElement.MediaOpened += new RoutedEventHandler(mediaElement_MediaOpened);
mediaElement.MediaEnded += new RoutedEventHandler(mediaElement_MediaEnded);
mediaElement.CurrentStateChanged += new RoutedEventHandler(mediaElement_CurrentStateChanged);
currentPosition.Tick += new EventHandler(currentPosition_Tick);
//enter url : http://www.yourdomain.com/audio.mp3
mediaElement.Source =new Uri( "REPLACE_THIS_WITH_REMOTE_MP3",UriKind.RelativeOrAbsolute);
}//end of funt

Note.pngNote: 请用一远程音频文件替换以上代码中的REPLACE_THIS_WITH_REMOTE_MP3

这将打开流媒体音频,其中有播放和暂停流媒体的选项。

private void play_btn_Click(object sender, RoutedEventArgs e)
{
mediaElement.Play();
}
 
private void pause_btn_Click(object sender, RoutedEventArgs e)
{
mediaElement.Pause();
}//end of funt

现在当用户按下设备上的Windows键,OnNavigatedFrom()方法被调用,在这里我们获取当前MediaElement音轨位置。

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
thisApp.iCurrentTrackPositionText = mediaElement.Position.TotalMinutes.ToString();
mediaElement.Stop();
}//end of funt

最后在我们处理Application_Deactivated()事件保存当前音轨信息到IsolatedStorage

private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
SaveCurrentTrackPosition();
}
 
void SaveCurrentTrackPosition()
{
 
//read file
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new StreamReader
StreamReader fileReader = null;
try
{
//Read the file from the specified location.
fileReader = new StreamReader(new IsolatedStorageFileStream("CurrentTrackPosition.txt", FileMode.Open, fileStorage));
//Read the contents of the file (the only line we created).
textFile = fileReader.ReadToEnd();
fileReader.Close();
}
catch
{
// MessageBox.Show("error saving data");
}
 
//replace text
textFile = textFile.Replace(textFile, iCurrentTrackPositionText);
//write file
//Obtain a virtual store for application
IsolatedStorageFile fileStorageUpdate = IsolatedStorageFile.GetUserStoreForApplication();
 
//Create a new StreamWriter, to write the file to the specified location.
StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("CurrentTrackPosition.txt", FileMode.OpenOrCreate, fileStorageUpdate));
fileWriter.Write(textFile);
//Close the StreamWriter.
fileWriter.Close();
}

这将使应用程序进入休眠/墓碑状态。现在当用户按下设备back按钮,会调用OnNavigatedTo()方法的Application_Activated()事件。

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ReadCurrentTrackLastPosition();
CurrentTrackLastKnowPosition = Convert.ToDouble(textFile);
initializeSection();
}

在这里ReadCurrentTrackLastPosition()函数从IsolatedStorage读取当前音轨关于流音频的信息。

 void ReadCurrentTrackLastPosition()
{
//read file
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new StreamReader
StreamReader fileReader = null;
try
{
//Read the file from the specified location.
fileReader = new StreamReader(new IsolatedStorageFileStream("CurrentTrackPosition.txt", FileMode.Open, fileStorage));
//Read the contents of the file (the only line we created).
textFile = fileReader.ReadToEnd();
fileReader.Close();
}
catch
{
MessageBox.Show("error loading data");
}
 
}

initializeSection()方法从上次它退出位置打开流音频。

 private void initializeSection()
{
progressBarTextAPlayerPage.Text = "loading";
progressBarAPlayerPage.IsHitTestVisible = true;
progressBarAPlayerPage.IsIndeterminate = true;
progressBarAPlayerPage.Visibility = Visibility.Visible;
 
 
progressBar.Visibility = Visibility.Collapsed;
play_btn.Visibility = Visibility.Collapsed;
pause_btn.Visibility = Visibility.Collapsed;
songTime.Visibility = Visibility.Collapsed;
 
string enternetConectivity = Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType.ToString();
 
if (enternetConectivity != "None")
{
 
playSong();
 
}
else
{
//MessageBox.Show("No internet connection");
}
}//end of funt

在Application_Activated()事件中我们没有检查IsApplicationInstancePreserved状态,因此不管当前应用程序是什么状态(休眠/墓碑),我们可以从它上次停止位置打开流音频。

总结

使用IsApplicationInstancePreserved参数,我们可以设置限制是否从休眠或只是墓碑状态中恢复。应用程序显示快速切换应用的行为,于是应用程序从休眠/墓碑状态恢复到其最后一个已知状态。


源代码

例子完整可用源代码:File:AudioResumeWP7.zip

This page was last modified on 17 July 2013, at 09:41.
77 page views in the last 30 days.
×