在Windows Phone应用程序中,播放网络音频和播放本地音频的处理方式差不多,只不过播放本地音频的时候使用的是音频本地相对路径,而播放网络音频的时候使用的是网络音频地址。需要注意的是,播放网络音频需要在手机联网的状态下才能够成功地播放,否则会抛出异常,所以在播放网络音频的时候需要检查手机的联网状态或者捕获该异常进行处理。
下面给出播放网络音乐的示例: 播放网络上的mp3音乐,并将播放的记录保存到程序的独立存储里面。
代码清单8-2:播放网络音乐(源代码:第8章\Examples_8_2)
MainPage.xaml文件主要代码
------------------------------------------------------------------------------------------------------------------
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Height="200" HorizontalAlignment="Left" Margin="6,79,0,0" Name="listBox1" VerticalAlignment="Top" Width="444" />
<TextBlock FontSize="30" Height="43" HorizontalAlignment="Left" Margin="12,285,0,0" Name="textBlock2" Text="请输入mp3的网络地址:" VerticalAlignment="Top" Width="374" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="0,334,0,0" Name="mp3Uri" VerticalAlignment="Top" Width="460" Text="http://localhost/2.mp3" />
<Button Content="播放" Height="72" HorizontalAlignment="Left" Margin="0,428,0,0" Name="play" VerticalAlignment="Top" Width="220" Click="play_Click" />
<Button Content="停止" Height="72" HorizontalAlignment="Left" Margin="224,428,0,0" Name="stop" VerticalAlignment="Top" Width="220" Click="stop_Click" />
<MediaElement Height="43" HorizontalAlignment="Left" Margin="6,555,0,0" Name="media" VerticalAlignment="Top" Width="438" />
<TextBlock Height="54" HorizontalAlignment="Left" Margin="12,23,0,0" Name="textBlock1" FontSize="30" Text="播放的历史记录:" VerticalAlignment="Top" Width="317" />
</Grid>
MainPage.xaml.cs文件代码
------------------------------------------------------------------------------------------------------------------
using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
namespace OnlineMp3
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
RefreshIsoFiles();
}
//刷新播放的历史记录
private void RefreshIsoFiles()
{
string[] fileList;
//读取独立存储中的文件名
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
fileList = store.GetFileNames();
}
//将文件名绑定到ListBox控件上
listBox1.ItemsSource = fileList;
}
//保存播放的历史记录
private void savehistory()
{
//使用音频的url地址作为文件名
string fileName = System.IO.Path.GetFileName(mp3Uri.Text);
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.FileExists(fileName))
{
//创建独立存储文件
IsolatedStorageFileStream file = store.CreateFile(fileName);
}
}
//刷新播放的历史记录
RefreshIsoFiles();
}
//播放音乐
private void play_Click(object sender, RoutedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(mp3Uri.Text))
{
media.Source = new Uri(mp3Uri.Text, UriKind.Absolute);
media.Play();
savehistory();
}
else
{
MessageBox.Show("请输入mp3的网络地址!");
}
}
catch (Exception)
{
//捕获播放失败的异常
MessageBox.Show("无法播放!");
}
}
//停止播放
private void stop_Click(object sender, RoutedEventArgs e)
{
media.Stop();
}
}
}
{
if (!store.FileExists(fileName))
{
//创建独立存储文件
IsolatedStorageFileStream file = store.CreateFile(fileName);
}
}
//刷新播放的历史记录
RefreshIsoFiles();
}
//播放音乐
private void play_Click(object sender, RoutedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(mp3Uri.Text))
{
media.Source = new Uri(mp3Uri.Text, UriKind.Absolute);
media.Play();
savehistory();
}
else
{
MessageBox.Show("请输入mp3的网络地址!");
}
}
catch (Exception)
{
//捕获播放失败的异常
MessageBox.Show("无法播放!");
}
}
//停止播放
private void stop_Click(object sender, RoutedEventArgs e)
{
media.Stop();
}
}
}
程序运行的效果如图8.2所示。