Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
Delphi

Playing Audio Clips on Android

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

      See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

      Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
      Design. Code. Compile. Deploy.
      Start Free Trial   Upgrade Today

         Free Delphi Community Edition   Free C++Builder Community Edition

      About author

      Director of Product Management, Developer Tools Idera, Inc.

      Leave a Reply

      This site uses Akismet to reduce spam. Learn how your comment data is processed.

      IN THE ARTICLES