您的位置:首页 > 其它

与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成

2014-02-22 19:41 399 查看
原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成[索引页]
[源码下载]

[align=center]与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成[/align]

作者:webabcd

介绍
与众不同 windows phone 7.5 (sdk 7.1) 之媒体

音频播放器

视频播放器

与 Windows Phone 的音乐和视频中心集成

示例
1、演示音频播放器
Audio.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Media.Audio"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">

<Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical">
<MediaElement x:Name="mediaElement" Source="Assets/SuperMario.mp3" AutoPlay="False" />

<Button x:Name="btnPlay" Content="播放" Click="btnPlay_Click" />
<Button x:Name="btnPause" Content="暂停" Click="btnPause_Click" />

<TextBlock x:Name="lblStatus" />
</StackPanel>
</Grid>

</phone:PhoneApplicationPage>


Audio.xaml.cs

/*
* MediaElement - 用于播放视频或音频,本地地址或远程地址均可
*     支持的编码格式参见:http://msdn.microsoft.com/en-us/library/ff462087(v=vs.92)
*
* MediaElement 的详细说明参见:http://www.cnblogs.com/webabcd/archive/2008/12/01/1344632.html
* Launcher 方式参见:http://www.cnblogs.com/webabcd/archive/2012/06/14/2548776.html 中的 MediaPlayerLauncher
*
* XNA 播放音频参见:http://www.cnblogs.com/webabcd/archive/2011/07/11/2102713.html
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Demo.Media
{
public partial class Audio : PhoneApplicationPage
{
public Audio()
{
InitializeComponent();

mediaElement.CurrentStateChanged += new RoutedEventHandler(mediaElement_CurrentStateChanged);
}

void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
{
// 显示 MediaElement 的当前状态
lblStatus.Text = mediaElement.CurrentState.ToString();
}

private void btnPlay_Click(object sender, RoutedEventArgs e)
{
// 播放
mediaElement.Play();
}

private void btnPause_Click(object sender, RoutedEventArgs e)
{
// 暂停
mediaElement.Pause();
}
}
}


2、演示视频播放器
Video.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Media.Video"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">

<Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical">
<MediaElement x:Name="mediaElement" Source="Assets/Demo.mp4" AutoPlay="False" />

<Button x:Name="btnPlay" Content="播放" Click="btnPlay_Click" />
<Button x:Name="btnPause" Content="暂停" Click="btnPause_Click" />

<TextBlock x:Name="lblStatus" />
</StackPanel>
</Grid>

</phone:PhoneApplicationPage>


Video.xaml.cs

/*
* MediaElement - 用于播放视频或音频,本地地址或远程地址均可
*     支持的编码格式参见:http://msdn.microsoft.com/en-us/library/ff462087(v=vs.92)
*
* MediaElement 的详细说明参见:http://www.cnblogs.com/webabcd/archive/2008/12/01/1344632.html
* Launcher 方式参见:http://www.cnblogs.com/webabcd/archive/2012/06/14/2548776.html 中的 MediaPlayerLauncher
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Demo.Media
{
public partial class Video : PhoneApplicationPage
{
public Video()
{
InitializeComponent();

mediaElement.CurrentStateChanged += new RoutedEventHandler(mediaElement_CurrentStateChanged);
}

void mediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
{
// 显示 MediaElement 的当前状态
lblStatus.Text = mediaElement.CurrentState.ToString();
}

private void btnPlay_Click(object sender, RoutedEventArgs e)
{
// 播放
mediaElement.Play();
}

private void btnPause_Click(object sender, RoutedEventArgs e)
{
// 暂停
mediaElement.Pause();
}
}
}


3、演示如何与 Windows Phone 的音乐和视频中心集成
IntegrateWithTheMusicAndVideoHub.xaml

<phone:PhoneApplicationPage
x:Class="Demo.Media.IntegrateWithTheMusicAndVideoHub"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">

<Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical">

<TextBlock TextWrapping="Wrap">
<Run>本 app 会出现在“音乐视频中心”中的“应用程序”下</Run>
<LineBreak />
<Run>操作完成后,请去“音乐视频中心”看效果</Run>
</TextBlock>

<Button x:Name="btnNow" Content="设置“音乐视频中心”中的“正在播放磁贴”" Click="btnNow_Click" />
<Button x:Name="btnRecent" Content="向“音乐视频中心”中的“历史记录”添加新的磁贴" Click="btnRecent_Click" />
<Button x:Name="btnAcquired" Content="向“音乐视频中心”中的“最新上市”添加新的磁贴" Click="btnAcquired_Click" />

</StackPanel>
</Grid>

</phone:PhoneApplicationPage>


IntegrateWithTheMusicAndVideoHub.xaml.cs

/*
* 本例演示如何将 app 集成进“音乐视频中心”
*
* MediaHistoryItem - 出现在“音乐视频中心”中的磁贴对象(包括“正在播放”,“历史记录”和“最新上市”)
*     ImageStream - 磁贴上需要显示的背景图片流
*     Title - 磁贴的标题
*     PlayerContext - key/value 对集合,用户点击“历史记录”或“最新上市”中的某个磁贴会进入到 app 的主页面,同时也会将对应的 key/value 数据一同带过去,参见 MainPage.xaml.cs
*
* MediaHistory - 管理 MediaHistoryItem 的类
*     Instance - 获得 MediaHistory 实例
*     NowPlaying - 指定“正在播放”对象,MediaHistoryItem 类型
*     WriteRecentPlay(MediaHistoryItem item) - 在“历史记录”中增加一个指定的 MediaHistoryItem 对象
*     WriteAcquiredItem(MediaHistoryItem item) - 在“最新上市”中增加一个指定的 MediaHistoryItem 对象
*
*
* 注意:
* 1、“正在播放”磁贴大小为 358 * 358,背景图不能大于 75 KB
* 2、“历史记录”和“最新上市”磁贴大小为 173 * 173
* 3、app 提交到商店审核时,如果认证程序检测到 app 调用了 MediaHistory 和 MediaHistoryItem,则此 app 就会出现在“音乐视频中心”的应用程序中
* 4、出于测试目的,如果想在 app 提交商店前使其出现在“音乐视频中心”的应用程序中的话,需要修改 manifest,在 <App /> 中增加 HubType="1"
* 5、MediaHistoryItem 的 PlayerContext 指定的 key/value 对集合是 MediaHistoryItem 的上下文数据,用户在“历史记录”或“最新上市”单击某个磁贴对象时,会跳转到 app 的主页面,同时将对应的 key/value 数据一同带过去,其可以在主页面通过 NavigationContext.QueryString[key] 获取到,参见 MainPage.xaml.cs
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Microsoft.Devices;
using System.IO;
using System.Windows.Resources;
using System.IO.IsolatedStorage;
using Microsoft.Phone.BackgroundAudio;

namespace Demo.Media
{
public partial class IntegrateWithTheMusicAndVideoHub : PhoneApplicationPage
{
public IntegrateWithTheMusicAndVideoHub()
{
InitializeComponent();

PlayAudio();
}

// 播放一个音频
private void PlayAudio()
{
// 由于播放本地音频时只能从独立存储中播放,所以此处把示例用音频文件从程序包中复制到独立存储
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!storage.FileExists("SuperMario.mp3"))
{
StreamResourceInfo resource = Application.GetResourceStream(new Uri("Assets/SuperMario.mp3", UriKind.Relative));

using (IsolatedStorageFileStream file = storage.CreateFile("SuperMario.mp3"))
{
int chunkSize = 4096;
byte[] bytes = new byte[chunkSize];
int byteCount;

while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
{
file.Write(bytes, 0, byteCount);
}
}
}
}

BackgroundAudioPlayer.Instance.Play();
}

private void btnNow_Click(object sender, RoutedEventArgs e)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri("Assets/TileBackgroundRed.png", UriKind.Relative));

MediaHistoryItem mediaHistoryItem = new MediaHistoryItem();
mediaHistoryItem.ImageStream = sri.Stream;
mediaHistoryItem.Title = "正在播放";
mediaHistoryItem.PlayerContext.Add("keyNow", "正在播放的音乐");
MediaHistory.Instance.NowPlaying = mediaHistoryItem;
}

private void btnRecent_Click(object sender, RoutedEventArgs e)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri("Assets/TileBackgroundGreen.png", UriKind.Relative));

MediaHistoryItem mediaHistoryItem = new MediaHistoryItem();
mediaHistoryItem.ImageStream = sri.Stream;
mediaHistoryItem.Title = "最近播放";
mediaHistoryItem.PlayerContext.Add("keyRecent", "最近播放的音乐");
MediaHistory.Instance.WriteRecentPlay(mediaHistoryItem);
}

private void btnAcquired_Click(object sender, RoutedEventArgs e)
{
StreamResourceInfo sri = Application.GetResourceStream(new Uri("Assets/TileBackgroundBlue.png", UriKind.Relative));

MediaHistoryItem mediaHistoryItem = new MediaHistoryItem();
mediaHistoryItem.ImageStream = sri.Stream;
mediaHistoryItem.Title = "最新上市";
mediaHistoryItem.PlayerContext.Add("keyAcquired", "最新上市的音乐");
MediaHistory.Instance.WriteAcquiredItem(mediaHistoryItem);
}
}
}


OK
[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐