使用 Windows Phone 7 媒体播放器在页面之间共享媒体播放

发布于 2024-10-18 10:22:44 字数 2889 浏览 0 评论 0原文

我想要实现的目标:

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

我尝试过的内容:

MediaElement:

  • 如果 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”是只读的,并且没有搜索方法。
  • 请参阅帖子:“http://forums.create.msdn.com/forums/t/17318.aspx” - 显然这是由于 XBox 与 Xna(?!)

Microsoft Silverlight 媒体框架的限制而设计的:

  • http://smf .codeplex.com/
  • 我最喜欢的选项,因为它看起来非常全面
  • 从以下位置下载了“Silverlight Media Framework 2.3,WP7 特定”程序集: http://smf.codeplex.com/releases/view/57991#DownloadId=190196
  • 我知道这很hacky,但为了让某些东西正常工作,在下面的代码中,“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 发送到该服务,而无需关心当前显示的是哪个页面。

What I want to achieve:

  • I want to initiate audio playback from an mp3 and/or aac HTTP stream in a WP7 application
  • I want to initiate playback from a specific 'PhoneApplicationPage' instance, but still allow navigation to other pages whilst maintaining playback without any interuption - i.e. I want playback to be 'application-scope'
  • I want to be able to 'seek' within my media
  • I playback to continue whilst the phone is locked

What I have tried:

MediaElement:

  • If the MediaElement is not owned by a page, no sound is produced when Play() is called, despite no exceptions being thrown.
  • After following 'http://blog.jayway.com/2010/10/04/enable-background-audio-for-multiple-pages-in-windows-phone-7/', playback still resets between page transitions
  • It also seems like a quite a hacky way of doing things...

Microsoft.Xna.Framework.MediaPlayer:

  • Works, but "MediaPlayer.PlayPosition" is read-only, and there is no seek method.
  • See post: 'http://forums.create.msdn.com/forums/t/17318.aspx' - Apparently this is by design due to XBox constraints with Xna (?!)

Microsoft Silverlight Media Framework:

  • http://smf.codeplex.com/
  • My favourite option, as it seems very comprehensive
  • Downloaded 'Silverlight Media Framework 2.3, WP7 specific' assemblies from:
    http://smf.codeplex.com/releases/view/57991#DownloadId=190196
  • I know this is hacky, but to get something working, in the code below, the 'SMFPlayer' is static, and added to each page's layout on navigation.
  • If the 'SMFPlayer' is not owned by a page, no sound is produced when Play() is called, despite no exceptions being thrown.
  • Playback still resets between page transitions...
  • Code:
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));
        }
    }
}

Does anyone have any idea how I can satisfy my requirements?
Example code?

From an architectural point of view, what I really want is a Media Service which i can send streaming URLs to without caring about which page is currently shown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぇ气 2024-10-25 10:22:44

我最终找到了一个简单但有效的解决方案:
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();

I eventually found a simple, but effective solution:
http://blog.reis.se/post/Enable-background-audio-for-multiple-pages-in-Windows-Phone-7-e28093-Take-2.aspx

In App.xaml:

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

In App.xaml.cs:

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

In your page:

public partial class MyPage : PhoneApplicationPage
{
    MediaElement MEAudio;

    public MainPage()
    {
        InitializeComponent();    
        MEAudio = App.GlobalMediaElement;
    }

    private void OnSomeEvent(object sender, RoutedEventArgs e)
    {
        MEAudio.xxxxx();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文