Windows Phone实用开发技巧(37):创建一个全局播放器
2012-09-07 15:44
633 查看
在做windows phone 开发的时候,有时候我们需要在某些页面中进行声音的播放。而播放的方式也有多种:
1. 使用MediaElement
2. 使用SoundEffect
3. 使用后台播放
SoundEffect只能播放wav格式的文件;后台播放可以在程序退出后仍然继续播放;MediaElement适用于大多数情况,但不能实现后台播放,MediaElement依赖页面并且在页面中只有一个实例可以正常工作。
本文讲讲如何在应用中使用MediaElement作为程序全局播放器,并提供一个帮助类。
由于MediaElement依赖于UI,我们不能在ViewModel中创建一个MediaElement实例进行声音的播放,所以我们需要将MediaElement放置在一个UI容器中,下面是帮助类的代码:
使用方法十分简单,当我们需要在不同的页面播放同一声音的时候,将存储在独立空间的路径传入即可。
Updated 2013-02-28: 我们使用静态构造函数去初始化MediaElement 和PopUp
static Player() { popUp = new Popup(); _player = new MediaElement(); popUp.Child = _player; popUp.IsOpen = true; }
public static void Play(string filePath)
{ _player.Source = null; try { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Open, store)) { _player.SetSource(stream); _player.Volume = 1.0; _player.Play(); _player.MediaEnded += new RoutedEventHandler((sender, e) => { stream.Close(); stream.Dispose(); _player.Source = null; _player = null; }); _player.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>((s, args) => { stream.Close(); stream.Dispose(); _player.Source = null; _player = null; }); } } } catch (Exception) { _player.Source = null; _player = null; } }
1. 使用MediaElement
2. 使用SoundEffect
3. 使用后台播放
SoundEffect只能播放wav格式的文件;后台播放可以在程序退出后仍然继续播放;MediaElement适用于大多数情况,但不能实现后台播放,MediaElement依赖页面并且在页面中只有一个实例可以正常工作。
本文讲讲如何在应用中使用MediaElement作为程序全局播放器,并提供一个帮助类。
由于MediaElement依赖于UI,我们不能在ViewModel中创建一个MediaElement实例进行声音的播放,所以我们需要将MediaElement放置在一个UI容器中,下面是帮助类的代码:
public class GlobalPlayer { static Popup popUp; private static MediaElement _player; public static void Play(string filePath) { popUp = new Popup(); _player = new MediaElement(); _player.Source = null; popUp.Child = _player; popUp.IsOpen = true; try { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Open, store)) { _player.SetSource(stream); _player.Volume = 1.0; _player.Play(); _player.MediaEnded += new RoutedEventHandler((sender, e) => { stream.Close(); stream.Dispose(); _player.Source = null; _player = null; }); _player.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>((s, args) => { stream.Close(); stream.Dispose(); _player.Source = null; _player = null; }); } } } catch (Exception) { _player.Source = null; _player = null; } } public static void StopPlay() { if (_player != null && _player.CurrentState == MediaElementState.Playing) { try { _player.Source = null; _player.Stop(); } catch (Exception) { //throw; } } } }
使用方法十分简单,当我们需要在不同的页面播放同一声音的时候,将存储在独立空间的路径传入即可。
Updated 2013-02-28: 我们使用静态构造函数去初始化MediaElement 和PopUp
static Player() { popUp = new Popup(); _player = new MediaElement(); popUp.Child = _player; popUp.IsOpen = true; }
public static void Play(string filePath)
{ _player.Source = null; try { using (var store = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Open, store)) { _player.SetSource(stream); _player.Volume = 1.0; _player.Play(); _player.MediaEnded += new RoutedEventHandler((sender, e) => { stream.Close(); stream.Dispose(); _player.Source = null; _player = null; }); _player.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>((s, args) => { stream.Close(); stream.Dispose(); _player.Source = null; _player = null; }); } } } catch (Exception) { _player.Source = null; _player = null; } }
相关文章推荐
- Windows Phone实用开发技巧(40):使用NGif创建GIF图片
- Windows Phone 实用开发技巧(27):创建透明Tile
- Windows Phone 实用开发技巧(18):使用SystemTray显示全局消息提醒 推荐
- Windows Phone 实用开发技巧(18):使用SystemTray显示全局消息提醒
- Windows Phone 实用开发技巧(27):创建透明Tile
- Windows Phone实用开发技巧(33):不重启程序切换当前语言
- Windows Phone 实用开发技巧(22):使用日志记录当前信息与异常信息
- Windows Phone 实用开发技巧(3):输入框自动聚焦并打开SIP
- Windows Phone 实用开发技巧(5):让你的手机在运行应用程序运行时不自动锁屏
- Windows Phone 实用开发技巧1-30合集(电子书+源代码)
- Windows Phone 实用开发技巧1-20合集(电子书+源代码)
- Windows Phone 实用开发技巧(23):强制关闭SIP
- Windows Phone 实用开发技巧(10):Windows Phone 中处理图片的技巧 推荐
- Windows Phone 实用开发技巧(9):自定义Windows Phone 页面切换动画
- Windows Phone实用开发技巧(36):保存WebBrowser中的图片
- Windows Phone 实用开发技巧(5):让你的手机在运行应用程序运行时不自动锁屏 推荐
- Windows Phone 实用开发技巧(7):设置输入框光标的颜色
- Windows Phone 实用开发技巧(17):自定义应用程序的Tile
- Windows Phone实用开发技巧(1):保存图片及加载图片
- Windows Phone 实用开发技巧(14):输入框隐藏SIP事件