打造.Net Framework3.0 下的Media Player

*基本要求:安装了.NET Framework 3.0 beta版(我用的是beta3)

下载Demo

打造.Net Framework3.0 下的Media Player
 

我打算学着做一个MP3 Player玩一下,虽然在.Net Famework 2.0 System.Media命名空间有一个可以播放声音的类SoundPlayer,然而它只支持播放*.wav波形文件,而我需要的是可以播放*.mp3文件。于是在网搜了一下,结果基本上都是用“Windows Media Player”这一COM组件

于是我又在Microsoft Visual Studio 2005 文档中搜了一下“MediaPlayer”,以期上面有对这一组件的说明,结果意外地发现在.Net Framework 3.0中竟然就有“MediaPlayer”这个类,一看名字就猜测它能够满足我的需求,最后证明的确如此。于是我立即动手写了一个Demo,学习一下这个类的使用。

MediaPlayer处于System.Windows.Media命名空间下,与PresentationCore.dll、PresentationFramework.dll以及WindowsBase.dll三个.Net Framwork 3.0中新加入的三个动态链接库相关联。好了,那就让我们开始吧!

先新建一个Window应用程序,然后添加对以上3个动态链接库的引用,最后再加一条“using System.Windows.Media;”添加MediaPlayer的命名空间。你就可以在你的应用程序中使用MediaPlayer这个类了。

当然,你也可以直接新建一个WinFx Windows Application,它会自动完成对动态链接库和命名空间的引用,然后删除xaml文件,因为在我的这个Demo里只是想试用一下MediaPlay这个类,不需要用到xaml,再添加一个Windows窗体即可(当然还要手工添加Main函数)。

至此该Demo的应用程序框架已经搭建好了,只剩下在它上面添砖加瓦了,也就是尝试着使用MediaPlayer的各种属性和方法了。

以下是,NET Framwork 3.0 Development   >   Class Library 中列出的该类的全部属性和方法:

Provides media playback for drawings.

The following tables list the members exposed by the MediaPlayer type.

Public Constructors

 

Name

Description

MediaPlayer

Initializes a new instance of the MediaPlayer class.

Public Properties

 

Name

Description

Balance

Gets or sets the balance between the left and right speaker volumes.

BufferingProgress

Gets the percentage of buffering completed for streaming content.

CanFreeze 

Gets a value that indicates whether this object can be made unmodifiable. (inherited from Freezable)

CanPause

Gets a value indicating whether the media can be paused.

Clock

Gets or sets the MediaClock associated with the MediaTimeline to be played.

DependencyObjectType 

Gets the type that represents the common language runtime (CLR) type of this instance. (inherited from DependencyObject)

Dispatcher 

Gets the Dispatcher this DispatcherObject is associated with. (inherited from DispatcherObject)

DownloadProgress

Gets the percentage of download progress for content located at a remote server.

HasAnimatedProperties 

Returns true if any properties on this DependencyObject have a persistent animation or the object has one or more clocks associated with any of its properties. (inherited from Animatable)

HasAudio

Gets a value that indicating whether the media has audio output.

HasVideo

Gets a value that indicates whether the media has video output.

IsBuffering

Gets a value that indicates whether the media is buffering.

IsFrozen 

Gets a value that indicates whether the object is currently modifiable. (inherited from Freezable)

IsMuted

Gets a value that indicates whether the media is muted.

IsSealed 

Gets a value that declares whether this instance is currently read-only.(inherited from DependencyObject)

NaturalDuration

Gets the natural duration of the media.

NaturalVideoHeight

Gets the pixel height of the video.

NaturalVideoWidth

Gets the pixel width of the video.

Position

Gets or sets the current position of the media.

ScrubbingEnabled

 

Source

Gets the media Uri.

SpeedRatio

Gets or sets the ratio of speed that media is played at.

Volume

Gets or sets the media's volume.

Public Methods (see also Protected Methods )

 

Name

Description

ApplyAnimationClock 

Overloaded. Animates the specified DependencyProperty using the specified AnimationClock. (inherited from Animatable)

BeginAnimation 

Overloaded. Applies an animation to the specified DependencyProperty. (inherited from Animatable)

CheckAccess 

Determines whether the calling thread has access to this DispatcherObject. (inherited from DispatcherObject)

ClearValue 

Overloaded. Clears the local value of a property. (inherited from DependencyObject)

Clone 

Returns a modifiable copy of this instance. (inherited from Animatable)

CloneCurrentValue 

Creates a modifiable clone of this Freezable, making deep copies of this object's current values. (inherited from Freezable)

Close

 

CoerceValue 

Invokes any CoerceValueCallback function that was declared when this dependency property was registered. This same callback will be called internally whenever that property on a DependencyObject changes its value. (inherited from DependencyObject)

Equals 

Overloaded. (inherited from DependencyObject)

Freeze 

Overloaded. Makes a Freezable unmodifiable and sets its IsFrozen property to true, or determines whether the specified Freezable can be made unmodifiable. (inherited from Freezable)

GetAnimationBaseValue 

Returns the non-animated value of the specified DependencyProperty. (inherited from Animatable)

GetAsFrozen 

Creates a frozen copy of this Freezable , using base (non-animated, non-databound) property values. Because the copy is frozen, any frozen sub-objects are copied by reference. (inherited from Freezable)

GetCurrentValueAsFrozen 

Creates a frozen copy of this Freezable , using current property values. Because the copy is frozen, any frozen sub-objects are copied by reference. (inherited from Freezable)

GetHashCode 

(inherited from DependencyObject)

GetLocalValueEnumerator 

Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject. (inherited from DependencyObject)

GetType 

Gets the Type of the current instance. (inherited from Object)

GetValue 

Returns the current effective value of a dependency property on this DependencyObject. (inherited from DependencyObject)

InvalidateProperty 

Invalidates the preexisting value of the specified dependency property. (inherited from DependencyObject)

Open

Opens the given Uri for media playback.

Pause

Pauses media playback.

Play

Plays media from the current Position.

ReadLocalValue 

Returns the local value of a dependency property, if it exists. (inherited from DependencyObject)

ReferenceEquals 

Determines whether the specified Object instances are the same instance. (inherited from Object)

SetValue 

Overloaded. Sets the local value of a dependency property. (inherited from DependencyObject)

ShouldSerializeStoredWeakReference 

(inherited from Animatable)

Stop

Stops media playback.

ToString 

Returns a String that represents the current Object. (inherited from Object)

VerifyAccess 

Determines whether the calling thread has access to this DispatcherObject. (inherited from DispatcherObject)

Protected Methods

 

Name

Description

CloneCore

Overridden.  

CloneCurrentValueCore

Overridden.  

CreateInstance 

Initializes a new instance of the Freezable class. (inherited from Freezable)

CreateInstanceCore

Overridden.  

Finalize 

Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (inherited from Object)

Freeze 

Overloaded. Makes a Freezable unmodifiable and sets its IsFrozen property to true, or determines whether the specified Freezable can be made unmodifiable. (inherited from Freezable)

FreezeCore 

An Animatable will return false from this method if there are any Clocks animating any of its properties. If the Animatable has persistent animations specified, but all of the Clocks have been removed, it may still return true from this method if the Timelines themselves can be frozen. (inherited from Animatable)

GetAsFrozenCore

Overridden.  

GetCurrentValueAsFrozenCore 

Implements GetCurrentValueAsFrozen behavior in Freezable derived classes. (inherited from Freezable)

MemberwiseClone 

Creates a shallow copy of the current Object. (inherited from Object)

OnChanged 

Called when the current Freezable object is modified. (inherited from Freezable)

OnFreezablePropertyChanged 

Overloaded. (inherited from Freezable)

OnPropertyChanged 

Overrides the DependencyObject implementation of OnPropertyChanged in order to also invoke any Changed handlers in response to a dependency property of type Freezable changing. (inherited from Freezable)

ReadPreamble

 

ShouldSerializeProperty 

Returns a value that indicates whether serialization processes should serialize the values for the given dependency property. (inherited from DependencyObject)

WritePostscript 

Invokes the OnChanged method on this Freezable. (inherited from Freezable)

WritePreamble

 

Public Events

 

Name

Description

BufferingEnded

Occurs when buffering has finished.

BufferingStarted

Occurs when buffering has started.

Changed 

Occurs when the current object is modified. (inherited from Freezable)

MediaEnded

Occurs when the media has finished playback.

MediaFailed

Occurs when an error is encountered

MediaOpened

Occurs when the media is opened.

ScriptCommand

Occurs when a script command has been encountered within the media.



这些相信不用我翻译就应该看得懂吧,从属性可以知道它不仅支持音频文件,还支持视频文件(其实从类名就可以看出来)。在这些属性中,最重要的有:

Source属性:

public Uri Source { get; }


IsMuted
属性:可以设置静音

Volume属性:The media's volume represented on a linear scale between 0 and 1. The default is 0.5.


Position
属性:

public TimeSpan Position { get; set; }


SpeedRatio
属性:The ratio of speed that media is played back represented by a value
between 0 and the largest double value. The default is 1.0.

 

 

最重要的方法当然就是OpenPlayPauseStop这四个方法了。

有了这此属性和方法,你不需要WMPlayer组件就可以打造一个属于你自己的MP3 Player了。

 

你可能感兴趣的:(framework)