public class

OpenSLMediaPlayer

extends Object
implements IBasicMediaPlayer
java.lang.Object
   ↳ com.h6ah4i.android.media.opensl.OpenSLMediaPlayer

Summary

Constants
int OPTION_USE_FADE
[Expand]
Inherited Constants
From interface com.h6ah4i.android.media.IBasicMediaPlayer
Public Constructors
OpenSLMediaPlayer(OpenSLMediaPlayerContext context, int options)
Public Methods
void attachAuxEffect(int effectId)
Attaches an auxiliary effect to the player.
int getAudioSessionId()
Returns the audio session ID.
int getCurrentPosition()
Gets the current playback position.
int getDuration()
Gets the duration of the file.
boolean isLooping()
Checks whether the MediaPlayer is looping or non-looping.
boolean isPlaying()
Checks whether the MediaPlayer is playing.
void pause()
Pauses playback.
void prepare()
Prepares the player for playback, synchronously.
void prepareAsync()
Prepares the player for playback, asynchronously.
void release()
Releases resources associated with this MediaPlayer object.
void reset()
Resets the MediaPlayer to its uninitialized state.
void seekTo(int msec)
Seeks to specified time position.
void setAudioAttributes(AudioAttributes attributes)
Sets the audio attributes for this MediaPlayer.
void setAudioSessionId(int sessionId)
Sets the audio session ID.
void setAudioStreamType(int streamtype)
Sets the audio stream type for this MediaPlayer.
void setAuxEffectSendLevel(float level)
Sets the send level of the player to the attached auxiliary effect.
void setDataSource(FileDescriptor fd)
Sets the data source (FileDescriptor) to use.
void setDataSource(FileDescriptor fd, long offset, long length)
Sets the data source (FileDescriptor) to use.
void setDataSource(String path)
Sets the data source (file-path or http/rtsp URL) to use.
void setDataSource(Context context, Uri uri)
Sets the data source as a content Uri.
void setLooping(boolean looping)
Sets the player to be looping or non-looping.
void setNextMediaPlayer(IBasicMediaPlayer next)
Set the MediaPlayer to start when this MediaPlayer finishes playback (i.e.
void setOnBufferingUpdateListener(IBasicMediaPlayer.OnBufferingUpdateListener listener)
Register a callback to be invoked when the status of a network stream's buffer has changed.
void setOnCompletionListener(IBasicMediaPlayer.OnCompletionListener listener)
Register a callback to be invoked when the end of a media source has been reached during playback.
void setOnErrorListener(IBasicMediaPlayer.OnErrorListener listener)
Register a callback to be invoked when an error has happened during an asynchronous operation.
void setOnInfoListener(IBasicMediaPlayer.OnInfoListener listener)
Register a callback to be invoked when an info/warning is available.
void setOnPreparedListener(IBasicMediaPlayer.OnPreparedListener listener)
Register a callback to be invoked when the media source is ready for playback.
void setOnSeekCompleteListener(IBasicMediaPlayer.OnSeekCompleteListener listener)
Register a callback to be invoked when a seek operation has been completed.
void setVolume(float leftVolume, float rightVolume)
Sets the volume on this player.
void setWakeMode(Context context, int mode)
Set the low-level power management behavior for this MediaPlayer.
void start()
Starts or resumes playback.
void stop()
Stops playback after playback has been stopped or paused.
Protected Methods
void finalize()
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.h6ah4i.android.media.IBasicMediaPlayer
From interface com.h6ah4i.android.media.IReleasable

Constants

public static final int OPTION_USE_FADE

Constant Value: 1 (0x00000001)

Public Constructors

public OpenSLMediaPlayer (OpenSLMediaPlayerContext context, int options)

Public Methods

public void attachAuxEffect (int effectId)

Attaches an auxiliary effect to the player. A typical auxiliary effect is a reverberation effect which can be applied on any sound source that directs a certain amount of its energy to this effect. This amount is defined by setAuxEffectSendLevel().

After creating an auxiliary effect (e.g. android.media.audiofx.EnvironmentalReverb), retrieve its ID with getId() and use it when calling this method to attach the player to the effect.

To detach the effect from the player, call this method with a null effect id.

This method must be called after one of the overloaded setDataSource methods.

Parameters
effectId system wide unique id of the effect to attach
Throws
IllegalArgumentException
IllegalStateException

public int getAudioSessionId ()

Returns the audio session ID.

Returns
  • the audio session ID. Note that the audio session ID is 0 only if a problem occurred when the MediaPlayer was contructed.

public int getCurrentPosition ()

Gets the current playback position.

Returns
  • the current position in milliseconds

public int getDuration ()

Gets the duration of the file.

Returns
  • the duration in milliseconds, if no duration is available (for example, if streaming live content), -1 is returned.

public boolean isLooping ()

Checks whether the MediaPlayer is looping or non-looping.

Returns
  • true if the MediaPlayer is currently looping, false otherwise

public boolean isPlaying ()

Checks whether the MediaPlayer is playing.

Returns
  • true if currently playing, false otherwise
Throws
IllegalStateException

public void pause ()

Pauses playback. Call start() to resume.

Throws
IllegalStateException

public void prepare ()

Prepares the player for playback, synchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For files, it is OK to call prepare(), which blocks until MediaPlayer is ready for playback.

Throws
IOException
IllegalStateException

public void prepareAsync ()

Prepares the player for playback, asynchronously. After setting the datasource and the display surface, you need to either call prepare() or prepareAsync(). For streams, you should call prepareAsync(), which returns immediately, rather than blocking until enough data has been buffered.

Throws
IllegalStateException

public void release ()

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around. In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time.

public void reset ()

Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().

public void seekTo (int msec)

Seeks to specified time position.

Parameters
msec the offset in milliseconds from the start to seek to
Throws
IllegalStateException

public void setAudioAttributes (AudioAttributes attributes)

Sets the audio attributes for this MediaPlayer. See AudioAttributes for how to build and configure an instance of this class. You must call this method before prepare() or prepareAsync() in order for the audio attributes to become effective thereafter.

Parameters
attributes a non-null set of audio attributes

public void setAudioSessionId (int sessionId)

Sets the audio session ID.

Parameters
sessionId the audio session ID. The audio session ID is a system wide unique identifier for the audio stream played by this MediaPlayer instance. The primary use of the audio session ID is to associate audio effects to a particular instance of MediaPlayer: if an audio session ID is provided when creating an audio effect, this effect will be applied only to the audio content of media players within the same audio session and not to the output mix. When created, a MediaPlayer instance automatically generates its own audio session ID. However, it is possible to force this player to be part of an already existing audio session by calling this method. This method must be called before one of the overloaded setDataSource methods.
Throws
IllegalArgumentException
IllegalStateException

public void setAudioStreamType (int streamtype)

Sets the audio stream type for this MediaPlayer. See AudioManager for a list of stream types. Must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.

Parameters
streamtype the audio stream type

public void setAuxEffectSendLevel (float level)

Sets the send level of the player to the attached auxiliary effect. The level value range is 0 to 1.0.

By default the send level is 0, so even if an effect is attached to the player this method must be called for the effect to be applied.

Note that the passed level value is a raw scalar. UI controls should be scaled logarithmically: the gain applied by audio framework ranges from -72dB to 0dB, so an appropriate conversion from linear UI input x to level is: x == 0 -> level = 0 0 < x <= R -> level = 10^(72*(x-R)/20/R)

Parameters
level send level scalar

public void setDataSource (FileDescriptor fd)

Sets the data source (FileDescriptor) to use. It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

Parameters
fd the FileDescriptor for the file you want to play
Throws
IOException
IllegalArgumentException
IllegalStateException

public void setDataSource (FileDescriptor fd, long offset, long length)

Sets the data source (FileDescriptor) to use. The FileDescriptor must be seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility to close the file descriptor. It is safe to do so as soon as this call returns.

Parameters
fd the FileDescriptor for the file you want to play
offset the offset into the file where the data to be played starts, in bytes
length the length in bytes of the data to be played
Throws
IOException
IllegalArgumentException
IllegalStateException

public void setDataSource (String path)

Sets the data source (file-path or http/rtsp URL) to use.

Parameters
path the path of the file, or the http/rtsp URL of the stream you want to play
Throws
IOException
IllegalArgumentException
IllegalStateException

public void setDataSource (Context context, Uri uri)

Sets the data source as a content Uri.

Parameters
context the Context to use when resolving the Uri
uri the Content URI of the data you want to play
Throws
IOException
IllegalArgumentException
IllegalStateException
SecurityException

public void setLooping (boolean looping)

Sets the player to be looping or non-looping.

Parameters
looping whether to loop or not

public void setNextMediaPlayer (IBasicMediaPlayer next)

Set the MediaPlayer to start when this MediaPlayer finishes playback (i.e. reaches the end of the stream). The media framework will attempt to transition from this player to the next as seamlessly as possible. The next player can be set at any time before completion. The next player must be prepared by the app, and the application should not call start() on it. The next MediaPlayer must be different from 'this'. An exception will be thrown if next == this. The application may call setNextMediaPlayer(null) to indicate no next player should be started at the end of playback. If the current player is looping, it will keep looping and the next player will not be started.

Parameters
next the player to start after this one completes playback.

public void setOnBufferingUpdateListener (IBasicMediaPlayer.OnBufferingUpdateListener listener)

Register a callback to be invoked when the status of a network stream's buffer has changed.

Parameters
listener the callback that will be run.

public void setOnCompletionListener (IBasicMediaPlayer.OnCompletionListener listener)

Register a callback to be invoked when the end of a media source has been reached during playback.

Parameters
listener the callback that will be run

public void setOnErrorListener (IBasicMediaPlayer.OnErrorListener listener)

Register a callback to be invoked when an error has happened during an asynchronous operation.

Parameters
listener the callback that will be run

public void setOnInfoListener (IBasicMediaPlayer.OnInfoListener listener)

Register a callback to be invoked when an info/warning is available.

Parameters
listener the callback that will be run

public void setOnPreparedListener (IBasicMediaPlayer.OnPreparedListener listener)

Register a callback to be invoked when the media source is ready for playback.

Parameters
listener the callback that will be run

public void setOnSeekCompleteListener (IBasicMediaPlayer.OnSeekCompleteListener listener)

Register a callback to be invoked when a seek operation has been completed.

Parameters
listener the callback that will be run

public void setVolume (float leftVolume, float rightVolume)

Sets the volume on this player. This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. Note that the passed volume values are raw scalars in range 0.0 to 1.0. UI controls should be scaled logarithmically.

Parameters
leftVolume left volume scalar
rightVolume right volume scalar

public void setWakeMode (Context context, int mode)

Set the low-level power management behavior for this MediaPlayer.

This function has the MediaPlayer access the low-level power manager service to control the device's power usage while playing is occurring. The parameter is a combination of android.os.PowerManager wake flags. Use of this method requires WAKE_LOCK permission. By default, no attempt is made to keep the device awake during playback.

Parameters
context the Context to use
mode the power/wake mode to set

public void start ()

Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.

Throws
IllegalStateException

public void stop ()

Stops playback after playback has been stopped or paused.

Throws
IllegalStateException

Protected Methods

protected void finalize ()

Throws
Throwable