博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wp7音乐播放器
阅读量:5150 次
发布时间:2019-06-13

本文共 5079 字,大约阅读时间需要 16 分钟。

前台代码:

    <!--LayoutRoot 是包含所有页面内容的根网格-->

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock FontSize="25" x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock FontSize="60" x:Name="PageTitle" Text="播放MediaPlayer" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Width="486" />
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Image Height="216" HorizontalAlignment="Left" Margin="35,48,0,0" Name="cover" Stretch="Fill" VerticalAlignment="Top" Width="401" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="1,298,0,0" Name="SongName" Text="" VerticalAlignment="Top" Width="460" />
            <Button Content="播放" Height="72" HorizontalAlignment="Left" Margin="6,409,0,0" Name="btn_play_music" VerticalAlignment="Top" Width="444" Click="btn_play_music_Click_1" />
            <Button Content="停止" Height="72" HorizontalAlignment="Left" Margin="12,503,0,0" Name="btn_stop" VerticalAlignment="Top" Width="438" Click="btn_stop_Click_1" />

        </Grid>

    </Grid>

 

后台代码:

 

namespace yinyuebofangqi

{
    public partial class MainPage : PhoneApplicationPage
    {
        #region 成员声明
        Song _playSong; //声明一个Song 类型的变量
        bool _historyItemLaunch;
        const String _playSongKey = "playSong";
        #endregion

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            _playSong = null;
            _historyItemLaunch = false;

            //声明一个Timer 使用线程运行 XNA internals 来自于微软XNA框架

            DispatcherTimer dt = new DispatcherTimer();
            dt.Interval = TimeSpan.FromMilliseconds(33);
            //使用委托调用逐帧调用  FrameworkDispatcher.Update();
            dt.Tick += delegate
            {
                try
                {
                    // 这个函数作用是更新各种XNA组件状态,并触发相应的事件,这个是必须的,否则会报异常
                    FrameworkDispatcher.Update();

                }

                catch
                {

                    throw;

                }
            };
            dt.Start();

            MediaPlayer.MediaStateChanged += new EventHandler<EventArgs>(MediaPlayer_MediaStateChanged);

        }

        /// <summary>

        /// 当页面进入时调用
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            MediaLibrary library = new MediaLibrary();
            if (NavigationContext.QueryString.ContainsKey(_playSongKey))
            {
                String songToPlay = NavigationContext.QueryString[_playSongKey];
                foreach (Song song in library.Songs)
                {
                    if (0 == String.Compare(songToPlay, song.Name))
                    {
                        _playSong = song;
                    }
                }
                _historyItemLaunch = true;
            }
            else if (MediaPlayer.State == MediaState.Playing)
            {
                _playSong = MediaPlayer.Queue.ActiveSong;
            }
            else
            {
                Random rd = new Random();
                if (library.Songs.Count > 0)
                {
                    _playSong = library.Songs[rd.Next(library.Songs.Count)];
                }
                else
                {
                    SongName.Text = "no songs in library";
                    btn_play_music.IsEnabled = false;
                }
            }

            base.OnNavigatedTo(e);
        }

        void MediaPlayer_MediaStateChanged(object sender, EventArgs e)

        {
            switch (MediaPlayer.State)
            {

                case MediaState.Playing:

                    btn_play_music.IsEnabled = false;
                    btn_stop.IsEnabled = true;
                    break;
                case MediaState.Paused:
                case MediaState.Stopped:
                    btn_play_music.IsEnabled = true;
                    btn_stop.IsEnabled = false;
                    break;
            }
        }

        /// <summary>

        /// 为主窗体添加UI加载完成后的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            PopulateSongMetaData();
            setInitialButtonState();
            if (_historyItemLaunch)
            {
                playSong();
            }
        }

        /// <summary>
        /// 开始播放音乐
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btn_play_music_Click(object sender, RoutedEventArgs e)
        //{
        //    playSong();
        //}

        / <summary>

        / 停止播放音乐
        / </summary>
        / <param name="sender"></param>
        / <param name="e"></param>
        //private void btn_stop_Click(object sender, RoutedEventArgs e)
        //{
        //    stopSong();
        //}

        /// <summary>

        /// 播放
        /// </summary>
        private void playSong()
        {
            if (_playSong != null)
            {
                MediaPlayer.Play(_playSong);
            }
        }
        /// <summary>
        /// 停止
        /// </summary>
        private void stopSong()
        {
            if (MediaState.Playing == MediaPlayer.State)
            {
                MediaPlayer.Stop();
            }
        }

        /// <summary>

        /// 使用图片读取图片封面
        /// </summary>
        private void PopulateSongMetaData()
        {
            if (_playSong != null)
            {
                SongName.Text = _playSong.Name;
                Stream albumArtStream = _playSong.Album.GetAlbumArt();
                if (albumArtStream == null)
                {
                    StreamResourceInfo albumArtPlaceholder = Application.GetResourceStream(new Uri("ApplicationIcon.png", UriKind.Relative));
                    albumArtStream = albumArtPlaceholder.Stream;
                }
                BitmapImage albumArtImage = new BitmapImage();
                albumArtImage.SetSource(albumArtStream);
                cover.Source = albumArtImage;
            }
        }

        /// <summary>

        /// 初始化按钮
        /// </summary>
        private void setInitialButtonState()
        {
            switch (MediaPlayer.State)
            {

                case MediaState.Playing:

                    btn_play_music.IsEnabled = false;
                    btn_stop.IsEnabled = true;
                    break;
                case MediaState.Paused:
                case MediaState.Stopped:
                    btn_play_music.IsEnabled = true;
                    btn_stop.IsEnabled = false;
                    break;
            }
        }

        private void btn_play_music_Click_1(object sender, RoutedEventArgs e)

        {
            playSong();
        }

        private void btn_stop_Click_1(object sender, RoutedEventArgs e)

        {
            stopSong();
        }

 

    }

}

转载于:https://www.cnblogs.com/qiqiBoKe/archive/2013/06/14/3134973.html

你可能感兴趣的文章
大牛讲解信号与系统以及数字信号处理
查看>>
基于dsp_builder的算法在FPGA上的实现
查看>>
BZOJ2154 Crash的数字表格
查看>>
Python学习之路1 - 基础入门
查看>>
如何用windbg分析内存泄露
查看>>
Vue系列之 => 列表动画
查看>>
定位、浮动与清除
查看>>
sqlserver----记录转载(行转列)、列转行、pivot、unpivot
查看>>
细节!重点!易错点!--面试java基础篇(一)
查看>>
ios KeyChain中保存数据
查看>>
解决node-sass安装不了的问题
查看>>
javaScript几个操作数组增减元素的函数
查看>>
PostgreSQL在何处处理 sql查询之三十九
查看>>
安装python
查看>>
html的框架标签
查看>>
从VB6迁移到VB.NET及相关问题
查看>>
Joomla3.1.1在64位win7下安装
查看>>
[转] Latex设置字体大小,加粗,加下划线,变斜体
查看>>
React子组件怎么改变父组件的state
查看>>
Metropolis-Hastings算法
查看>>