public class Player extends Object
A Player instance can play or loop one or more media files. Example code:
try (FileInputStream in = new FileInputStream(new File(PLAYER_TEST_FILE))) {
FileDescriptor fd = in.getFD();
Source source = new Source(fd);
Player impl = new Player();
impl.setSource(source);
impl.prepare();
impl.play();
} catch (IOException e) {
// ...
}
SystemCapability.Multimedia#MEDIA| 限定符和类型 | 类 | 说明 |
|---|---|---|
static class |
Player.AudioHandler |
Enumerates handling modes for audio parameters whose values are out of common ranges.
|
static class |
Player.DeviceInfo |
Provides information about a device that plays an audio or video file.
|
static interface |
Player.IPlayerCallback |
Provides media player callbacks upon completion of playback, playback position changes, and video size changes.
|
static class |
Player.MediaTimedMetaData |
Represents media timed metadata.
|
static class |
Player.MediaTimeInfo |
Represents media time information.
|
static class |
Player.StreamInfo |
Provides descriptions of the media stream metadata contained in media files.
|
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
PLAYER_ERROR_IO |
Indicates a media file I/O error.
|
static int |
PLAYER_ERROR_MALFORMED |
Indicates an invalid file format.
|
static int |
PLAYER_ERROR_SERVER_DIED |
Indicates a media playback service is error.
|
static int |
PLAYER_ERROR_SYSTEM |
Indicates an unknown system error.
|
static int |
PLAYER_ERROR_TIMED_OUT |
Indicates playback timeout.
|
static int |
PLAYER_ERROR_UNKNOWN |
Indicates an unknown playback error.
|
static int |
PLAYER_ERROR_UNSUPPORTED |
Indicates an unsupported media type.
|
static int |
PLAYER_INFO_AUDIO_NOT_PLAYING |
Indicates that the audio playback has stopped, but the video is still playing.
|
static int |
PLAYER_INFO_BAD_INTERLEAVING |
Indicates that media is incorrectly interleaved or not interleaved.
|
static int |
PLAYER_INFO_BUFFERING_END |
Indicates that the player resumes after the buffer is filled.
|
static int |
PLAYER_INFO_BUFFERING_START |
Indicates that the player pauses to buffer more data.
|
static int |
PLAYER_INFO_EXTERNAL_METADATA_UPDATE |
Indicates that a new set of metadata is only externally available.
|
static int |
PLAYER_INFO_METADATA_UPDATE |
Indicates that a new set of metadata is available.
|
static int |
PLAYER_INFO_NETWORK_BANDWIDTH |
Indicates that the estimated network bandwidth information is available.
|
static int |
PLAYER_INFO_NOT_SEEKABLE |
Indicates that the media is not seekable.
|
static int |
PLAYER_INFO_STARTED_AS_NEXT |
Indicates that the current player has just finished the playback and the next player starts the playback.
|
static int |
PLAYER_INFO_SUBTITLE_TIMED_OUT |
Indicates that parsing the subtitle track is time-consuming.
|
static int |
PLAYER_INFO_TIMED_TEXT_ERROR |
Indicates that an error occurs when processing timed text.
|
static int |
PLAYER_INFO_UNKNOWN |
Indicates an unknown the information type.
|
static int |
PLAYER_INFO_UNSUPPORTED_SUBTITLE |
Indicates that the media system does not support subtitle tracks.
|
static int |
PLAYER_INFO_VIDEO_NOT_PLAYING |
Indicates that video playback has stopped, but the audio is still playing.
|
static int |
PLAYER_INFO_VIDEO_RENDERING_START |
Indicates that the rendering of the first frame in the media file starts.
|
static int |
PLAYER_INFO_VIDEO_TRACK_LAGGING |
Indicates that video frames cannot be decoded quickly.
|
static int |
VIDEO_SCALE_TYPE_CROP |
Indicates that the aspect ratio of the video remains unchanged.
|
static int |
VIDEO_SCALE_TYPE_FIT |
Indicates that the video is stretched in the rendering surface.
|
| 构造器 | 说明 |
|---|---|
Player(Context context) |
A constructor used to create a
Player instance for local playback. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
boolean |
enableScreenOn(boolean screenOn) |
Keeps the screen always on during video playback.
|
boolean |
enableSingleLooping(boolean looping) |
Sets the current playback to loop a single item.
|
protected void |
finalize() |
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
Player.AudioHandler |
getAudioHandler() |
Obtains the
AudioHandler for audio parameters whose values are out of common ranges. |
float |
getAudioPitch() |
Obtains the pitch coefficient set for the current audio.
|
int |
getAudioStreamType() |
Obtains the audio type.
|
int |
getCurrentStream(int streamType) |
Obtains the index of a media stream (of a specified type) selected for playback.
|
int |
getCurrentTime() |
Obtains the playback position, which is in milliseconds.
|
int |
getDuration() |
Obtains the total duration (in milliseconds) of media files.
|
float |
getPlaybackSpeed() |
Obtains the playback speed.
|
Player.StreamInfo[] |
getStreamInfo() |
Obtains all media stream information contained in a media file.
|
int |
getVideoHeight() |
Obtains the video height.
|
int |
getVideoWidth() |
Obtains the video width.
|
boolean |
isNowPlaying() |
Checks whether the player is playing.
|
boolean |
isSingleLooping() |
Checks whether the current playback is set to loop a single item.
|
boolean |
pause() |
Pauses playback.
|
boolean |
play() |
Starts playback.
|
boolean |
prepare() |
Prepares the playback environment and buffers media data.
|
boolean |
release() |
Releases playback resources.
|
boolean |
reset() |
Restores the player to the initial state.
|
boolean |
rewindTo(long microseconds) |
Changes the playback position.
|
boolean |
setAudioHandler(Player.AudioHandler audioHandler) |
Sets the
AudioHandler for audio parameters whose values are out of common ranges. |
boolean |
setAudioParamsInitialized() |
Sets audio parameters, such as the pitch and handling mode, to default values.
|
boolean |
setAudioPitch(float audioPitch) |
Sets the pitch coefficient for the current audio.
|
boolean |
setAudioStreamType(int type) |
Sets the audio type.
|
boolean |
setNextPlayer(Player next) |
Sets the next player to start after the current player finishes playback.
|
boolean |
setPlaybackSpeed(float speed) |
Sets the playback speed.
|
void |
setPlayerCallback(Player.IPlayerCallback callback) |
Registers a
IPlayerCallback to receive event notifications. |
boolean |
setSource(BaseFileDescriptor assetFD) |
Sets a media file to be played from app's assets directory.
|
boolean |
setSource(Source source) |
Sets a media file to be played from locally or from the Internet.
|
boolean |
setSurfaceOps(SurfaceOps surfaceHolder) |
Sets the video playback window.
|
boolean |
setVideoScaleType(int type) |
Sets the video scaling type.
|
boolean |
setVideoSurface(Surface surface) |
Sets the video playback window.
|
boolean |
setVolume(float volume) |
Adjusts volume.
|
void |
specifyStream(int streamIndex) |
Selects a media stream.
|
boolean |
stop() |
Stops playback.
|
void |
unspecifyStream(int streamIndex) |
Unselects a selected media stream.
|
public static final int PLAYER_ERROR_IO
public static final int PLAYER_ERROR_MALFORMED
public static final int PLAYER_ERROR_SERVER_DIED
public static final int PLAYER_ERROR_SYSTEM
public static final int PLAYER_ERROR_TIMED_OUT
public static final int PLAYER_ERROR_UNKNOWN
public static final int PLAYER_ERROR_UNSUPPORTED
public static final int PLAYER_INFO_AUDIO_NOT_PLAYING
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_BAD_INTERLEAVING
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_BUFFERING_END
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_BUFFERING_START
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_EXTERNAL_METADATA_UPDATE
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_METADATA_UPDATE
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_NETWORK_BANDWIDTH
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_NOT_SEEKABLE
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_STARTED_AS_NEXT
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_SUBTITLE_TIMED_OUT
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_TIMED_TEXT_ERROR
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_UNKNOWN
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_UNSUPPORTED_SUBTITLE
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_VIDEO_NOT_PLAYING
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_VIDEO_RENDERING_START
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int PLAYER_INFO_VIDEO_TRACK_LAGGING
Player.IPlayerCallback.onMessage(int, int),
常量字段值public static final int VIDEO_SCALE_TYPE_CROP
If the aspect ratio of the video differs from that of the surface, the video will be cropped to fit the surface.
public static final int VIDEO_SCALE_TYPE_FIT
If the aspect ratio of the video differs from that of the surface, the original aspect ratio will not be retained during video rendering.
public Player(Context context)
Player instance for local playback.context - Indicates the application context for the invocations among internal modules of the system.public boolean setSource(Source source)
source - Indicates a local media file or one on the Internet.true if the setting is successful; returns false otherwise.public boolean setSource(BaseFileDescriptor assetFD)
assetFD - Indicates a media file in the app's assets directory.true if the setting is successful; returns false otherwise.public boolean prepare()
true if the preparation is successful; returns false otherwise.public boolean play()
true if the playback is started; returns false otherwise.public boolean isNowPlaying()
true if the player is playing; returns false otherwise.public boolean pause()
true if the playback is paused; returns false otherwise.public boolean stop()
true if the playback is stopped; returns false otherwise.public boolean rewindTo(long microseconds)
microseconds - Indicates the target playback position.true if the playback position is changed; returns false otherwise.public boolean setVolume(float volume)
volume - Indicates the target volume to set, which ranges from 0.0 to 1.0.public boolean setVideoSurface(Surface surface)
surface - Indicates the video playback window.true if the playback window is set; returns false otherwise.public boolean setSurfaceOps(SurfaceOps surfaceHolder)
You must call this method or setVideoSurface(ohos.agp.graphics.Surface) for video playback.
Otherwise, only the audio track will be played.
surfaceHolder - Indicates the surface used for video playback.true if the operation is successful; returns false otherwise.public boolean enableScreenOn(boolean screenOn)
This method uses the SurfaceOps to keep the screen on during video playback.
screenOn - Indicates whether to keep the screen constantly on. Value true means that the screen
is kept on during video playback, and false means the opposite.true if the operation is successful; returns false otherwise.public boolean setVideoScaleType(int type)
type - Indicates the way the video will be scaled. The value must be one of the following:
true if the operation is successful; returns false otherwise.public boolean enableSingleLooping(boolean looping)
looping - Indicates whether to enable playback looping. Value true means to enable playback
looping, and false means to disable playback looping.true if the setting is successful; returns false otherwise.public boolean isSingleLooping()
true if single-item loop is set; returns false otherwise.public int getCurrentTime()
public int getDuration()
public int getVideoWidth()
public int getVideoHeight()
public boolean setPlaybackSpeed(float speed)
speed - Indicates the playback speed to set, which ranges from 0.01 to 20.00.true if the playback speed is set; returns false otherwise.public float getPlaybackSpeed()
public Player.AudioHandler getAudioHandler()
AudioHandler for audio parameters whose values are out of common ranges.AudioHandler if obtained; returns null otherwise.public boolean setAudioHandler(Player.AudioHandler audioHandler)
AudioHandler for audio parameters whose values are out of common ranges.audioHandler - Indicates the handling mode to set, which is defined in AudioHandler.true if the setting is successful; returns false otherwise.IllegalArgumentException - Throws this exception if audioHandler is invalid.public boolean setAudioParamsInitialized()
true if the setting is successful; returns false otherwise.public boolean setAudioPitch(float audioPitch)
The audioPitch is multiplied by the original pitch to make the playback pitch of the current audio.
audioPitch - Indicates the coefficient to be multiplied. The value must be greater than 0.
The default value is 1.0. You are advised to set a value within [0.3, 3]. Otherwise, the audio will be
severely distorted.true if the setting is successful; returns false otherwise.IllegalArgumentException - Throws this exception if audioPitch is less than 0.public float getAudioPitch()
1.0
if the pitch coefficient is not set.public boolean setAudioStreamType(int type)
type - Indicates the audio type. For details, see AudioManager.AudioVolumeTypetrue if the setting is successful; returns false otherwise.public int getAudioStreamType()
public boolean setNextPlayer(Player next)
setSource(ohos.media.common.Source) has been called, and must be
prepared by the application.next - Indicates the player to set. The next player must be different from the current one, and the pointer
cannot be null.true if the next player is set; returns false otherwise.public boolean reset()
true if the player is restored; returns false otherwise.public boolean release()
true if the resources are released; returns false otherwise.public void setPlayerCallback(Player.IPlayerCallback callback)
IPlayerCallback to receive event notifications.callback - Indicates the callback.public Player.StreamInfo[] getStreamInfo()
StreamInfo array
whose length equals the number of streams.public int getCurrentStream(int streamType)
The index is contained in the array returned by getStreamInfo().
You can use the index for calling specifyStream(int) or unspecifyStream(int).
streamType - Indicates the stream type, which can be
StreamInfo#MEDIA_STREAM_TYPE_VIDEO,
StreamInfo#MEDIA_STREAM_TYPE_AUDIO,
StreamInfo#MEDIA_STREAM_TYPE_SUBTITLE,
or StreamInfo#MEDIA_STREAM_TYPE_TIMEDTEXT.streamType or the streamType
value is out of the value range.public void specifyStream(int streamIndex)
Currently, only four types of streams are supported. StreamInfo#MEDIA_STREAM_TYPE_VIDEO
and StreamInfo#MEDIA_STREAM_TYPE_AUDIO are selected by default if they are available.
StreamInfo#MEDIA_STREAM_TYPE_SUBTITLE and StreamInfo#MEDIA_STREAM_TYPE_TIMEDTEXT
can be selected only by calling this method.
streamIndex - Indicates the index of the media stream to select.
The value ranges from 0 to the total number of media streams minus 1.
You can obtain the number of media streams via getStreamInfo().public void unspecifyStream(int streamIndex)
Currently, this method only supports streams of the
StreamInfo#MEDIA_STREAM_TYPE_SUBTITLE or
StreamInfo#MEDIA_STREAM_TYPE_TIMEDTEXT type.
streamIndex - Indicates the index of the media stream to unselect.
The value ranges from 0 to the total number of media streams minus 1.
You can obtain the number of media streams via getStreamInfo().protected void finalize()
throws Throwable
Objectfinalize method to dispose of
system resources or to perform other cleanup.
The general contract of finalize is that it is invoked
if and when the Java™ virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The finalize method may take any action, including
making this object available again to other threads; the usual purpose
of finalize, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.
The finalize method of class Object performs no
special action; it simply returns normally. Subclasses of
Object may override this definition.
The Java programming language does not guarantee which thread will
invoke the finalize method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.
The finalize method is never invoked more than once by a Java
virtual machine for any given object.
Any exception thrown by the finalize method causes
the finalization of this object to be halted, but is otherwise
ignored.
finalize 在类中 ObjectThrowable - the Exception raised by this methodWeakReference,
PhantomReference