Delphi多媒體設計之TMediaPlayer組件(六)

來源:互聯網
上載者:User
CD音頻(CD Audio)

TMediaPlayer播放CD音頻相當簡單。要放CD,只需簡單地改變DeviceType屬性為dtCDAudio即可,並點擊Play按鈕(或調用Play方法)。

 

掌握CD音訊裝置程式設計最困難的是用於CD中不同的時間格式。使用者將使用TMSF(time, minutes, seconds, frames)的時間格式來擷取某一特定音軌的資訊或設定當前位置為某一個特定音軌。例如:下面的代碼中將格式化字串並得到當前音軌內播放的位置。

var Time: Integer; Track: Integer; Minutes: Integer; Seconds: Integer; TimeStr: string;begin  if MediaPlayer.Mode <> mpPlaying then    Exit;  Time    := MediaPlayer.Position;  Track   := mci_TMSF_Track(Time);  Minutes := mci_TMSF_Minute(Time);  Seconds := mci_TMSF_Second(Time);  TimeStr := Format('Track Time: %2.2d:%2.2d', [Minutes, Seconds]);  lbl1.Caption := 'Track: ' + IntToStr(Track);  lbl2.Caption := TimeStr;  { 擷取總體時間}  MediaPlayer.TimeFormat := tfMSF;  Time := MediaPlayer.Position;  Minutes := mci_MSF_Minute(Time);  Seconds := mci_MSF_Second(Time);  TimeStr := Format('Total Time: %2.2d:%2.2d', [Minutes, Seconds]);  lbl3.Caption := TimeStr;  MediaPlayer.TimeFormat := tfTMSF;end;

首先TimeFormat被設定成tfTMSF確保正確的時間格式。其次,當前位置儲存在Time變數中。再次,利用Windows的時間轉換宏(macros)mci_TMSF_Track,mci_TMSF_Minute和mci_TMSF_Second擷取不同的時間值(音軌,分和秒)。這些宏包含在MMSystem單元中。如果使用這些宏,必須把MMSystem單元添加到uses列表中。獲得單獨的時間後,分別在lbl1和lbl2標籤中顯示當前音軌,當前音軌的分和秒資訊。

使用者使用MSF(分、秒和幀)的時間格式來獲得整體的時間資訊。例如,使用MSF事件格式擷取相對於CD起點的當前位置。同樣地,如果想設定CD當前位置為30分而不考慮音軌的話,使用MSF格式。上面的代碼中包括了如何擷取總體時間資訊的代碼。

本講範例,使用了一個TMediaPlayer組件,設定其屬性如下:

增加了一個Timer組件,用於定時擷取播放的進度時間,其屬性設定如下:

增加三個Label標籤組件,分別顯示當前音軌、當前播放時間、總體播放時間資訊,介面設計如下:

接下來在Timer的OnTimer事件中輸入顯示時間的代碼:

procedure TForm1.TimerTimer(Sender: TObject);var Time: Integer; Track: Integer; Minutes: Integer; Seconds: Integer; TimeStr: string;begin  if MediaPlayer.Mode <> mpPlaying then    Exit;  Time    := MediaPlayer.Position;  Track   := mci_TMSF_Track(Time);  Minutes := mci_TMSF_Minute(Time);  Seconds := mci_TMSF_Second(Time);  TimeStr := Format('Track Time: %2.2d:%2.2d', [Minutes, Seconds]);  lbl1.Caption := 'Track: ' + IntToStr(Track);  lbl2.Caption := TimeStr;  { 擷取總體時間}  MediaPlayer.TimeFormat := tfMSF;  Time := MediaPlayer.Position;  Minutes := mci_MSF_Minute(Time);  Seconds := mci_MSF_Second(Time);  TimeStr := Format('Total Time: %2.2d:%2.2d', [Minutes, Seconds]);  lbl3.Caption := TimeStr;  MediaPlayer.TimeFormat := tfTMSF;end;

在MediaPlayer組件的OnClick事件中輸入下面的代碼:

procedure TForm1.MediaPlayerClick(Sender: TObject; Button: TMPBtnType;  var DoDefault: Boolean);begin  if Button = btEject then begin    lbl1.Caption := 'Track: ';    lbl2.Caption := 'Track Time: ';    lbl3.Caption := 'Total Time: ';  end;end;

OK,在你的光碟機中放入CD音樂,然後運行程式,點擊播放按鈕開始欣賞美妙的音樂吧。

以上代碼在Delphi 7中測試通過,範例程式碼下載:CDPlayer.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.