微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器

[索引页]
[源码下载]


稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement,开发一个简易版的全功能播放器


作者:webabcd


介绍
Silverlight 2.0 详解MediaElement:开发一个简易版的全功能播放器
    MediaOpened - 当媒体被成功地打开时所触发的事件
    MediaFailed - 当媒体未能被成功地打开时所触发的事件
    CurrentStateChanged - 播放状态(CurrentState)发生改变时所触发的事件
    DownloadProgressChanged - 下载进度(DownloadProgress)发生变化时所触发的事件
    MediaEnded - 当媒体播放到末尾时所触发的事件
    BufferingProgressChanged - 缓冲进度(BufferingProgress)发生变化时所触发的事件
    Source - 需要播放的媒体地址
    Stretch - 拉伸值
    Autoplay - 是否自动播放媒体
    CurrentState - 播放状态
    Position - 媒体的位置
    DroppedFramesPerSecond - 媒体每秒正在丢弃的帧数
    BufferingProgress - 缓冲进度
    DownloadProgress - 下载进度
    NaturalDuration - 媒体文件的时长
    Volume - 音量大小
    Balance - 音量平衡
    BufferingTime - 需要缓冲的时间的长度
    CurrentState - 播放状态
    IsMuted - 是否静音
    Play() - 播放媒体
    Pause() - 暂停媒体的播放
    Stop() - 停止媒体的播放


在线DEMO
http://www.voidcn.com/article/p-ounmxjds-tq.html

示例
VideoPlayer.xaml
<UserControl x:Class="Silverlight20.Video.VideoPlayer"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel HorizontalAlignment="Left">
        
                <!--
                Source - 需要播放的媒体地址
                Stretch - 拉伸值 [System.Windows.Media.Stretch 枚举]。参见:本Demo的Shape/Shape.xaml
                Autoplay - 是否自动播放媒体
                -->
                <MediaElement x:Name="mediaElement"
                                Width="400" Height="200"
                                Source="http://download.microsoft.com/download/2/0/5/205d8c39-3d55-4032-8195-7b0e6eda4cb6/WinVideo-SL-InstallExperience.wmv"    
                                Stretch="Fill"    
                                Autoplay="False">
                </MediaElement>

                <Button x:Name="play" Content="播放" Margin="5" Click="play_Click" />
                <Button x:Name="pause" Content="暂停" Margin="5" Click="pause_Click" />
                <Button x:Name="stop" Content="停止" Margin="5" Click="stop_Click" />
                <Button x:Name="mute" Content="静音" Margin="5" Click="mute_Click" />

                <Slider x:Name="playSlider" Minimum="0" Maximum="1" Margin="5" ToolTipService.ToolTip="播放进度" ValueChanged="playSlider_ValueChanged" />
                <Slider x:Name="volumeSlider" Minimum="0" Maximum="1" Margin="5" ToolTipService.ToolTip="音量大小" ValueChanged="volumeSlider_ValueChanged" />
                <Slider x:Name="balanceSlider" Minimum="-1" Maximum="1" Margin="5" ToolTipService.ToolTip="音量平衡" ValueChanged="balanceSlider_ValueChanged" />

                <TextBlock x:Name="lblPlayTime" Margin="5" />
                <TextBlock x:Name="lblVolume" Margin="5" />
                <TextBlock x:Name="lblBalance" Margin="5" />

                <TextBlock x:Name="lblDownloadProgress" Margin="5" />
                <TextBlock x:Name="lblBufferingProgress" Margin="5" />
                <TextBlock x:Name="lblDroppedFramesPerSecond" Margin="5" />
                <TextBlock x:Name="lblState" Margin="5" />

                <TextBlock x:Name="lblWidth" Margin="5" />
                <TextBlock x:Name="lblHeight" Margin="5" />
                <TextBlock x:Name="lblTotalTime" Margin="5" />
                <TextBlock x:Name="lblBufferingTime" Margin="5" />

        </StackPanel>
</UserControl>
 
VideoPlayer.xaml.cs

@H_404_105@using System;

@H_404_105@using System.Collections.Generic;

@H_404_105@using System.Linq;

@H_404_105@using System.Net;

@H_404_105@using System.Windows;

@H_404_105@using System.Windows.Controls;

@H_404_105@using System.Windows.Documents;

@H_404_105@using System.Windows.Input;

@H_404_105@using System.Windows.Media;

@H_404_105@using System.Windows.Media.Animation;

@H_404_105@using System.Windows.Shapes;


@H_404_105@using System.Windows.Threading;


@H_404_105@namespace Silverlight20.Video

{

         @H_404_105@public partial @H_404_105@class VideoPlayer : UserControl

        {

                 // 媒体的时长

                 @H_404_105@private TimeSpan _duration;


                 @H_404_105@private dispatcherTimer _timer = @H_404_105@new dispatcherTimer();


                 @H_404_105@public VideoPlayer()

                {

                        InitializeComponent();


                         @H_404_105@this.Loaded += @H_404_105@new RoutedEventHandler(VideoPlayer_Loaded);


                         /*

                         * MediaOpened - 当媒体被成功地打开时所触发的事件

                         * MediaFailed - 当媒体未能被成功地打开时所触发的事件

                         * CurrentStateChanged - 播放状态(CurrentState)发生改变时所触发的事件

                         * DownloadProgressChanged - 下载进度(DownloadProgress)发生变化时所触发的事件(当下载增加量大于等于 0.05 或下载进度增加到 1 时会触发此事件)

                         * MediaEnded - 当媒体播放到末尾时所触发的事件

                         * BufferingProgressChanged - 缓冲进度(BufferingProgress)发生变化时所触发的事件(当缓冲增加量大于等于 0.05 或缓冲进度增加到 1 时会触发此事件)

                         */


                        mediaElement.MediaOpened += @H_404_105@new RoutedEventHandler(mediaElement_MediaOpened);

                        mediaElement.CurrentStateChanged += @H_404_105@new RoutedEventHandler(mediaElement_CurrentStateChanged);

                        mediaElement.DownloadProgressChanged += @H_404_105@new RoutedEventHandler(mediaElement_DownloadProgressChanged);

                        mediaElement.MediaEnded += @H_404_105@new RoutedEventHandler(mediaElement_MediaEnded);

                        mediaElement.BufferingProgressChanged += @H_404_105@new RoutedEventHandler(mediaElement_BufferingProgressChanged);

                }


                 @H_404_105@void VideoPlayer_Loaded( @H_404_105@object sender,RoutedEventArgs e)

                {

                         // 每 500 毫秒调用一次指定的方法

                        _timer.Interval = TimeSpan.FromMilliseconds(500);

                        _timer.Tick += @H_404_105@new EventHandler(_timer_Tick);

                        _timer.Start();

                }


                 @H_404_105@void _timer_Tick( @H_404_105@object sender,EventArgs e)

                {

                         // CurrentState - 播放状态 [System.Windows.Media.MediaElementState枚举]

                         // Position - 媒体的位置(单位:秒)

                         @H_404_105@if (mediaElement.CurrentState == MediaElementState.Playing)

                        {

                                lblPlayTime.Text = @H_404_105@string.Format(

                                         "{0}{1:00}:{2:00}:{3:00}",

                                         "播放进度:",

                                        mediaElement.Position.Hours,

                                        mediaElement.Position.Minutes,

                                        mediaElement.Position.Seconds);

                        }


                         // DroppedFramesPerSecond - 媒体每秒正在丢弃的帧数

                        lblDroppedFramesPerSecond.Text = "每秒正在丢弃的帧数:" + mediaElement.DroppedFramesPerSecond.ToString();

                }


                 @H_404_105@void mediaElement_BufferingProgressChanged( @H_404_105@object sender,RoutedEventArgs e)

                {

                         // BufferingProgress - 缓冲进度(0 - 1 之间)

                        lblBufferingProgress.Text = @H_404_105@string.Format(

                                 "缓冲进度:{0:##%}",

                                mediaElement.BufferingProgress);

                }


                 @H_404_105@void mediaElement_MediaEnded( @H_404_105@object sender,RoutedEventArgs e)

                {

                        mediaElement.Stop();

                }


                 @H_404_105@void mediaElement_DownloadProgressChanged( @H_404_105@object sender,RoutedEventArgs e)

                {

                         // DownloadProgress - 下载进度(0 - 1 之间)

                        lblDownloadProgress.Text = @H_404_105@string.Format(

                                 "下载进度:{0:##%}",

                                mediaElement.DownloadProgress);

                }

                 @H_404_105@private @H_404_105@void mediaElement_MediaOpened( @H_404_105@object sender,RoutedEventArgs e)

                {

                         /*

                         * NaturalVideoWidth - 媒体文件的宽

                         * NaturalVideoHeight - 媒体文件的高

                         * HasTimeSpan - 是否可取得媒体文件的时长

                         * NaturalDuration - 媒体文件的时长

                         * Volume - 音量大小(0 - 1 之间)

                         * Balance - 音量平衡(-1 - 1 之间)

                         * BufferingTime - 需要缓冲的时间的长度

                         */


                        lblWidth.Text = "媒体文件的宽:" + mediaElement.NaturalVideoWidth.ToString();

                        lblHeight.Text = "媒体文件的高:" + mediaElement.NaturalVideoHeight.ToString();


                        _duration = mediaElement.NaturalDuration.HasTimeSpan ? mediaElement.NaturalDuration.TimeSpan : TimeSpan.FromMilliseconds(0);


                        lblTotalTime.Text = @H_404_105@string.Format(

                                 "{0}{1:00}:{2:00}:{3:00}","时长:",

                                _duration.Hours,

                                _duration.Minutes,

                                _duration.Seconds);


                        mediaElement.Volume = 0.8;

                        volumeSlider.Value = 0.8;

                        lblVolume.Text = "音量大小:80%";


                        mediaElement.Balance = 0;

                        balanceSlider.Value = 0;

                        lblBalance.Text = "音量平衡:0%";


                        mediaElement.BufferingTime = TimeSpan.FromSeconds(30);

                        lblBufferingTime.Text = "缓冲长度:30秒";

                }


                 @H_404_105@private @H_404_105@void mediaElement_CurrentStateChanged( @H_404_105@object sender,RoutedEventArgs e)

                {

                         /*

                         * CurrentState - 播放状态 [System.Windows.Media.MediaElementState枚举]

                         *         MediaElementState.Closed - 无可用媒体

                         *         MediaElementState.opening - 尝试打开媒体(此时Play(),Pause(),Stop()命令会被排进队列,等到媒体被成功打开后再依次执行)

                         *         MediaElementState.Buffering - 缓冲中

                         *         MediaElementState.Playing - 播放中

                         *         MediaElementState.Paused - 被暂停(显示当前帧)

                         *         MediaElementState.Stopped - 被停止(显示第一帧)

                         */


                        lblState.Text = "播放状态:" + mediaElement.CurrentState.ToString();

                }


                 @H_404_105@private @H_404_105@void play_Click( @H_404_105@object sender,RoutedEventArgs e)

                {

                         // Play() - 播放媒体(在当前 Position 处播放)

                        mediaElement.Play();

                }


                 @H_404_105@private @H_404_105@void pause_Click( @H_404_105@object sender,RoutedEventArgs e)

                {

                         // CanPause - 媒体是否可暂停

                         // Pause() - 暂停媒体的播放

                         @H_404_105@if (mediaElement.CanPause)

                                mediaElement.Pause();

                }


                 @H_404_105@private @H_404_105@void stop_Click( @H_404_105@object sender,RoutedEventArgs e)

                {

                         // Stop() - 停止媒体的播放

                        mediaElement.Stop();

                }


                 @H_404_105@void mute_Click( @H_404_105@object sender,RoutedEventArgs e)

                {

                         // IsMuted - 是否静音

                         @H_404_105@if (mediaElement.IsMuted == @H_404_105@true)

                        {

                                mute.Content = "静音";

                                mediaElement.IsMuted = @H_404_105@false;

                        }

                         @H_404_105@else

                        {

                                mute.Content = "有声";

                                mediaElement.IsMuted = @H_404_105@true;

                        }

                }


                 @H_404_105@private @H_404_105@void playSlider_ValueChanged( @H_404_105@object sender,RoutedPropertyChangedEventArgs< @H_404_105@double> e)

                {

                         // CanSeek - 是否可以通过设置 Position 来重新定位媒体

                         // Position - 媒体的位置(单位:秒)

                         @H_404_105@if (mediaElement.CanSeek)

                        {

                                mediaElement.Pause();

                                mediaElement.Position = TimeSpan.FromSeconds(_duration.TotalSeconds * playSlider.Value);

                                mediaElement.Play();

                        }

                }


                 @H_404_105@private @H_404_105@void volumeSlider_ValueChanged( @H_404_105@object sender,RoutedPropertyChangedEventArgs< @H_404_105@double> e)

                {

                         // Volume - 音量大小(0 - 1 之间)

                        mediaElement.Volume = volumeSlider.Value;

                        lblVolume.Text = @H_404_105@string.Format(

                                 "音量大小:{0:##%}",

                                volumeSlider.Value);

                }


                 @H_404_105@private @H_404_105@void balanceSlider_ValueChanged( @H_404_105@object sender,RoutedPropertyChangedEventArgs< @H_404_105@double> e)

                {

                         // Balance - 音量平衡(-1 - 1 之间)

                        mediaElement.Balance = balanceSlider.Value;

                        lblBalance.Text = @H_404_105@string.Format(

                                 "音量平衡:{0:##%}",

                                balanceSlider.Value);

                }

        }

}
 
 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐