public final class

KSYMediaPlayer

com.ksyun.media.player.KSYMediaPlayer

Class Overview

播放SDK的核心类,实现播放视频的功能

Summary

Nested Classes
class KSYMediaPlayer.Builder 创建KSYMediaPlayer所需的内部类  
enum KSYMediaPlayer.KSYDecodeMode 播放器解码类型 
enum KSYMediaPlayer.KSYDeinterlaceMode 反交错模式,该功能只在使用点播库时方可生效 
enum KSYMediaPlayer.KSYReloadMode Reload模式 
interface KSYMediaPlayer.OnAudioPCMListener  
interface KSYMediaPlayer.OnVideoRawDataListener 原始视频数据的回调监听器  
interface KSYMediaPlayer.OnVideoTextureListener  
Constants
int KSY_LOG_DEBUG
int KSY_LOG_DEFAULT
int KSY_LOG_ERROR
int KSY_LOG_FATAL
int KSY_LOG_INFO
int KSY_LOG_SILENT
int KSY_LOG_UNKNOWN
int KSY_LOG_VERBOSE
int KSY_LOG_WARN
int MEDIA_MESSAGE_INFO
int MEDIA_SET_VIDEO_SAR
int SDL_FCC_RV16 原始视频输出格式为RGB565
int SDL_FCC_RV32 原始视频输出格式为RGB8888
int VIDEO_SCALING_MODE_NOSCALE_TO_FIT 视频缩放模式,拉伸模式,视频完全填满显示窗口,视频与窗口比例不匹配画面会有变形setVideoScalingMode(int)
int VIDEO_SCALING_MODE_SCALE_TO_FIT 视频缩放模式,填充模式,在视频宽高比例与手机宽高比例不一致时,播放可能留有黑边,具体接口setVideoScalingMode(int)
int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING 视频缩放模式,裁剪模式,具体接口setVideoScalingMode(int)
Public Methods
void _onAudioPCMReady(ByteBuffer buffer, long timestamp, int samplerate, int channels, int fmt)
void addTimedTextSource(String path)
设置外挂字幕的路径,播放器会打开并解码外挂字幕
void addVideoRawBuffer(byte[] rawBuffer)
将缓存buffer交还给播放器
int bufferEmptyCount()
获取卡顿次数
float bufferEmptyDuration()
获取卡顿时长,单位为秒
void deselectTrack(int trackIndex)
根据流索引关闭对应的流, 流索引可通过 getTrackInfo() 获取
void enableFastPlayMode(boolean enable)
long getAudioCachedBytes()
long getAudioCachedDuration()
long getAudioCachedPackets()
int getAudioSessionId()
获取音频Session ID
float getBufferTimeMax()
获取当前播放器缓存时长的阈值
String getClientIP()
获取当前使用者的出口IP地址
static String getColorFormatName(int mediaCodecColorFormat)
long getCurrentPosition()
获取当前播放的进度
long getCurrentPts()
long getCurrentVideoPts()
String getDataSource()
long getDecodedDataSize()
获取已解码的数据量
long getDownloadDataSize()
获取已下载的数据量
long getDuration()
获取当前多媒体文件的时长,须在IMediaPlayer.OnPreparedListener发起回调了方可有效
String getLocalDnsIP()
获取本地DNS服务器的IP地址
MediaInfo getMediaInfo()
Bundle getMediaMeta()
Bitmap getScreenShot()
int getSelectedTrack(int trackType)
String getServerAddress()
获取服务器IP
float getSpeed()
获取当前播放速度,1.0f为正常播放速度
KSYQosInfo getStreamQosInfo()
获取当前播放器的缓存状态
long getStreamStartTime()
KSYTrackInfo[] getTrackInfo()
获取当前播放的多媒体文件的流信息 须在prepareAsync()成功后调用才有意义
static String getVersion()
获取播放SDK的版本
long getVideoCachedBytes()
long getVideoCachedDuration()
long getVideoCachedPackets()
float getVideoDecodeFramesPerSecond()
获取当前视频解码帧率
int getVideoDecoder()
int getVideoHeight()
获取视频的高度,须在IMediaPlayer.OnPreparedListener发起回调了方可有效
float getVideoOutputFramesPerSecond()
获取当前视频渲染的帧率
int getVideoSarDen()
int getVideoSarNum()
int getVideoWidth()
获取视频的宽度,须在IMediaPlayer.OnPreparedListener发起回调了方可有效
boolean isLooping()
Checks whether the MediaPlayer is looping or non-looping.
boolean isPlayable()
boolean isPlaying()
播放器是否处于播放状态
void pause()
暂停播放视频
void prepareAsync()
准备播放多媒体文件,准备成功后IMediaPlayer.OnPreparedListener会发出相应回调
void release()
Releases resources associated with this KSYMediaPlayer object.
void reload(String path, boolean flushBuffer, KSYMediaPlayer.KSYReloadMode mode)
reload视频
void reload(String path, boolean flushBuffer)
reload视频,默认使用KSY_RELOAD_MODE_FAST模式
void reset()
重置播放器,软解情况下会清除屏幕内容
void resetListeners()
void seekTo(long msec, boolean accurate)
快进快退
void seekTo(long msec)
快进快退
void selectTrack(int trackIndex)
根据流索引开启对应的流, 流索引可通过 getTrackInfo() 获取
void setBufferSize(int size)
设置播放器缓存数据的最大值,须在prepareAsync()之前调用
void setBufferTimeMax(float timeSecond)
设置播放器缓存数据时长的最大阈值,只对直播有效,须在prepareAsync()之前调用。该值较大,则主播和观众之间延迟较大,该值较小,则对网络波动更敏感,容易引发卡顿
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 setDataSource(Context context, Uri uri, Map<String, String> headers)
Sets the data source as a content Uri.
void setDataSource(IMediaDataSource mediaDataSource)
void setDecodeMode(KSYMediaPlayer.KSYDecodeMode mode)
设置播放器的解码类型,须在prepareAsync()之前调用
void setDeinterlaceMode(KSYMediaPlayer.KSYDeinterlaceMode mode)
设置反交错的模式,须使用点播库方可生效
void setDisplay(SurfaceHolder sh)
Sets the SurfaceHolder to use for displaying the video portion of the media.
void setLogEnabled(boolean enable)
void setLooping(boolean looping)
Sets the player to be looping or non-looping.
boolean setMirror(boolean mirror)
设置视频镜像显示
void setOnAudioPCMAvailableListener(KSYMediaPlayer.OnAudioPCMListener listener)
void setOnControlMessageListener(KSYMediaPlayer.OnControlMessageListener listener)
void setOnVideoTextureListener(KSYMediaPlayer.OnVideoTextureListener listener)
void setPlayableRanges(long startTime, long endTime)
由金山云引入, 设置可播放的范围,仅对点播有效 调用此接口时, 需注意: 1.
void setPlayerMute(int mute)
设置播放器是否禁音
boolean setRotateDegree(int degree)
设置旋转角度,旋转方向为逆时针
void setScreenOnWhilePlaying(boolean screenOn)
设置在播放视频时是否保持屏幕常亮 必须在setDisplay(SurfaceHolder)之后调用方可生效
void setSpeed(float speed)
设置播放速度
void setSurface(Surface surface)
Sets the Surface to be used as the sink for the video portion of the media.
void setTimeout(int prepareTimeout, int readTimeout)
设置准备和读数据超时阈值,需在prepareAsync()之前调用方可生效
void setVideoOffset(float horizontal, float vertical)
设置视频的水平和垂直方向的平移量。以手机屏幕中心为原点建立坐标轴,横纵轴坐标各自平行于手机屏幕边缘。归一化处理后,手机屏幕的坐标取值范围为[-1.0, 1.0] 仅在VIDEO_SCALING_MODE_SCALE_TO_FIT模式下本接口方可生效
void setVideoRawDataListener(KSYMediaPlayer.OnVideoRawDataListener listener)
设置获取原始视频数据的监听器
void setVideoRenderingState(int mode)
设置当前是否由播放器渲染视频内容,与获取视频内容的回调接口KSYMediaPlayer.OnVideoRawDataListener相结合使用
void setVideoScalingMode(int mode)
设置播放器视频缩放模式
void setVolume(float leftVolume, float rightVolume)
设置当前播放音频,范围为 0.0f -- 1.0f,左右声道的音量建议一致
void setWakeMode(Context context, int mode)
void shouldAutoPlay(boolean state)
必须在prepareAsync()之前调用方可生效 在prepare完成后是否自动开播,默认为
void softReset()
重置播放器,但是不会清除屏幕内容
void start()
开始播放视频
void stop()
停止播放视频
Protected Methods
void finalize()

Constants

public static final int KSY_LOG_DEBUG

Constant Value: 3 (0x00000003)

public static final int KSY_LOG_DEFAULT

Constant Value: 1 (0x00000001)

public static final int KSY_LOG_ERROR

Constant Value: 6 (0x00000006)

public static final int KSY_LOG_FATAL

Constant Value: 7 (0x00000007)

public static final int KSY_LOG_INFO

Constant Value: 4 (0x00000004)

public static final int KSY_LOG_SILENT

Constant Value: 8 (0x00000008)

public static final int KSY_LOG_UNKNOWN

Constant Value: 0 (0x00000000)

public static final int KSY_LOG_VERBOSE

Constant Value: 2 (0x00000002)

public static final int KSY_LOG_WARN

Constant Value: 5 (0x00000005)

protected static final int MEDIA_MESSAGE_INFO

Constant Value: 599 (0x00000257)

protected static final int MEDIA_SET_VIDEO_SAR

Constant Value: 10001 (0x00002711)

public static final int SDL_FCC_RV16

原始视频输出格式为RGB565

Constant Value: 909203026 (0x36315652)

public static final int SDL_FCC_RV32

原始视频输出格式为RGB8888

Constant Value: 842225234 (0x32335652)

public static final int VIDEO_SCALING_MODE_NOSCALE_TO_FIT

视频缩放模式,拉伸模式,视频完全填满显示窗口,视频与窗口比例不匹配画面会有变形setVideoScalingMode(int)

Constant Value: 0 (0x00000000)

public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT

视频缩放模式,填充模式,在视频宽高比例与手机宽高比例不一致时,播放可能留有黑边,具体接口setVideoScalingMode(int)

Constant Value: 1 (0x00000001)

public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING

视频缩放模式,裁剪模式,具体接口setVideoScalingMode(int)

Constant Value: 2 (0x00000002)

Public Methods

public void _onAudioPCMReady (ByteBuffer buffer, long timestamp, int samplerate, int channels, int fmt)

public void addTimedTextSource (String path)

设置外挂字幕的路径,播放器会打开并解码外挂字幕

Parameters
path 外挂字幕路径

public void addVideoRawBuffer (byte[] rawBuffer)

将缓存buffer交还给播放器

public int bufferEmptyCount ()

获取卡顿次数

Returns
  • 卡顿次数

public float bufferEmptyDuration ()

获取卡顿时长,单位为秒

Returns
  • 卡顿时长

public void deselectTrack (int trackIndex)

根据流索引关闭对应的流, 流索引可通过 getTrackInfo() 获取

public void enableFastPlayMode (boolean enable)

public long getAudioCachedBytes ()

public long getAudioCachedDuration ()

public long getAudioCachedPackets ()

public int getAudioSessionId ()

获取音频Session ID

Returns
  • 音频Session ID

public float getBufferTimeMax ()

获取当前播放器缓存时长的阈值

Returns
  • 播放器缓存时长的阈值

public String getClientIP ()

获取当前使用者的出口IP地址

Returns
  • 当前使用者的出口IP地址

public static String getColorFormatName (int mediaCodecColorFormat)

public long getCurrentPosition ()

获取当前播放的进度

Returns
  • 当前播放进度, 单位:毫秒

public long getCurrentPts ()

public long getCurrentVideoPts ()

public String getDataSource ()

public long getDecodedDataSize ()

获取已解码的数据量

Returns
  • 已解码的数据量

public long getDownloadDataSize ()

获取已下载的数据量

Returns
  • 已下载的数据量

public long getDuration ()

获取当前多媒体文件的时长,须在IMediaPlayer.OnPreparedListener发起回调了方可有效

Returns
  • 多媒体文件的时长, 单位:毫秒

public String getLocalDnsIP ()

获取本地DNS服务器的IP地址

Returns
  • 本地DNS服务器的IP地址

public MediaInfo getMediaInfo ()

public Bundle getMediaMeta ()

public Bitmap getScreenShot ()

public int getSelectedTrack (int trackType)

public String getServerAddress ()

获取服务器IP

Returns
  • 服务器IP

public float getSpeed ()

获取当前播放速度,1.0f为正常播放速度

Returns
  • 当前播放速度

public KSYQosInfo getStreamQosInfo ()

获取当前播放器的缓存状态

Returns

public long getStreamStartTime ()

public KSYTrackInfo[] getTrackInfo ()

获取当前播放的多媒体文件的流信息 须在prepareAsync()成功后调用才有意义

public static String getVersion ()

获取播放SDK的版本

Returns
  • 播放SDK的版本

public long getVideoCachedBytes ()

public long getVideoCachedDuration ()

public long getVideoCachedPackets ()

public float getVideoDecodeFramesPerSecond ()

获取当前视频解码帧率

Returns
  • 视频解码帧率

public int getVideoDecoder ()

public int getVideoHeight ()

获取视频的高度,须在IMediaPlayer.OnPreparedListener发起回调了方可有效

Returns
  • 视频的高度

public float getVideoOutputFramesPerSecond ()

获取当前视频渲染的帧率

Returns
  • 视频渲染帧率

public int getVideoSarDen ()

public int getVideoSarNum ()

public int getVideoWidth ()

获取视频的宽度,须在IMediaPlayer.OnPreparedListener发起回调了方可有效

Returns
  • 视频的宽度

public boolean isLooping ()

Checks whether the MediaPlayer is looping or non-looping.

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

public boolean isPlayable ()

public boolean isPlaying ()

播放器是否处于播放状态

Returns
  • true表示播放器处于播放状态, 反之则否

public void pause ()

暂停播放视频

Throws
IllegalStateException

public void prepareAsync ()

准备播放多媒体文件,准备成功后IMediaPlayer.OnPreparedListener会发出相应回调

Throws
IllegalStateException

public void release ()

Releases resources associated with this KSYMediaPlayer object. It is considered good practice to call this method when you're done using the KSYMediaPlayer. 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 KSYMediaPlayer 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 KSYMediaPlayer 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 reload (String path, boolean flushBuffer, KSYMediaPlayer.KSYReloadMode mode)

reload视频

Parameters
path 视频地址
flushBuffer 是否flush播放器中的旧数据
mode reload模式KSYMediaPlayer.KSYReloadMode
Throws
IllegalStateException

public void reload (String path, boolean flushBuffer)

reload视频,默认使用KSY_RELOAD_MODE_FAST模式

Parameters
path 视频地址
flushBuffer 是否flush播放器中的旧数据
Throws
IllegalStateException

public void reset ()

重置播放器,软解情况下会清除屏幕内容

public void resetListeners ()

public void seekTo (long msec, boolean accurate)

快进快退

Parameters
msec 目标时间点,单位:毫秒
accurate 是否进行精准seek
Throws
IllegalStateException

public void seekTo (long msec)

快进快退

Parameters
msec 目标时间点,单位:毫秒
Throws
IllegalStateException

public void selectTrack (int trackIndex)

根据流索引开启对应的流, 流索引可通过 getTrackInfo() 获取

Parameters
trackIndex 流索引

public void setBufferSize (int size)

设置播放器缓存数据的最大值,须在prepareAsync()之前调用

Parameters
size 播放器缓存数据的最大值,单位:MB

public void setBufferTimeMax (float timeSecond)

设置播放器缓存数据时长的最大阈值,只对直播有效,须在prepareAsync()之前调用。该值较大,则主播和观众之间延迟较大,该值较小,则对网络波动更敏感,容易引发卡顿

Parameters
timeSecond 播放器缓存的最大时长,单位:秒

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
IllegalStateException if it is called in an invalid state
IOException
IllegalArgumentException

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
IllegalStateException if it is called in an invalid state
IOException
IllegalArgumentException

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
IllegalStateException if it is called in an invalid state

When path refers to a local file, the file may actually be opened by a process other than the calling application. This implies that the pathname should be an absolute path (as any other process runs with unspecified current working directory), and that the pathname should reference a world-readable file.

IOException
IllegalArgumentException
SecurityException

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
IllegalStateException if it is called in an invalid state
IOException
IllegalArgumentException
SecurityException

public void setDataSource (Context context, Uri uri, Map<String, String> headers)

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
headers the headers to be sent together with the request for the data Note that the cross domain redirection is allowed by default, but that can be changed with key/value pairs through the headers parameter with "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value to disallow or allow cross domain redirection.
Throws
IllegalStateException if it is called in an invalid state
IOException
IllegalArgumentException
SecurityException

public void setDataSource (IMediaDataSource mediaDataSource)

Throws
IllegalArgumentException
SecurityException
IllegalStateException

public void setDecodeMode (KSYMediaPlayer.KSYDecodeMode mode)

设置播放器的解码类型,须在prepareAsync()之前调用

Parameters
mode 参见KSYMediaPlayer.KSYDecodeMode

public void setDeinterlaceMode (KSYMediaPlayer.KSYDeinterlaceMode mode)

设置反交错的模式,须使用点播库方可生效

Parameters
mode 可参见KSYMediaPlayer.KSYDeinterlaceMode

public void setDisplay (SurfaceHolder sh)

Sets the SurfaceHolder to use for displaying the video portion of the media.

Either a surface holder or surface must be set if a display or video sink is needed. Not calling this method or setSurface(Surface) when playing back a video will result in only the audio track being played. A null surface holder or surface will result in only the audio track being played.

Parameters
sh the SurfaceHolder to use for video display

public void setLogEnabled (boolean enable)

public void setLooping (boolean looping)

Sets the player to be looping or non-looping.

Parameters
looping whether to loop or not

public boolean setMirror (boolean mirror)

设置视频镜像显示

Parameters
mirror true和false分别指镜像显示和正常显示
Returns
  • true和false分别指设置镜像成功或失败

public void setOnAudioPCMAvailableListener (KSYMediaPlayer.OnAudioPCMListener listener)

public void setOnControlMessageListener (KSYMediaPlayer.OnControlMessageListener listener)

public void setOnVideoTextureListener (KSYMediaPlayer.OnVideoTextureListener listener)

public void setPlayableRanges (long startTime, long endTime)

由金山云引入, 设置可播放的范围,仅对点播有效 调用此接口时, 需注意: 1. startTime和endTime中必有一值大于0 2. 当endTime大于0时, 其值必须大于startTime 当以上任一条件不满足时会有IllegalArgumentException抛出

Parameters
startTime 起始播放时间点, 单位为毫秒(MS)
endTime 结束播放时间点, 单位为毫秒(MS)

public void setPlayerMute (int mute)

设置播放器是否禁音

Parameters
mute 1和0分别指播放禁音和不禁音

public boolean setRotateDegree (int degree)

设置旋转角度,旋转方向为逆时针

Parameters
degree 旋转角度,只能为 0/90/180/270 中的某个值
Returns
  • true表示设置成功, false则反之

public void setScreenOnWhilePlaying (boolean screenOn)

设置在播放视频时是否保持屏幕常亮 必须在setDisplay(SurfaceHolder)之后调用方可生效

Parameters
screenOn true保持屏幕常量,false则反之

public void setSpeed (float speed)

设置播放速度

Parameters
speed 目标播放速度

public void setSurface (Surface surface)

Sets the Surface to be used as the sink for the video portion of the media. This is similar to setDisplay(SurfaceHolder), but does not support setScreenOnWhilePlaying(boolean). Setting a Surface will un-set any Surface or SurfaceHolder that was previously set. A null surface will result in only the audio track being played.

If the Surface sends frames to a SurfaceTexture, the timestamps returned from SurfaceTexture will have an unspecified zero point. These timestamps cannot be directly compared between different media sources, different instances of the same media source, or multiple runs of the same program. The timestamp is normally monotonically increasing and is unaffected by time-of-day adjustments, but it is reset when the position is set.

Parameters
surface The Surface to be used for the video portion of the media.

public void setTimeout (int prepareTimeout, int readTimeout)

设置准备和读数据超时阈值,需在prepareAsync()之前调用方可生效

Parameters
prepareTimeout 准备超时阈值,即播放器在建立链接、解析流媒体信息的超时阈值
readTimeout 读数据超时阈值

public void setVideoOffset (float horizontal, float vertical)

设置视频的水平和垂直方向的平移量。以手机屏幕中心为原点建立坐标轴,横纵轴坐标各自平行于手机屏幕边缘。归一化处理后,手机屏幕的坐标取值范围为[-1.0, 1.0] 仅在VIDEO_SCALING_MODE_SCALE_TO_FIT模式下本接口方可生效

Parameters
horizontal 水平方向的平移量,范围为 -1.0至1.0,大于0表示水平向右移动,反之则水平向左移动
vertical 垂直方向的平移量,范围为 -1.0至1.0,大于0表示垂直向上移动,反之则垂直向下移动

public void setVideoRawDataListener (KSYMediaPlayer.OnVideoRawDataListener listener)

设置获取原始视频数据的监听器

public void setVideoRenderingState (int mode)

设置当前是否由播放器渲染视频内容,与获取视频内容的回调接口KSYMediaPlayer.OnVideoRawDataListener相结合使用

Parameters
mode 1和0分别指让播放器渲染和不渲染视频内容

public void setVideoScalingMode (int mode)

设置播放器视频缩放模式

public void setVolume (float leftVolume, float rightVolume)

设置当前播放音频,范围为 0.0f -- 1.0f,左右声道的音量建议一致

Parameters
leftVolume 左声道音量
rightVolume 右声道音量

public void setWakeMode (Context context, int mode)

public void shouldAutoPlay (boolean state)

必须在prepareAsync()之前调用方可生效 在prepare完成后是否自动开播,默认为

public void softReset ()

重置播放器,但是不会清除屏幕内容

public void start ()

开始播放视频

Throws
IllegalStateException

public void stop ()

停止播放视频

Throws
IllegalStateException

Protected Methods

protected void finalize ()

Throws
Throwable