package com.codename1.media

Video and Audio playback support are handled within this package using the com.codename1.media.Media & com.codename1.media.MediaManager APIs. Said API’s allow for video playback both within a native full screen player and embedded within an application screen.

Simplified video playback API is also available via the com.codename1.components.MediaPlayer class. Capture/recording is handled separately for the most part thru the com.codename1.capture.Capture API. However, there is some basic low level recording functionality within com.codename1.media.MediaManager as well.

The code below demonstrates capturing and playing back audio files using this API:

Form hi = new Form("Capture", BoxLayout.y());
hi.setToolbar(new Toolbar());
Style s = UIManager.getInstance().getComponentStyle("Title");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_MIC, s);

FileSystemStorage fs = FileSystemStorage.getInstance();
String recordingsDir = fs.getAppHomePath() + "recordings/";
fs.mkdir(recordingsDir);
try {
    for(String file : fs.listFiles(recordingsDir)) {
        MultiButton mb = new MultiButton(file.substring(file.lastIndexOf("/") + 1));
        mb.addActionListener((e) -> {
            try {
                Media m = MediaManager.createMedia(recordingsDir + file, false);
                m.play();
            } catch(IOException err) {
                Log.e(err);
            }
        });
        hi.add(mb);
    }

    hi.getToolbar().addCommandToRightBar("", icon, (ev) -> {
        try {
            String file = Capture.captureAudio();
            if(file != null) {
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MMM-dd-kk-mm");
                String fileName =sd.format(new Date());
                String filePath = recordingsDir + fileName;
                Util.copy(fs.openInputStream(file), fs.openOutputStream(filePath));
                MultiButton mb = new MultiButton(fileName);
                mb.addActionListener((e) -> {
                    try {
                        Media m = MediaManager.createMedia(filePath, false);
                        m.play();
                    } catch(IOException err) {
                        Log.e(err);
                    }
                });
                hi.add(mb);
                hi.revalidate();
            }
        } catch(IOException err) {
            Log.e(err);
        }
    });
} catch(IOException err) {
    Log.e(err);
}
hi.show();

The sample code below demonstrates simple video playback.

final Form hi = new Form("MediaPlayer", new BorderLayout());
hi.setToolbar(new Toolbar());
Style s = UIManager.getInstance().getComponentStyle("Title");
FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_VIDEO_LIBRARY, s);
hi.getToolbar().addCommandToRightBar(new Command("", icon) {
@Override
    public void actionPerformed(ActionEvent evt) {
        Display.getInstance().openGallery((e) -> {
            if(e != null && e.getSource() != null) {
                String file = (String)e.getSource();
                try {
                    Media video = MediaManager.createMedia(file, true);
                    hi.removeAll();
                    hi.add(BorderLayout.CENTER, new MediaPlayer(video));
                    hi.revalidate();
                } catch(IOException err) {
                    Log.e(err);
                }
            }
        }, Display.GALLERY_VIDEO);
    }
});
hi.show();

Types

class AbstractMediaAn abstract base class for AsyncMedia.
interface AsyncMediaAn interface for media elements that provides asynchronous pause and play functionality as well as support for state change events, so that interested parties can register to be notified when state changes between play and pause.
class AudioBufferThis class can be used to capture raw PCM data from the device’s microphone.
class AudioEffectsSmall PCM effects that operate on AudioBuffer data.
class AudioMixerMixes multiple PCM tracks into a single AudioBuffer.
interface CompletionAwareSoundPoolPeerAn optional capability a SoundPoolPeer may implement to report when its voices finish playing.
interface MediaMedia control interface allows for media playback, recording.
class MediaManagerAllow us to create Media objects using String URI’s or with a stream to the media data.
class MediaMetaDataMetaData for use by RemoteControlListener to provide information about the currently playing background media on the device’s lock screen.
class MediaRecorderBuilderA builder class to generate a Media recorder with specific settings.
interface RecognitionCallbackListener for SpeechRecognizer results.
class RecognitionOptionsTuning knobs for SpeechRecognizer.
class RemoteControlListenerA base class that is meant to be overridden to implement functionality that responds to the device’s remote control for media playback.
interface SoundPoolPeerLow level service provider interface backing com.codename1.gaming.SoundPool.
class SpeechRecognizerOn-device speech-to-text.
class TextToSpeechSpeaks text aloud using the platform’s built-in synthesizer.
interface TimedRecognitionCallbackCaption-aware SpeechRecognizer callback.
class TranscriberProvider-pluggable file transcriber.
class TranscriptionRequestFile transcription request shared by transcription providers.
class TranscriptionResultA complete transcription: plain text plus the timed segments needed for captions.
class TranscriptionSegmentOne timed transcript span.
class TtsOptionsTuning knobs for TextToSpeech.speak(String, TtsOptions).
class VideoCodecDescribes a single codec made available by the underlying platform, as returned by VideoIO#getAvailableEncoders() and VideoIO#getAvailableDecoders().
class VideoFrameA single decoded video frame produced by a VideoReader.
class VideoIOLow level video encoding and decoding using the platform’s native codecs.
class VideoReaderDecodes an existing video clip into individual RGBA frames and PCM audio.
class VideoWriterEncodes application supplied frames and audio into a video file using the platform’s native codecs.
class VideoWriterBuilderA fluent builder describing how a video file should be encoded.
interface VoiceCompletionListenerLow-level callback a SoundPoolPeer fires when one of its voices finishes.
class WAVWriterA class that can write raw PCM data to a WAV file.