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

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

概述[索引页] [源码下载] 稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器 作者:webabcd 介绍 Silverlight 2.0 详解MediaElement:开发一个简易版的全功能播放器     MediaOpened - 当媒体被成功地打开时所触发的事件     MediaFailed - 当媒体未能被成功地打开时所触发 [索引页]
[源码下载]


稳扎稳打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

using System;

using System.Collections.Generic;

using System.linq;

using System.Net;

using System.windows;

using System.windows.Controls;

using System.windows.documents;

using System.windows.input;

using System.windows.Media;

using System.windows.Media.Animation;

using System.windows.Shapes;


using System.windows.Threading;


namespace Silverlight20.VIDeo

{

         public partial class VIDeoPlayer : UserControl

        {

                 // 媒体的时长

                 private TimeSpan _duration;


                 private dispatcherTimer _timer = new dispatcherTimer();


                 public VIDeoPlayer()

                {

                        InitializeComponent();


                         this.Loaded += new RoutedEventHandler(VIDeoPlayer_Loaded);


                         /*

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

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

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

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

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

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

                         */


                        mediaElement.MediaOpened += new RoutedEventHandler(mediaElement_MediaOpened);

                        mediaElement.CurrentStateChanged += new RoutedEventHandler(mediaElement_CurrentStateChanged);

                        mediaElement.DownloadProgressChanged += new RoutedEventHandler(mediaElement_DownloadProgressChanged);

                        mediaElement.MediaEnded += new RoutedEventHandler(mediaElement_MediaEnded);

                        mediaElement.BufferingProgressChanged += new RoutedEventHandler(mediaElement_BufferingProgressChanged);

                }


                 voID VIDeoPlayer_Loaded( object sender,RoutedEventArgs e)

                {

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

                        _timer.Interval = TimeSpan.FromMilliseconds(500);

                        _timer.Tick += new EventHandler(_timer_Tick);

                        _timer.Start();

                }


                 voID _timer_Tick( object sender,EventArgs e)

                {

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

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

                         if (mediaElement.CurrentState == MediaElementState.Playing)

                        {

                                lblPlayTime.Text = string.Format(

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

                                         @H_301_423@"播放进度:",

                                        mediaElement.position.Hours,

                                        mediaElement.position.Minutes,

                                        mediaElement.position.Seconds);

                        }


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

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

                }


                 voID mediaElement_BufferingProgressChanged( object sender,RoutedEventArgs e)

                {

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

                        lblBufferingProgress.Text = string.Format(

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

                                mediaElement.BufferingProgress);

                }


                 voID mediaElement_MediaEnded( object sender,RoutedEventArgs e)

                {

                        mediaElement.Stop();

                }


                 voID mediaElement_DownloadProgressChanged( object sender,RoutedEventArgs e)

                {

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

                        lblDownloadProgress.Text = string.Format(

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

                                mediaElement.DownloadProgress);

                }

                 private voID mediaElement_MediaOpened( object sender,RoutedEventArgs e)

                {

                         /*

                         * NaturalVIDeoWIDth - 媒体文件的宽

                         * NaturalVIDeoHeight - 媒体文件的高

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

                         * NaturalDuration - 媒体文件的时长

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

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

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

                         */


                        lblWIDth.Text = @H_301_423@"媒体文件的宽:" + mediaElement.NaturalVIDeoWIDth.ToString();

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


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


                        lblTotalTime.Text = string.Format(

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

                                _duration.Hours,

                                _duration.Minutes,

                                _duration.Seconds);


                        mediaElement.Volume = 0.8;

                        volumeSlIDer.Value = 0.8;

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


                        mediaElement.Balance = 0;

                        balanceSlIDer.Value = 0;

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


                        mediaElement.BufferingTime = TimeSpan.FromSeconds(30);

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

                }


                 private voID mediaElement_CurrentStateChanged( 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 = @H_301_423@"播放状态:" + mediaElement.CurrentState.ToString();

                }


                 private voID play_Click( object sender,RoutedEventArgs e)

                {

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

                        mediaElement.Play();

                }


                 private voID pause_Click( object sender,RoutedEventArgs e)

                {

                         // CanPause - 媒体是否可暂停

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

                         if (mediaElement.CanPause)

                                mediaElement.Pause();

                }


                 private voID stop_Click( object sender,RoutedEventArgs e)

                {

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

                        mediaElement.Stop();

                }


                 voID mute_Click( object sender,RoutedEventArgs e)

                {

                         // IsMuted - 是否静音

                         if (mediaElement.IsMuted == true)

                        {

                                mute.Content = @H_301_423@"静音";

                                mediaElement.IsMuted = false;

                        }

                         else

                        {

                                mute.Content = @H_301_423@"有声";

                                mediaElement.IsMuted = true;

                        }

                }


                 private voID playSlIDer_ValueChanged( object sender,RoutedPropertyChangedEventArgs< double> e)

                {

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

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

                         if (mediaElement.CanSeek)

                        {

                                mediaElement.Pause();

                                mediaElement.position = TimeSpan.FromSeconds(_duration.TotalSeconds * playSlIDer.Value);

                                mediaElement.Play();

                        }

                }


                 private voID volumeSlIDer_ValueChanged( object sender,RoutedPropertyChangedEventArgs< double> e)

                {

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

                        mediaElement.Volume = volumeSlIDer.Value;

                        lblVolume.Text = string.Format(

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

                                volumeSlIDer.Value);

                }


                 private voID balanceSlIDer_ValueChanged( object sender,RoutedPropertyChangedEventArgs< double> e)

                {

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

                        mediaElement.Balance = balanceSlIDer.Value;

                        lblBalance.Text = string.Format(

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

                                balanceSlIDer.Value);

                }

        }

}    
OK
[源码下载]
总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器全部内容,希望文章能够帮你解决稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1052964.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存