让我们创建一个空wp7项目,要实现这个场景,首先我们将在mainpage.xaml中添加MediaElement来播放音频流。
<MediaElement Name="mediaElement" BufferingTime="0:0:5" AutoPlay="True" Margin="419,1,1,611" />
添加一个滚动条 一个开始一个暂停 一个剩余时间
<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>
播放音乐
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
播放跟暂停
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