Chủ Nhật, 30 tháng 11, 2014

Chơi nhạc remote bằng BackgroundWorker và mediaElement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Threading;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
namespace PhoneApp9
{
    public partial class Page3 : PhoneApplicationPage
    {
        private BackgroundWorker bw = new BackgroundWorker();
        double Duration;
        public Page3()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(PhoneApplicationPage_Loaded);
            mainBar.Minimum = 0;
            mainBar.Maximum = 100;
            
            mediaPlayer.BufferingProgressChanged += new RoutedEventHandler(mediaPlayer_BufferingProgressChanged);
            mediaPlayer.CurrentStateChanged += new RoutedEventHandler(mediaPlayer_CurrentStateChanged);
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = false;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            
        }


        private void timelineSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            mediaPlayer.Position = TimeSpan.FromSeconds(timelineSlider.Value);
        }
        private void mediaPlayer_MediaOpened(object sender, RoutedEventArgs e)
        {
            TimeSpan ts = mediaPlayer.NaturalDuration.TimeSpan;
            timelineSlider.Maximum = ts.TotalSeconds;
            timelineSlider.SmallChange = 1;
            timelineSlider.LargeChange = Math.Min(10, ts.Seconds / 10);
        }
        private void seekBar_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            
        }

        void mediaPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            if (mediaPlayer.CurrentState == MediaElementState.Playing)
            {
                Duration = mediaPlayer.NaturalDuration.TimeSpan.TotalSeconds;
                if (!bw.IsBusy)
                {
                    bw.RunWorkerAsync();
                }
            }
        }
        void mediaPlayer_BufferingProgressChanged(object sender, RoutedEventArgs e)
        {
            MediaElement m = (MediaElement)sender;
            System.Diagnostics.Debug.WriteLine("Progress change:" + m.BufferingProgress);
            if (m.BufferingProgress == 1)
                System.Diagnostics.Debug.WriteLine(m.NaturalDuration);
        }
        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            double Progress = mediaPlayer.Position.TotalSeconds * 100 / Duration;
            mainBar.Value = Progress;
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            while (true)
            {
                Thread.Sleep(1);
                worker.ReportProgress(1);
            }
            
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            mediaPlayer.Source = new Uri("http://k003.kiwi6.com/hotlink/00wm0efwme/Anh_Nho_Em_Nguoi_Yeu_Cu_-_Minh_Vuong_M4U.mp3", UriKind.Absolute);
            mediaPlayer.Play(); 
            
        }

      
    }
}


File giao diện:

<phone:PhoneApplicationPage
    x:Class="PhoneApp9.Page3"
    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"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Slider Name="timelineSlider" Margin="5,5,5,5" ValueChanged="timelineSlider_ValueChanged"  Grid.Row="0" />
            <MediaElement Name="mediaPlayer" Grid.Row="1" MediaOpened="mediaPlayer_MediaOpened"></MediaElement>
            <ProgressBar x:Name="mainBar" Height="20" Width="400" Grid.Row="2"></ProgressBar>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>






Không có nhận xét nào:

Đăng nhận xét