Windows Phone實用開發技巧(37):創建一個全局播放器


在做windows phone 開發的時候,有時候我們需要在某些頁面中進行聲音的播放。而播放的方式也有多種:

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;
            
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM