Package org.videolan.libvlc
Class MediaPlayer
- java.lang.Object
-
- org.videolan.libvlc.MediaPlayer
-
- All Implemented Interfaces:
IVLCObject<MediaPlayer.Event>
public class MediaPlayer extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MediaPlayer.Chapter
static class
MediaPlayer.Equalizer
static class
MediaPlayer.Event
static interface
MediaPlayer.EventListener
static class
MediaPlayer.Navigate
static class
MediaPlayer.Position
static class
MediaPlayer.ScaleType
static class
MediaPlayer.Title
static class
MediaPlayer.TrackDescription
-
Field Summary
Fields Modifier and Type Field Description static int
SURFACE_SCALES_COUNT
-
Constructor Summary
Constructors Constructor Description MediaPlayer(ILibVLC ILibVLC)
Create an empty MediaPlayerMediaPlayer(IMedia media)
Create a MediaPlayer from a Media
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addSlave(int type, Uri uri, boolean select)
Add a slave (or subtitle) to the current media player.boolean
addSlave(int type, String path, boolean select)
Add a slave (or subtitle) to the current media player.void
attachViews(VLCVideoLayout surfaceFrame, DisplayManager dm, boolean subtitles, boolean textureView)
Attach a video layout to the playerboolean
canDoPassthrough()
void
detachViews()
Detach the video layoutprotected void
finalize()
boolean
forceAudioDigitalEncodings(int[] encodings)
Convenient method forsetAudioOutputDevice(java.lang.String)
String
getAspectRatio()
Get current video aspect ratiolong
getAudioDelay()
Get the current audio delay.int
getAudioTrack()
Get the current audio track.MediaPlayer.TrackDescription[]
getAudioTracks()
Get the list of available audio tracks.int
getAudioTracksCount()
Get the number of available audio tracks.int
getChapter()
MediaPlayer.Chapter[]
getChapters(int title)
Get the full description of available chapters.IMedia.VideoTrack
getCurrentVideoTrack()
Get the current video tracklong
getInstance()
long
getLength()
Gets current movie's length in ms.ILibVLC
getLibVLC()
IMedia
getMedia()
Get the Media used by this MediaPlayer.int
getPlayerState()
Get player state.float
getPosition()
Gets the movie position.float
getRate()
Get the current playback speedfloat
getScale()
Get the current video scaling factorlong
getSpuDelay()
Get the current spu (subtitle) delay.int
getSpuTrack()
Get the current spu (subtitle) track.MediaPlayer.TrackDescription[]
getSpuTracks()
Get the list of available spu (subtitle) tracks.int
getSpuTracksCount()
Get the number of available spu (subtitle) tracks.long
getTime()
Gets the current movie time (in ms).int
getTitle()
MediaPlayer.Title[]
getTitles()
Get the full description of available titles.MediaPlayer.ScaleType
getVideoScale()
Get the current video scale typeint
getVideoTrack()
Get the current video track.MediaPlayer.TrackDescription[]
getVideoTracks()
Get the list of available video tracks.int
getVideoTracksCount()
Get the number of available video tracks.IVLCVout
getVLCVout()
Get the IVLCVout helper.int
getVolume()
Gets volume as integerboolean
hasMedia()
Is a media in use by this MediaPlayerboolean
isPlaying()
Returns true if any media is playingboolean
isReleased()
Returns true if native object is releasedboolean
isSeekable()
Returns true if any media is seekablevoid
nativeSetPosition(float pos, boolean fast)
long
nativeSetTime(long time, boolean fast)
void
navigate(int navigate)
int
nextChapter()
protected MediaPlayer.Event
onEventNative(int eventType, long arg1, long arg2, float argf1, String args1)
Called when libvlc send events.protected void
onReleaseNative()
Called when native object is released (refcount is 0).void
pause()
Pauses any playing mediavoid
play()
Play the mediavoid
play(AssetFileDescriptor afd)
Load an asset and starts playbackvoid
play(Uri uri)
Play a media via its Urivoid
play(String path)
Play a media via its mrlvoid
play(IMedia media)
Starts playback from an already prepared Mediavoid
playAsset(Context context, String assetFilename)
Load an asset and starts playbackint
previousChapter()
boolean
record(String directory)
Start/stop recordingvoid
release()
Release the native object if ref count is 1.boolean
retain()
Increment internal ref count of the native object.void
setAspectRatio(String aspect)
Set new video aspect ratio.boolean
setAudioDelay(long delay)
Set current audio delay.boolean
setAudioDigitalOutputEnabled(boolean enabled)
Enable or disable Digital Output Works only with AudioTrack AudioOutput.boolean
setAudioOutput(String aout)
Selects an audio output module.boolean
setAudioOutputDevice(String id)
Configures an explicit audio output device.boolean
setAudioTrack(int index)
Set the audio track.void
setChapter(int chapter)
boolean
setEqualizer(MediaPlayer.Equalizer equalizer)
Apply new equalizer settings to a media player.protected void
setEventListener(AbstractVLCEvent.Listener<T> listener)
Set an event listener.protected void
setEventListener(AbstractVLCEvent.Listener<T> listener, Handler handler)
Set an event listener and an executor Handlervoid
setEventListener(MediaPlayer.EventListener listener)
void
setMedia(IMedia media)
Set a Mediavoid
setPosition(float pos)
void
setPosition(float pos, boolean fast)
Sets the movie position.void
setRate(float rate)
Sets the speed of playback (1 being normal speed, 2 being twice as fast)int
setRenderer(RendererItem item)
Set a renderervoid
setScale(float scale)
Set the video scaling factor That is the ratio of the number of pixels on screen to the number of pixels in the original decoded video in each dimension.boolean
setSpuDelay(long delay)
Set current spu (subtitle) delay.boolean
setSpuTrack(int index)
Set the spu (subtitle) track.long
setTime(long time)
long
setTime(long time, boolean fast)
Sets the movie time (in ms), if any media is being played.void
setTitle(int title)
void
setUseOrientationFromBounds(Boolean mUseOrientationFromBounds)
void
setVideoScale(MediaPlayer.ScaleType type)
Set the video scale type, by default, scaletype is set to ScaleType.SURFACE_BEST_FITvoid
setVideoTitleDisplay(int position, int timeout)
Set if, and how, the video title will be shown when media is playedboolean
setVideoTrack(int index)
Set the video track.void
setVideoTrackEnabled(boolean enabled)
Set the enabled state of the video trackint
setVolume(int volume)
Sets volume as integervoid
stop()
Stops the playing mediavoid
updateVideoSurfaces()
Update the video surfaces, either to switch from one to another or to resize itboolean
updateViewpoint(float yaw, float pitch, float roll, float fov, boolean absolute)
Update the video viewpoint informationBoolean
useOrientationFromBounds()
-
-
-
Method Detail
-
getVLCVout
@NonNull public IVLCVout getVLCVout()
Get the IVLCVout helper.
-
attachViews
public void attachViews(@NonNull VLCVideoLayout surfaceFrame, @Nullable DisplayManager dm, boolean subtitles, boolean textureView)
Attach a video layout to the player- Parameters:
surfaceFrame
-VLCVideoLayout
in which the video will be displayeddm
- OptionalDisplayManager
to help switch between renderers, primary and secondary displayssubtitles
- Whether you wish to show subtitlestextureView
- If true,VLCVideoLayout
will use aTextureView
instead of aSurfaceView
-
detachViews
public void detachViews()
Detach the video layout
-
updateVideoSurfaces
public void updateVideoSurfaces()
Update the video surfaces, either to switch from one to another or to resize it
-
setVideoScale
public void setVideoScale(@NonNull MediaPlayer.ScaleType type)
Set the video scale type, by default, scaletype is set to ScaleType.SURFACE_BEST_FIT
-
getVideoScale
@NonNull public MediaPlayer.ScaleType getVideoScale()
Get the current video scale type- Returns:
- the current
MediaPlayer.ScaleType
used by MediaPlayer
-
setMedia
public void setMedia(@Nullable IMedia media)
Set a Media- Parameters:
media
- a valid Media object
-
setRenderer
public int setRenderer(@Nullable RendererItem item)
Set a renderer- Parameters:
item
-RendererItem
. if null VLC play on default output
-
hasMedia
public boolean hasMedia()
Is a media in use by this MediaPlayer- Returns:
- true if a media is set
-
getMedia
@Nullable public IMedia getMedia()
Get the Media used by this MediaPlayer. This Media should be released withIVLCObject.release()
.
-
play
public void play()
Play the media
-
playAsset
public void playAsset(@NonNull Context context, @NonNull String assetFilename) throws IOException
Load an asset and starts playback- Parameters:
context
- An application context, mandatory to access assetsassetFilename
- relative path of the asset in app assets folder- Throws:
IOException
-
play
public void play(@NonNull AssetFileDescriptor afd)
Load an asset and starts playback- Parameters:
afd
- TheAssetFileDescriptor
to play
-
play
public void play(@NonNull String path)
Play a media via its mrl- Parameters:
path
- Path of the media file to play
-
play
public void play(@NonNull Uri uri)
Play a media via its Uri- Parameters:
uri
-Uri
of the media to play
-
play
public void play(@NonNull IMedia media)
Starts playback from an already prepared Media- Parameters:
media
- TheIMedia
to play
-
stop
public void stop()
Stops the playing media
-
setVideoTitleDisplay
public void setVideoTitleDisplay(int position, int timeout)
Set if, and how, the video title will be shown when media is played- Parameters:
position
- seeMediaPlayer.Position
timeout
-
-
getScale
public float getScale()
Get the current video scaling factor- Returns:
- the currently configured zoom factor, or 0. if the video is set to fit to the output window/drawable automatically.
-
setScale
public void setScale(float scale)
Set the video scaling factor That is the ratio of the number of pixels on screen to the number of pixels in the original decoded video in each dimension. Zero is a special value; it will adjust the video to the output window/drawable (in windowed mode) or the entire screen.- Parameters:
scale
- the scaling factor, or zero
-
getAspectRatio
public String getAspectRatio()
Get current video aspect ratio- Returns:
- the video aspect ratio or NULL if unspecified
-
setAspectRatio
public void setAspectRatio(String aspect)
Set new video aspect ratio.- Parameters:
aspect
- new video aspect-ratio or NULL to reset to default
-
updateViewpoint
public boolean updateViewpoint(float yaw, float pitch, float roll, float fov, boolean absolute)
Update the video viewpoint information- Parameters:
yaw
- View point yaw in degreespitch
- View point pitch in degreesroll
- View point roll in degreesfov
- Field of view in degrees (default 80.0f)absolute
- if true replace the old viewpoint with the new one. If false, increase/decrease it.- Returns:
- true on success.
-
setAudioOutput
public boolean setAudioOutput(String aout)
Selects an audio output module. Any change will take effect only after playback is stopped and restarted. Audio output cannot be changed while playing. By default, the "android_audiotrack" is selected. Starting Android 21, passthrough is enabled for encodings supported by the device/audio system. Calling this method will disable the encoding detection.- Returns:
- true on success.
-
setAudioDigitalOutputEnabled
public boolean setAudioDigitalOutputEnabled(boolean enabled)
Enable or disable Digital Output Works only with AudioTrack AudioOutput. IfsetAudioOutputDevice(java.lang.String)
was previously called, this method won't have any effects.- Parameters:
enabled
- true to enable Digital Output- Returns:
- true on success
-
forceAudioDigitalEncodings
public boolean forceAudioDigitalEncodings(int[] encodings)
Convenient method forsetAudioOutputDevice(java.lang.String)
- Parameters:
encodings
- list of encodings to play via passthrough (see AudioFormat.ENCODING_*), null to don't force any.- Returns:
- true on success
-
setUseOrientationFromBounds
public void setUseOrientationFromBounds(Boolean mUseOrientationFromBounds)
-
useOrientationFromBounds
public Boolean useOrientationFromBounds()
-
setAudioOutputDevice
public boolean setAudioOutputDevice(String id)
Configures an explicit audio output device. Audio output will be moved to the device specified by the device identifier string. Available devices for the "android_audiotrack" module (the default) are "stereo": Up to 2 channels (compat mode). "pcm": Up to 8 channels. "encoded": Up to 8 channels, passthrough for every encodings if available. "encoded:ENCODING_FLAGS_MASK": passthrough for every encodings specified by ENCODING_FLAGS_MASK. This extra value is a long that contains binary-shifted AudioFormat.ENCODING_* values. Calling this method will disable the encoding detection (seesetAudioOutput(java.lang.String)
andsetAudioDigitalOutputEnabled(boolean)
).- Returns:
- true on success.
-
getTitles
public MediaPlayer.Title[] getTitles()
Get the full description of available titles.- Returns:
- the list of titles
-
getChapters
public MediaPlayer.Chapter[] getChapters(int title)
Get the full description of available chapters.- Parameters:
title
- index of the title (if -1, use the current title)- Returns:
- the list of Chapters for the title
-
getVideoTracksCount
public int getVideoTracksCount()
Get the number of available video tracks.
-
getVideoTracks
public MediaPlayer.TrackDescription[] getVideoTracks()
Get the list of available video tracks.
-
getVideoTrack
public int getVideoTrack()
Get the current video track.- Returns:
- the video track ID or -1 if no active input
-
setVideoTrack
public boolean setVideoTrack(int index)
Set the video track.- Returns:
- true on success.
-
setVideoTrackEnabled
public void setVideoTrackEnabled(boolean enabled)
Set the enabled state of the video track- Parameters:
enabled
-
-
getCurrentVideoTrack
public IMedia.VideoTrack getCurrentVideoTrack()
Get the current video track
-
getAudioTracksCount
public int getAudioTracksCount()
Get the number of available audio tracks.
-
getAudioTracks
public MediaPlayer.TrackDescription[] getAudioTracks()
Get the list of available audio tracks.
-
getAudioTrack
public int getAudioTrack()
Get the current audio track.- Returns:
- the audio track ID or -1 if no active input
-
setAudioTrack
public boolean setAudioTrack(int index)
Set the audio track.- Returns:
- true on success.
-
getAudioDelay
public long getAudioDelay()
Get the current audio delay.- Returns:
- delay in microseconds.
-
setAudioDelay
public boolean setAudioDelay(long delay)
Set current audio delay. The audio delay will be reset to zero each time the media changes.- Parameters:
delay
- in microseconds.- Returns:
- true on success.
-
getSpuTracksCount
public int getSpuTracksCount()
Get the number of available spu (subtitle) tracks.
-
getSpuTracks
public MediaPlayer.TrackDescription[] getSpuTracks()
Get the list of available spu (subtitle) tracks.
-
getSpuTrack
public int getSpuTrack()
Get the current spu (subtitle) track.- Returns:
- the spu (subtitle) track ID or -1 if no active input
-
setSpuTrack
public boolean setSpuTrack(int index)
Set the spu (subtitle) track.- Returns:
- true on success.
-
getSpuDelay
public long getSpuDelay()
Get the current spu (subtitle) delay.- Returns:
- delay in microseconds.
-
setSpuDelay
public boolean setSpuDelay(long delay)
Set current spu (subtitle) delay. The spu delay will be reset to zero each time the media changes.- Parameters:
delay
- in microseconds.- Returns:
- true on success.
-
setEqualizer
public boolean setEqualizer(MediaPlayer.Equalizer equalizer)
Apply new equalizer settings to a media player. The equalizer is first created by invokingMediaPlayer.Equalizer.create()
orMediaPlayer.Equalizer.createFromPreset(int)
}. It is possible to apply new equalizer settings to a media player whether the media player is currently playing media or not. Invoking this method will immediately apply the new equalizer settings to the audio output of the currently playing media if there is any. If there is no currently playing media, the new equalizer settings will be applied later if and when new media is played. Equalizer settings will automatically be applied to subsequently played media. To disable the equalizer for a media player invoke this method passing null.- Returns:
- true on success.
-
addSlave
public boolean addSlave(int type, Uri uri, boolean select)
Add a slave (or subtitle) to the current media player.- Parameters:
type
- seeIMedia.Slave.Type
uri
- a valid RFC 2396 Uri- Returns:
- true on success.
-
record
public boolean record(String directory)
Start/stop recording- Parameters:
directory
- path of the recording directory or null to stop recording- Returns:
- true on success.
-
addSlave
public boolean addSlave(int type, String path, boolean select)
Add a slave (or subtitle) to the current media player.- Parameters:
type
- seeIMedia.Slave.Type
path
- a local path- Returns:
- true on success.
-
setRate
public void setRate(float rate)
Sets the speed of playback (1 being normal speed, 2 being twice as fast)- Parameters:
rate
-
-
getRate
public float getRate()
Get the current playback speed
-
isPlaying
public boolean isPlaying()
Returns true if any media is playing
-
isSeekable
public boolean isSeekable()
Returns true if any media is seekable
-
pause
public void pause()
Pauses any playing media
-
getPlayerState
public int getPlayerState()
Get player state.
-
getVolume
public int getVolume()
Gets volume as integer
-
setVolume
public int setVolume(int volume)
Sets volume as integer- Parameters:
volume
- : Volume level passed as integer
-
getTime
public long getTime()
Gets the current movie time (in ms).- Returns:
- the movie time (in ms), or -1 if there is no media.
-
setTime
public long setTime(long time, boolean fast)
Sets the movie time (in ms), if any media is being played.- Parameters:
time
- : Time in ms.fast
- : Prefer fast seeking or precise seeking- Returns:
- the movie time (in ms), or -1 if there is no media.
-
setTime
public long setTime(long time)
-
getPosition
public float getPosition()
Gets the movie position.- Returns:
- the movie position, or -1 for any error.
-
setPosition
public void setPosition(float pos, boolean fast)
Sets the movie position.- Parameters:
pos
- : movie position.fast
- : Prefer fast seeking or precise seeking
-
setPosition
public void setPosition(float pos)
-
getLength
public long getLength()
Gets current movie's length in ms.- Returns:
- the movie length (in ms), or -1 if there is no media.
-
getTitle
public int getTitle()
-
setTitle
public void setTitle(int title)
-
getChapter
public int getChapter()
-
previousChapter
public int previousChapter()
-
nextChapter
public int nextChapter()
-
setChapter
public void setChapter(int chapter)
-
navigate
public void navigate(int navigate)
-
setEventListener
public void setEventListener(MediaPlayer.EventListener listener)
-
onEventNative
protected MediaPlayer.Event onEventNative(int eventType, long arg1, long arg2, float argf1, @Nullable String args1)
Called when libvlc send events.- Parameters:
eventType
- event typearg1
- first argumentarg2
- second argumentargf1
- first float argument- Returns:
- Event that will be dispatched to listeners
-
onReleaseNative
protected void onReleaseNative()
Called when native object is released (refcount is 0). This is where you must release native resources.
-
canDoPassthrough
public boolean canDoPassthrough()
-
nativeSetTime
public long nativeSetTime(long time, boolean fast)
-
nativeSetPosition
public void nativeSetPosition(float pos, boolean fast)
-
isReleased
public boolean isReleased()
Returns true if native object is released- Specified by:
isReleased
in interfaceIVLCObject<T extends AbstractVLCEvent>
-
retain
public final boolean retain()
Increment internal ref count of the native object.- Specified by:
retain
in interfaceIVLCObject<T extends AbstractVLCEvent>
- Returns:
- true if media is retained
-
release
public final void release()
Release the native object if ref count is 1. After this call, native calls are not possible anymore. You can still call others methods to retrieve cached values. For example: if you parse, then release a media, you'll still be able to retrieve all Metas or Tracks infos.- Specified by:
release
in interfaceIVLCObject<T extends AbstractVLCEvent>
-
getLibVLC
public ILibVLC getLibVLC()
- Specified by:
getLibVLC
in interfaceIVLCObject<T extends AbstractVLCEvent>
-
setEventListener
protected void setEventListener(AbstractVLCEvent.Listener<T> listener)
Set an event listener. Events are sent via the android main thread.- Parameters:
listener
- seeAbstractVLCEvent.Listener
-
setEventListener
protected void setEventListener(AbstractVLCEvent.Listener<T> listener, Handler handler)
Set an event listener and an executor Handler- Parameters:
listener
- seeAbstractVLCEvent.Listener
handler
- Handler in which events are sent. If null, a handler will be created running on the main thread
-
getInstance
public long getInstance()
-
-