RAD Studio 10.4.1 is now available! Learn more. 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


      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.
      Related posts
      C++Delphi

      Ultimate Compression Toolkit For Delphi And C++ Builder Developers

      DelphiNews

      RAD Studio with Delphi - The Original Low Code!

      DelphiShowcase

      KisKis The Crazy Social Network For Android And iOS In Delphi

      DelphiRAD Studio

      Setting up the IDE for your First Android App

      Leave a Reply

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

      IN THE ARTICLES