读书频道 > 网站 > 网页设计 > 深入浅出:Windows Phone 8应用开发
8.3 网络音频播放
13-01-22    奋斗的小年轻
收藏    我要投稿   
本书系统论述了Windows Phone 8操作系统的基本架构、开发方法与开发实践。全书内容共分三篇:开发基础篇、开发技术篇和开发实例篇。开发基础篇包括第1章~第3章,介绍了Windows Phone 8的技术架构及基本特性、...立即去当当网订购

在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所示。


 

点击复制链接 与好友分享!回本站首页
分享到: 更多
您对本文章有什么意见或着疑问吗?请到论坛讨论您的关注和建议是我们前行的参考和动力  
上一篇:1.3 功能
下一篇:1.5 小结
相关文章
图文推荐
JavaScript网页动画设
1.9 响应式
1.8 登陆页式
1.7 主题式
排行
热门
文章
下载
读书

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做最好的IT技术学习网站