VCL為簡單多媒體操作提供MediaPlayer組件。該組件位於選項板組件System中。
該組件可播放Wave文件、MIDI文件、AVI視頻等等。如果只是簡單的播放Wave文件,常常使用上一講中的PlaySound函數。也可以使用更復雜的MediaPlayer組件。
TMediaPlayer組件最簡單的使用方法就是將其拖放到窗體上。如下圖:
窗體上顯示出播放的控制按鈕,控制按鈕包括play、pause、stop、next、previous、step、back、record、eject。如上圖中。
使用MediaPlayer最基本的形式就是設置TMediaPlayer組件的FileName屬性為有效的多媒體文件名,然后並點擊MediaPlayer上的play按鈕即可。多媒體文件可任選WAV、MID或AVI文件,MediaPlayer知道怎樣自動處理這些文件。如果想要讓TMediaPlayer做一些更有趣的事情,就需要了解更深一些了。
盡管有時MediaPlayer的控制按鈕很有用,但是,有時候卻需要使用不帶控制按鈕的MediaPlayer,通過代碼來實現MediaPlayer的播放、開始等操作,如果不想在運行階段看到MediaPlayer控制按鈕,可以設置它的Visible屬性為False。
MediaPlayer屬性、方法和事件(MediaPlayer Properties, Methods, and Events)
TMediaPlayer具有許多屬性。其中大部分都是很容易理解的。但是有些比較復雜。下面的表格中列出了TMediaPlayer的主要屬性。
表1. TMediaPlayer主要屬性
屬性 | 描述 |
AutoOpen | Determines if the media player is opened automatically when the application is run. 是否已啟動播放程序就打開設備,默認為False。 |
AutoRewind | Determines if the media player control rewinds before playing or recording. 當為True時,在媒體播放后,媒體位置指針將被重新設置為媒體起點,默認為True。 |
DeviceType | Specifies a multimedia device type to open the media player. 多媒體設備類型。設置成dtAutoSelect時,可根據文件名擴展自動選擇設備類型,默認值為dtAutoSelect。 |
Display | Specifies the display window for an multimedia device that uses a window for output. 用於設置媒體設備的顯示窗口。 |
DisplayRect | Specifies a rectangular area in the control specified by the Display property that is used to display output from a multimedia device. 用於設置媒體設備回放窗口的大小和位置,重新確定視頻大小以適應矩形框。 |
EnableButtons | Determines which buttons on the media player are usable. 決定播放控制按鈕是否允許使用。默認為所有按鈕。 |
EndPos | Specifies the position within the currently loaded medium at which to stop playing or recording. 媒體結束位置,媒體播放是從起點到終點(EndPos),如果沒有指定EndPos,媒體將全部播放完。所賦EndPos值依賴於播放的類型。 |
Error | Specifies the MCI error code returned by the media control method. 最近一次操作的錯誤碼。 |
ErrorMessage | Describes the error code stored in the Error property. 最近錯誤的文本描述。 |
Frame | Specifies the number of frames moved forward or backward. 調用Back或Next方法時,或點擊MediaPlayer控制欄上的Back或Next按鈕時所要移動的幀數。 |
Length | Specifies the length of the medium in the open multimedia device. 媒體長度,Length值依賴於被播放的媒體類型和TimeFormat的值。 |
Mode | Indicates the state of the currently open multimedia device. 設備狀態,可以是mpNotready, mpStopped, mpPlaying, mpRecording, mpSeeking, mpPaused, mpOpen。 |
Notify | Determines whether an OnNotify event is generated. 若為True時,當MediaPlayer完成一個操作時,產生OnNotify事件。 |
Notify Value | Indicates the result of the last media control method that requested a notification. 上次通知操作的結果。可以是nvSuccessful, nvSuperseded, nvAborted或nvFailure。 |
Position | Specifies the current position within the currently loaded medium. 媒體當前位置。 |
StartPos | Specifies the position within the currently loaded medium from which to begin playing or recording. 媒體起點,媒體從StartPos開始播放到EndPos,若沒有指定媒體起點StartPos,媒體從頭開始播放。所賦StartPos值依賴於媒體類型。 |
TimeFormat | Determines the format used to specify position information. 使用媒體設備的時間格式。時間格式可以用毫秒、幀、字節、采樣、磁道/分/秒、小時/分/秒等格式。 |
Tracks | Specifies the number of playable tracks on the open multimedia device. 媒體所包含的磁道數(對於CD設備來說) |
VisibleButtons | Indicates which of the buttons on the media player are visible. 指定MediaPlayer控制欄上顯示的按鈕,默認為所有按鈕。 |
Wait | Determines whether a media control method returns control to the application only after it has been completed. 決定控制否馬上或在媒體播放完之后返回調用。 |
MediaPlayer組件還具有很多方法。這些方法許多功能與控制欄按鈕相同。下面表格列出了TMediaPlayer主要方法。
表2. TMediaPlayer主要方法
方法 | 描述 |
Back | Steps backward a number of frames (determined by the value of the Frames property) in the currently loaded medium. 將媒體回放由Frame屬性指定的幀數。 |
Close | Closes the open multimedia device. 關閉設備。 |
Eject | Releases the loaded medium from the open multimedia device. 如果合適的話,退出媒體(例如,退出CD)。 |
Next | Moves to the beginning of the next track of the currently loaded medium. 為支持軌道的設備開始下一個軌。 |
Open | Opens a multimedia device. 打開設備(用於AutoOpen屬性為False時)。 |
Pause | Toggles the open multimedia device on or off. 暫停播放或錄制。 |
Play | Plays the media loaded in the open multimedia device. 開始設備的播放。 |
Previous | Sets the current position to the beginning of the previous track if the position was at the beginning of a track when Previous was called. 移到前一個音軌的起點。 |
Resume | Resumes playing or recording the currently paused multimedia device. 恢復pause中斷的行為。 |
Rewind | Sets the current position to the beginning of the the medium, which is stored in the Start property. 重新設置媒體位置為媒體的開頭。 |
Save | Saves the currently loaded medium to the file specified in the FileName property. 把媒體存儲到FileName屬性指定的文件中。 |
StartRecording | Begins recording from the current Position or from the position specified in StartPos. 開始錄制數據。 |
Step | Moves forward a number of frames (determined by the Frames property) in the currently loaded medium. 前移Frame屬性指定的幀數。 |
Stop | Halts playing or recording. 停止播放或錄制。 |
MediaPlayer組件僅有一個重要的事件。每當完成一個命令時,OnNotify事件被調用,但是只有在Notify屬性設置為True時才行。可檢驗Error和NotifyValue屬性,確定操作是否成功執行。