使用 Windows Phone 7 媒体播放器在页面之间共享媒体播放
我想要实现的目标:
- 我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终找到了一个简单但有效的解决方案:
http://blog.reis.se/post/Enable-background-audio-for-multiple-pages-in-Windows-Phone-7-e28093-Take-2.aspx
App.xaml 中:
在 App.xaml.cs:
在您的页面中:
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:
In App.xaml.cs:
In your page: