【问题标题】:Sharing Media Playback Between Pages with Windows Phone 7 Media Player使用 Windows Phone 7 媒体播放器在页面之间共享媒体播放
【发布时间】:2011-02-20 19:02:11
【问题描述】:

我想要达到的目标:

  • 我想在 WP7 应用程序中从 mp3 和/或 aac HTTP 流启动音频播放
  • 我想从特定的“PhoneApplicationPage”实例开始播放,但仍然允许导航到其他页面,同时保持播放而没有任何中断 - 即我希望播放是“应用程序范围”
  • 我希望能够在我的媒体中“寻找”
  • 我在手机锁定时继续播放

我尝试过的:

媒体元素:

  • 如果 MediaElement 不属于某个页面,则在调用 Play() 时不会产生声音,尽管没有引发异常。
  • 遵循“http://blog.jayway.com/2010/10/04/enable-background-audio-for-multiple-pages-in-windows-phone-7/”后,页面转换之间的播放仍会重置
  • 这似乎也是一种相当老套的做事方式......

Microsoft.Xna.Framework.MediaPlayer:

  • 可以,但是“MediaPlayer.PlayPosition”是只读的,没有seek方法。
  • 参见帖子:'http://forums.create.msdn.com/forums/t/17318.aspx' - 显然这是由于 Xna 的 XBox 限制而设计的 (?!)

微软 Silverlight 媒体框架:

  • http://smf.codeplex.com/
  • 我最喜欢的选项,因为它看起来很全面
  • 从以下位置下载了“Silverlight Media Framework 2.3,WP7 特定”程序集: http://smf.codeplex.com/releases/view/57991#DownloadId=190196
  • 我知道这很老套,但为了让某些东西正常工作,在下面的代码中,“SMFPlayer”是静态的,并添加到每个页面的导航布局中。
  • 如果页面不拥有“SMFPlayer”,则在调用 Play() 时不会产生声音,尽管没有引发异常。
  • 在页面转换之间仍会重置播放...
  • 代码:
using System;
using System.Diagnostics;
using Microsoft.Phone.Controls;
using Microsoft.SilverlightMediaFramework.Core;
using Microsoft.SilverlightMediaFramework.Core.Media;
using Microsoft.SilverlightMediaFramework.Plugins.Primitives;

namespace WindowsPhoneApplication1
{
    public partial class MainPage : PhoneApplicationPage
    {
        public static readonly SMFPlayer Player = new SMFPlayer();

        static MainPage()
        {
            Player.VolumeLevel = 1.0f;
            Player.Playlist.Add(new PlaylistItem {MediaSource = new Uri("http://smf.vertigo.com/videos/wildlife.wmv", UriKind.Absolute)});

            Player.LogLevel = LogLevel.All;
            Player.LogEntryReceived += PlayerLogEntryReceived;
        }

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            LayoutRoot.Children.Add(Player);
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);
            LayoutRoot.Children.Remove(Player);
        }

        private static void PlayerLogEntryReceived(object sender, CustomEventArgs<LogEntry> e)
        {
            Debug.WriteLine(e.Value.Severity + e.Value.Message + e.Value.Type);
        }

        private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}

有谁知道我怎样才能满足我的要求? 示例代码?

从架构的角度来看,我真正想要的是一种媒体服务,我可以将流式 URL 发送到,而无需关心当前显示的是哪个页面。

【问题讨论】:

    标签: c# audio windows-phone-7 audio-streaming playback


    【解决方案1】:

    我最终找到了一个简单但有效的解决方案: http://blog.reis.se/post/Enable-background-audio-for-multiple-pages-in-Windows-Phone-7-e28093-Take-2.aspx

    在 App.xaml 中:

    <APPLICATION.RESOURCES>
        <MEDIAELEMENT x:key="GlobalMedia"></MEDIAELEMENT>
    </APPLICATION.RESOURCES>
    

    在 App.xaml.cs 中:

    public static MediaElement GlobalMediaElement
    {
      get { return Current.Resources["GlobalMedia"] as MediaElement; }
    }
    

    在您的页面中:

    public partial class MyPage : PhoneApplicationPage
    {
        MediaElement MEAudio;
    
        public MainPage()
        {
            InitializeComponent();    
            MEAudio = App.GlobalMediaElement;
        }
    
        private void OnSomeEvent(object sender, RoutedEventArgs e)
        {
            MEAudio.xxxxx();
    

    【讨论】:

    • 我发现 Silverlight 上的普通 MediaElement 也会发生同样的行为。如果未将其添加到可视树中,则不会播放音频。似乎添加到应用程序资源中也可能是我的情况的可行解决方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多