Playing Audio Clips on Android

by Sep 10, 2013

I have been working with our R&D team on the samples and snippets for RAD Studio XE5. Our mobile snippets are designed to showcase key functionality with just a couple of lines of code.

Today I thought I would highlight our AudioPlayBack snippet.

This snippet consists of the following 4 components:

  • TToolbar
  • 2 TSpeedButtons parented to the toolbar
  • TMediaPlayer

The toolbar Align property has been set to alTop. The first SpeedButton (the one to stop the playback) has been set to alLeft, with a Left Margin on 5. The second SpeedButton (the one to play the audio clip) has been set to alRight, with a Right Margin on 5.

Setting alignments and margins for the toolbar buttons automatically centers them vertically which ensures that your UI looks good on both Android and iOS.

For this snippet, I want to be able to play back an existing mp3 file I recorded.

To deploy an audio file with your application, you need to do the following:

    • Put the mp3 file into the same directory as the project
    • Go to Project-> Deployment and selected 'All Configurations – iOS Device Platform/iOS Simulator Platform/Android Platform'
    • Click on the 'Add Files' icon

      • Browse to your mp3 file
      • Set the Remote Path for your mp3 file

       Android

       iOS

      I also created two on-click events for playing the audio clip and stopping playback.

      uses

      System.iOUtils;

      procedure TAudioPlayBackForm.btnPlayClick(Sender: TObject);

      begin

      MediaPlayer1.FileName := TPath.GetDocumentsPath + PathDelim + 'soundsample.mp3';

      MediaPlayer1.Play;

      end;

      procedure TAudioPlayBackForm.btnStopClick(Sender: TObject);

      begin

      MediaPlayer1.Stop;

      end;

      end.

      Sincerely,

      Sarina