public class MediaManager extends Object
Allow us to create objects using String URI's or with a stream to
the media data.
Notice that the underlying platforms contains the actual codecs, therefore
you need to play common media file types such as mp3, mp4 to successfully play them across devices
on all target platforms. The simulator can't accurately reproduce the behavior of this class in all devices/cases.
The sample code below demonstrates simple video playback.
The code below demonstrates capturing audio using the Capture API and playing back audio files using the Media API:
The code below demonstrates capturing audio and playing back audio using the Media, MediaManager and MediaRecorderBuilder APIs, as alternative and more customizable approach than using the Capture API:
Constructor and Description |
---|
MediaManager() |
Modifier and Type | Method and Description |
---|---|
static void |
addCompletionHandler(Media media,
Runnable onCompletion)
Adds a callback to a Media element that will be called when the media finishes playing.
|
static Media |
createBackgroundMedia(String uri)
Creates an audio media that can be played in the background.
|
static AsyncResource<Media> |
createBackgroundMediaAsync(String uri)
Creates an audio media asynchronously that can be played in the background.
|
static Media |
createMedia(InputStream stream,
String mimeType)
Creates the Media in the given stream.
|
static Media |
createMedia(InputStream stream,
String mimeType,
Runnable onCompletion)
Creates the Media in the given stream
Notice that you should invoke cleanup on a media once you are done with it.
|
static Media |
createMedia(String uri,
boolean isVideo)
Creates a Media from a given URI
|
static Media |
createMedia(String uri,
boolean isVideo,
Runnable onCompletion)
Creates a Media from a given URI
|
static AsyncResource<Media> |
createMediaAsync(InputStream stream,
String mimeType,
Runnable onCompletion)
Creates the Media in the given stream asynchronously.
|
static AsyncResource<Media> |
createMediaAsync(String uri,
boolean isVideo,
Runnable onCompletion)
Creates a Media from a given URI asynchronously.
|
static Media |
createMediaRecorder(MediaRecorderBuilder builder)
Creates a Media recorder Object which will record from the device mic to
a file in the given path.
|
static Media |
createMediaRecorder(String path)
Deprecated.
see createMediaRecorder(String path, String mimeType) instead
|
static Media |
createMediaRecorder(String path,
String mimeType)
Creates a Media recorder Object which will record from the device mic to
a file in the given path.
|
static void |
deleteAudioBuffer(String path)
Deprecated.
Prefer to use
releaseAudioBuffer(java.lang.String) |
static AsyncMedia |
getAsyncMedia(Media media)
Converts the media object into an AsyncMedia object.
|
static AudioBuffer |
getAudioBuffer(String path)
Gets an audio buffer at the given path.
|
static AudioBuffer |
getAudioBuffer(String path,
boolean create,
int size)
Gets or creates an audio buffer at the given path.
|
static String[] |
getAvailableRecordingMimeTypes()
Gets the available recording MimeTypes
|
static String |
getMediaRecorderingMimeType()
Deprecated.
see getAvailableRecordingMimeTypes() instead
|
static RemoteControlListener |
getRemoteControlListener()
Gets the currently registered remote control listener.
|
static void |
releaseAudioBuffer(String path)
Releases an audio buffer at a given path.
|
static void |
removeCompletionHandler(Media media,
Runnable onCompletion)
Removes onComplete callback from Media element.
|
static void |
setRemoteControlListener(RemoteControlListener l)
Registers a listener to be notified of remote control events - e.g.
|
public static AudioBuffer getAudioBuffer(String path)
path
- The path to the Audio buffer. This path doesn't correspond to a real file. It is just
used as a key to map to the audio buffer so that it can be addressed.public static AudioBuffer getAudioBuffer(String path, boolean create, int size)
path
- The path to the Audio buffer. This path doesn't correspond to a real file. It is just
used as a key to map to the audio buffer so that it can be addressed.create
- If this flag is true and no buffer exists at the given path,
then the buffer will be created.size
- The maximum size of the buffer.public static void releaseAudioBuffer(String path)
getAudioBuffer(java.lang.String, boolean, int)
will increment
the counter, and calls to releaseAudioBuffer(java.lang.String)
will decrement the counter.path
- The path to the buffer.public static void deleteAudioBuffer(String path)
releaseAudioBuffer(java.lang.String)
path
- The path to the audio buffer to delete.public static void setRemoteControlListener(RemoteControlListener l)
l
- The remote control listener to set. null to set no listener.public static RemoteControlListener getRemoteControlListener()
public static Media createBackgroundMedia(String uri) throws IOException
uri
- the uri of the media can start with jar://, file://, http://
(can also use rtsp:// if supported on the platform)IOException
- if creation of media from the given URI has failedpublic static AsyncResource<Media> createBackgroundMediaAsync(String uri)
uri
- the uri of the media can start with jar://, file://, http://
(can also use rtsp:// if supported on the platform)public static Media createMedia(String uri, boolean isVideo) throws IOException
uri
- the uri of the media can start with file://, http:// (can also
use rtsp:// although may not be supported on all target platforms)isVideo
- a boolean flag to indicate if this is a video mediaIOException
- if creation of media from the given URI has failedpublic static Media createMedia(InputStream stream, String mimeType) throws IOException
stream
- the stream containing the media datamimeType
- the type of the data in the streamIOException
- if the creation of the Media has failedpublic static AsyncResource<Media> createMediaAsync(InputStream stream, String mimeType, Runnable onCompletion)
stream
- the stream containing the media datamimeType
- the type of the data in the streampublic static Media createMedia(String uri, boolean isVideo, Runnable onCompletion) throws IOException
uri
- the uri of the media can start with file://, http:// (can also
use rtsp:// although may not be supported on all target platforms)isVideo
- a boolean flag to indicate if this is a video mediaonCompletion
- a Runnable to be called when the media has finishedIOException
- if creation of media from given URI failedpublic static AsyncResource<Media> createMediaAsync(String uri, boolean isVideo, Runnable onCompletion)
uri
- the uri of the media can start with file://, http:// (can also
use rtsp:// although may not be supported on all target platforms)isVideo
- a boolean flag to indicate if this is a video mediaonCompletion
- a Runnable to be called when the media has finishedpublic static void addCompletionHandler(Media media, Runnable onCompletion)
media
- The media to add the callback to.onCompletion
- The callback that will run on the EDT when the playback completes.removeCompletionHandler(com.codename1.media.Media, java.lang.Runnable)
public static void removeCompletionHandler(Media media, Runnable onCompletion)
media
- The media element.onCompletion
- The callback.addCompletionHandler(com.codename1.media.Media, java.lang.Runnable)
public static Media createMedia(InputStream stream, String mimeType, Runnable onCompletion) throws IOException
stream
- the stream containing the media datamimeType
- the type of the data in the streamonCompletion
- a Runnable to be called when the media has finishedIOException
- if the URI access failspublic static Media createMediaRecorder(String path) throws IOException
path
- a file path to where to store the recording, if the file does
not exists it will be created.IOException
public static String getMediaRecorderingMimeType()
public static String[] getAvailableRecordingMimeTypes()
public static Media createMediaRecorder(String path, String mimeType) throws IOException
path
- a file path to where to store the recording, if the file does
not exists it will be created.mimeType
- the output mime type that is supported see
getAvailableRecordingMimeTypes()IllegalArgumentException
- if given mime-type is not supportedIOException
- id failed to create a Media objectpublic static Media createMediaRecorder(MediaRecorderBuilder builder) throws IOException
builder
- media settingsIllegalArgumentException
- if given mime-type is not supportedIOException
- id failed to create a Media objectpublic static AsyncMedia getAsyncMedia(Media media)
media
- The media object to convert.