Windows phone 墓碑机制的一些源码
2012-06-15 15:09
337 查看
让我们创建一个空wp7项目,要实现这个场景,首先我们将在mainpage.xaml中添加MediaElement来播放音频流。
添加一个滚动条 一个开始一个暂停 一个剩余时间
播放音乐
播放跟暂停
现在当用户按下设备上的Windows键,OnNavigatedFrom()方法被调用,在这里我们获取当前MediaElement音轨位置。
最后在我们处理Application_Deactivated()事件保存当前音轨信息到IsolatedStorage。
这将使应用程序进入休眠/墓碑状态。现在当用户按下设备back按钮,会调用OnNavigatedTo()方法的Application_Activated()事件。
在这里ReadCurrentTrackLastPosition()函数从IsolatedStorage读取当前音轨关于流音频的信息。
initializeSection()方法从上次它退出位置打开流音频。
<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
相关文章推荐
- 理解Windows Phone 7应用程序执行模型,墓碑机制,启动器和选择器及更多内容——Part 3
- Windows Phone开发-- 多任务之墓碑机制
- Windows Phone应用程序的生命周期与墓碑机制
- Windows Phone 7 墓碑机制
- 用cocos2d-x做一个简单的windows phone 7游戏:墓碑机制和收尾工作(完)
- Windows Phone 7 开发 31 日谈——第14日:墓碑机制(多任务)
- Windows Phone 7 开发 31 日谈——第14日:墓碑机制(多任务)
- 理解Windows Phone 7应用程序执行模型,墓碑机制,启动器和选择器及更多内容——Part 1
- windows phone 7为什么非要实现墓碑机制呢?
- Windows Phone开发(六)-- 多任务之墓碑机制
- Windows Phone 31 日谈——第14日:墓碑机制(多任务)
- Windows Phone 7 开发 31 日谈——第14日:墓碑机制(多任务)
- 理解Windows Phone 7应用程序执行模型,墓碑机制,启动器和选择器及更多内容——Part 2
- 对Android广播接收与发出机制的一些AMS以外的源码分析
- Windows Phone 7 开发 31 日谈——第14日:墓碑机制(多任务)
- 用cocos2d-x做一个简单的windows phone 7游戏:墓碑机制和收尾工作(完)
- 消息传递机制的具体实现过程(分析源码之后的总结)
- 从使用Handler致内存泄漏角度源码追踪Handler工作机制
- 剖析ActivityManagerService源码,了解广播接收和发送机制(二)
- Android触摸屏事件派发机制详解与源码分析一(View篇)