Audio-Video in FireMonkey

From RAD Studio
Jump to: navigation, search

Go Up to FireMonkey Applications Guide

FireMonkey supports the capture and playback of media files (audio-video).

This feature is available for Windows and OS X platforms as follows:

  • The Windows implementation uses DirectShow, and the audio-video feature works on every Windows version.
  • The OS X implementation uses QuickTime and works on OS X versions higher than 10.5.

Supported File Formats

We recommend that you save and use media files in the following formats:

  • .wav on Windows
  • .caf on iOS and Mac
  • .3GP on Android

You also can play other types of media files, such as MP3 files. For example, Android video files are typically the H.263 format.

Audio/Video Capture

The base class for capturing devices is TCaptureDevice. This class encapsulates the basic methods and properties for capturing devices. TAudioCaptureDevice and TVideoCaptureDevice extend the TCaptureDevice implementation to add specific behavior for audio capturing devices (like microphones) and respectively video capturing devices (like web cameras).

TCaptureDeviceManager is introduced to access and manage the capturing devices. TCaptureDeviceManager has specific methods and properties to:

The currently used device is specified through the Current property of the device manager. Current is created when an application requests access to TCaptureDeviceManager and is kept until the application is closed.

Code Example

// Delphi declaration
var
  VideoCamera : TVideoCaptureDevice;
begin
  // Get access to the default video capture device
  VideoCamera := TCaptureDeviceManager.Current.DefaultVideoCaptureDevice;
  if VideoCamera <> nil then
  begin
     //do something
  end;
end;
// C++ declaration
TCaptureDeviceManager* CaptureManager = TCaptureDeviceManager::Current;
// Get access to the default video capture device
TVideoCaptureDevice* VideoCamera = CaptureManager->DefaultVideoCaptureDevice;

if(VideoCamera){
  //do something
}

To start or stop the capture process, you must get access to the device using the TCaptureDeviceManager. Never destroy capture devices explicitly; only the capture manager does that.

TCaptureDeviceManager allows adding custom capture devices developed by third-party vendors. This new device can be restored using the RegisterCaptureDeviceClass method. After registering the new device, it can be accessed as every standard device in TCaptureDeviceManager.

Media Playback

The TMedia class is introduced to support playback for media sources. TMedia exposes media file properties such as file name, size, dimension, state, or duration.

For easy access, the nonvisual component TMediaPlayer is introduced. Call the Play and Stop methods to start playing a media file or to stop or pause a running media file. The current position is specified through the CurrentTime property. TMediaPlayer also exposes media file properties such as Duration, VideoSize, Volume, or State.

  • To play an audio file using TMediaPlayer, just specify the name of the file to be played by setting the FileName property and calling the Play method. The file name must include the path of the file, so it can be localized on the memory, and the extension.

Video on Android Platform

  • On the Android platform, .3GP files are typically audio-only, as illustrated in the AudioRecPlay mobile code snippet.
    However, the .3GP format can also support video playback. If you switch to use video codec in FMX.Media.Android.pas, you can play video as with other files (such as mp4). However, if you make this change, you cannot record audio and then play it back as you would expect with the AudioRecSample.

Code Example

//Delphi declaration
  MediaPlayer1.FileName := 'myVideo.avi';

  if MediaPlayer1.Media <> nil then
  begin
      MediaPlayer1.Play;
  end;
// C++ declaration
MediaPlayer1->FileName="myVideo.avi";
if(MediaPlayer1->Media!=null)
{
   MediaPlayer1->Play();
}

FireMonkey audio/video playback feature offers support for native platform media file formats. Using TMediaCodecManager, it is possible to register custom media codecs to extend the audio/video feature support.

See Also

Samples