オーディオ ファイルの再生

提供: RAD Studio
移動先: 案内検索

チュートリアル:FireMonkey のオーディオ/ビデオ機能 への移動

このチュートリアルでは、FireMonkey アプリケーションにオーディオ メディア データを追加し、再生する方法を説明します。

フォームの設計

  1. 前のチュートリアルで作成したフォームを使用します。
  2. 空のフロー レイアウトに以下を追加します。
    • TLabel。テキストを「Playing」に設定します。
    • 前のチュートリアルの手順で録音したオーディオ ファイルの再生を開始するための TButton
    • オーディオ ファイルの再生を停止または一時停止するための TButton
  3. 2 つのボタンの名前を「PlayButton」および「StopPlayButton」に変更します。
  4. StopPlayButtonEnable プロパティを False に設定して無効にします。
  5. TMediaPlayer をフォームに追加します。
  6. さらに 2 つの TImages をフォームに追加します。それぞれの TImageTImage.Bitmap プロパティを、再生および一時停止のアクションを示す画像に設定します。再生アクションには矢印のアイコンが、一時停止アクションには縦の 2 本線のアイコンが、通常は使われます。
  7. すべての画像を同じ位置および同じサイズに設定します。3 つの画像は完全に重なるはずです。
  8. すべての画像の Opacity プロパティを 0 に設定します。表示される画像は、現在実行中の処理を表す画像になります。
  9. TTimer をフォームに追加します。有効に設定します。
    フォームは次のようになります。
    Audio Play Form Design.png

実装

TMediaPlayer を使ってオーディオ ファイルを再生するには、FileName を設定します。

1. [Play]ボタンをダブルクリックして、OnClick イベント ハンドラを付加します。
Delphi の場合:
procedure TForm1.PlayButtonClick(Sender: TObject);
begin
// This application plays only recorded data, so if there is no data recorded there is nothing to play.
  if (Mic <> nil) and (Mic.State = TCaptureDeviceState.Stopped) then
  begin
    //Specifies the file to be played.
    MediaPlayer1.FileName := Mic.FileName;
    //Checks if the data can be decoded.
    if MediaPlayer1.Media <> nil then
    begin
      if (MediaPlayer1.State = TMediaState.Stopped) then
      begin
        Timer1.Enabled := true;
        StopPlayButton.Enabled := true;
        MediaPlayer1.Play;
        //Updates the opacity of each image.
        // Image2 shows the play sign.
        Image1.Opacity := 0;
        Image2.Opacity := 1;
        Image3.Opacity := 0;
      end;
    end;
  end
  else
  begin
    ShowMessage('The audio device is still capturing');
  end;
end;
C++ の場合:
void __fastcall TForm1::PlayButtonClick(TObject *Sender) 
{
  // This application plays only recorded data, so if there is no data recorded there is nothing to play.
  if ((Mic != NULL) && (Mic->State == TCaptureDeviceState::Stopped)) 
  {
    //Specifies the file to be played.
    MediaPlayer1->FileName = Mic->FileName;
    //Checks if the data can be decoded.
    if (MediaPlayer1->Media != NULL) 
    {
      Timer1->Enabled = 1;
      StopPlayButton->Enabled = 1;
      MediaPlayer1->Play();
      //Updates the opacity of each image.
      // Image2 shows the play sign.
      Image1->Opacity = 0;
      Image2->Opacity = 1;
      Image3->Opacity = 0;
    }
  }
  else
    ShowMessage("The audio device is still capturing.");
}
2. [Stop/Pause]ボタンをダブルクリックして、OnClick イベント ハンドラを追加します。
Delphi の場合:
procedure TForm1.StopPlayButtonClick(Sender: TObject);
begin
  if MediaPlayer1.Media <> nil then
  begin
    if MediaPlayer1.State = TMediaState.Playing then
    begin
      MediaPlayer1.Stop;
      Image1.Opacity := 0;
      Image2.Opacity := 0;
      Image3.Opacity := 1;
      Timer1.Enabled := false;
    end
    else
    begin
      Image1.Opacity := 0;
      Image2.Opacity := 1;
      Image3.Opacity := 0;
      MediaPlayer1.Play;
      Timer1.Enabled := true;
    end;
  end;
end;
C++ の場合:
void __fastcall TForm1::StopPlayButtonClick(TObject *Sender) 
{
  if (MediaPlayer1->Media != NULL) 
  {
    if (MediaPlayer1->State == TMediaState::Playing) 
    {
      MediaPlayer1->Stop();
      Image1->Opacity = 0;
      Image2->Opacity = 0;
      Image3->Opacity = 1;
      Timer1->Enabled = 0;
    }
    else 
    {
      Image1->Opacity = 0;
      Image2->Opacity = 1;
      Image3->Opacity = 0;
      Timer1->Enabled = 1;
    }
  }
}
3. タイマをダブルクリックして、OnTimer イベント ハンドラを追加します。このタイマは、再生処理中に画像の不透明度を操作するために使用します。
Delphi の場合:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (MediaPlayer1.Media <> nil) and
    (MediaPlayer1.CurrentTime = MediaPlayer1.Duration) then
  begin
    Image1.Opacity := 0;
    Image2.Opacity := 0;
    Image3.Opacity := 0;
    StopPlayButton.Enabled:=false;
    MediaPlayer1.Stop;
    MediaPlayer1.CurrentTime:=0;
  end;
end;
C++ の場合:
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  if ((MediaPlayer1->Media != NULL) &&
      (MediaPlayer1->CurrentTime == MediaPlayer1->Duration)) 
  {
    Image1->Opacity = 0;
    Image2->Opacity = 0;
    Image3->Opacity = 0;
    StopPlayButton->Enabled = 0;
    MediaPlayer1->Stop();
    MediaPlayer1->CurrentTime = 0;
  }
}

アプリケーションの実行

1. F9 キーを押して、プロジェクトを実行します。
2. [Record]ボタンを押してオーディオ データの録音を開始します。SaveDialog が開きます。
3. 録音したデータを保存するパスとファイル名を選択します。
フォームは次のようになります。
Recording process.png
4. [Stop]ボタンを押して録音を終了します。StopCapture メソッドを呼び出して録音の終了処理を行わなければ、メディア プレーヤーで再生しようとしたときに保存ファイルが正しくデコードされません。
5. [Play]ボタンを押します。メディア ファイルの再生が始まります。
再生中のフォームは次のようになります。
Playing process.png
6. [Stop/Pause]ボタンを押します。メディア ファイルが一時停止します。
メディア ファイルの一時停止中のフォームは次のようになります。
Stop process.png

前のトピック

次のトピック

関連項目

サンプル