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 classMediaPlayer.Chapterstatic classMediaPlayer.Equalizerstatic classMediaPlayer.Eventstatic interfaceMediaPlayer.EventListenerstatic classMediaPlayer.Navigatestatic classMediaPlayer.Positionstatic classMediaPlayer.ScaleTypestatic classMediaPlayer.Titlestatic classMediaPlayer.TrackDescription
-
Field Summary
Fields Modifier and Type Field Description static intSURFACE_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 booleanaddSlave(int type, Uri uri, boolean select)Add a slave (or subtitle) to the current media player.booleanaddSlave(int type, String path, boolean select)Add a slave (or subtitle) to the current media player.voidattachViews(VLCVideoLayout surfaceFrame, DisplayManager dm, boolean subtitles, boolean textureView)Attach a video layout to the playerbooleancanDoPassthrough()voiddetachViews()Detach the video layoutprotected voidfinalize()booleanforceAudioDigitalEncodings(int[] encodings)Convenient method forsetAudioOutputDevice(java.lang.String)StringgetAspectRatio()Get current video aspect ratiolonggetAudioDelay()Get the current audio delay.intgetAudioTrack()Get the current audio track.MediaPlayer.TrackDescription[]getAudioTracks()Get the list of available audio tracks.intgetAudioTracksCount()Get the number of available audio tracks.intgetChapter()MediaPlayer.Chapter[]getChapters(int title)Get the full description of available chapters.IMedia.VideoTrackgetCurrentVideoTrack()Get the current video tracklonggetInstance()longgetLength()Gets current movie's length in ms.ILibVLCgetLibVLC()IMediagetMedia()Get the Media used by this MediaPlayer.intgetPlayerState()Get player state.floatgetPosition()Gets the movie position.floatgetRate()Get the current playback speedfloatgetScale()Get the current video scaling factorlonggetSpuDelay()Get the current spu (subtitle) delay.intgetSpuTrack()Get the current spu (subtitle) track.MediaPlayer.TrackDescription[]getSpuTracks()Get the list of available spu (subtitle) tracks.intgetSpuTracksCount()Get the number of available spu (subtitle) tracks.longgetTime()Gets the current movie time (in ms).intgetTitle()MediaPlayer.Title[]getTitles()Get the full description of available titles.MediaPlayer.ScaleTypegetVideoScale()Get the current video scale typeintgetVideoTrack()Get the current video track.MediaPlayer.TrackDescription[]getVideoTracks()Get the list of available video tracks.intgetVideoTracksCount()Get the number of available video tracks.IVLCVoutgetVLCVout()Get the IVLCVout helper.intgetVolume()Gets volume as integerbooleanhasMedia()Is a media in use by this MediaPlayerbooleanisPlaying()Returns true if any media is playingbooleanisReleased()Returns true if native object is releasedbooleanisSeekable()Returns true if any media is seekablevoidnativeSetPosition(float pos, boolean fast)longnativeSetTime(long time, boolean fast)voidnavigate(int navigate)intnextChapter()protected MediaPlayer.EventonEventNative(int eventType, long arg1, long arg2, float argf1, String args1)Called when libvlc send events.protected voidonReleaseNative()Called when native object is released (refcount is 0).voidpause()Pauses any playing mediavoidplay()Play the mediavoidplay(AssetFileDescriptor afd)Load an asset and starts playbackvoidplay(Uri uri)Play a media via its Urivoidplay(String path)Play a media via its mrlvoidplay(IMedia media)Starts playback from an already prepared MediavoidplayAsset(Context context, String assetFilename)Load an asset and starts playbackintpreviousChapter()booleanrecord(String directory)Start/stop recordingvoidrelease()Release the native object if ref count is 1.booleanretain()Increment internal ref count of the native object.voidsetAspectRatio(String aspect)Set new video aspect ratio.booleansetAudioDelay(long delay)Set current audio delay.booleansetAudioDigitalOutputEnabled(boolean enabled)Enable or disable Digital Output Works only with AudioTrack AudioOutput.booleansetAudioOutput(String aout)Selects an audio output module.booleansetAudioOutputDevice(String id)Configures an explicit audio output device.booleansetAudioTrack(int index)Set the audio track.voidsetChapter(int chapter)booleansetEqualizer(MediaPlayer.Equalizer equalizer)Apply new equalizer settings to a media player.protected voidsetEventListener(AbstractVLCEvent.Listener<T> listener)Set an event listener.protected voidsetEventListener(AbstractVLCEvent.Listener<T> listener, Handler handler)Set an event listener and an executor HandlervoidsetEventListener(MediaPlayer.EventListener listener)voidsetMedia(IMedia media)Set a MediavoidsetPosition(float pos)voidsetPosition(float pos, boolean fast)Sets the movie position.voidsetRate(float rate)Sets the speed of playback (1 being normal speed, 2 being twice as fast)intsetRenderer(RendererItem item)Set a renderervoidsetScale(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.booleansetSpuDelay(long delay)Set current spu (subtitle) delay.booleansetSpuTrack(int index)Set the spu (subtitle) track.longsetTime(long time)longsetTime(long time, boolean fast)Sets the movie time (in ms), if any media is being played.voidsetTitle(int title)voidsetVideoScale(MediaPlayer.ScaleType type)Set the video scale type, by default, scaletype is set to ScaleType.SURFACE_BEST_FITvoidsetVideoTitleDisplay(int position, int timeout)Set if, and how, the video title will be shown when media is playedbooleansetVideoTrack(int index)Set the video track.voidsetVideoTrackEnabled(boolean enabled)Set the enabled state of the video trackintsetVolume(int volume)Sets volume as integervoidstop()Stops the playing mediavoidupdateVideoSurfaces()Update the video surfaces, either to switch from one to another or to resize itbooleanupdateViewpoint(float yaw, float pitch, float roll, float fov, boolean absolute)Update the video viewpoint information
-
-
-
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-VLCVideoLayoutin which the video will be displayeddm- OptionalDisplayManagerto help switch between renderers, primary and secondary displayssubtitles- Whether you wish to show subtitlestextureView- If true,VLCVideoLayoutwill use aTextureViewinstead 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.ScaleTypeused 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 IOExceptionLoad 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- TheAssetFileDescriptorto 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-Uriof the media to play
-
play
public void play(@NonNull IMedia media)Starts playback from an already prepared Media- Parameters:
media- TheIMediato 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.Positiontimeout-
-
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
-
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.Typeuri- 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.Typepath- 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:
isReleasedin interfaceIVLCObject<T extends AbstractVLCEvent>
-
retain
public final boolean retain()
Increment internal ref count of the native object.- Specified by:
retainin 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:
releasein interfaceIVLCObject<T extends AbstractVLCEvent>
-
getLibVLC
public ILibVLC getLibVLC()
- Specified by:
getLibVLCin 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.Listenerhandler- Handler in which events are sent. If null, a handler will be created running on the main thread
-
getInstance
public long getInstance()
-
-