我是靠谱客的博主 悦耳黄蜂,最近开发中收集的这篇文章主要介绍MediaPlayer(八)--start()流程,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

frameworks/base/media/java/android/media/MediaPlayer.java


/**
* 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 if it is called in an invalid state
*/
public void start() throws IllegalStateException {
//FIXME use lambda to pass startImpl to superclass
final int delay = getStartDelayMs();
if (delay == 0) {
startImpl();
} else {
new Thread() {
public void run() {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
baseSetStartDelayMs(0);
try {
startImpl();
} catch (IllegalStateException e) {
// fail silently for a state exception when it is happening after
// a delayed start, as the player state could have changed between the
// call to start() and the execution of startImpl()
}
}
}.start();
}
}
private void startImpl() {
baseStart();
stayAwake(true);
_start();
}
private native void _start() throws IllegalStateException;

当player处于pause状态是,执行start方法会从暂停的地方继续播放,当播放没开始时,执行start方法会从头播放。这里没有太多逻辑,直接调用了jni的_start方法

static void
android_media_MediaPlayer_start(JNIEnv *env, jobject thiz)
{
ALOGV("start");
sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
if (mp == NULL ) {
jniThrowException(env, "java/lang/IllegalStateException", NULL);
return;
}
process_media_player_call( env, thiz, mp->start(), NULL, NULL );
}

直接调用下一层
frameworks/av/media/libmedia/mediaplayer.cpp

status_t MediaPlayer::start()
{
ALOGV("start");
status_t ret = NO_ERROR;
Mutex::Autolock _l(mLock);
mLockThreadId = getThreadId();
if (mCurrentState & MEDIA_PLAYER_STARTED) {
ret = NO_ERROR;
} else if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED |
MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) {
mPlayer->setLooping(mLoop);
mPlayer->setVolume(mLeftVolume, mRightVolume);
mPlayer->setAuxEffectSendLevel(mSendLevel);
mCurrentState = MEDIA_PLAYER_STARTED;
ret = mPlayer->start();
if (ret != NO_ERROR) {
mCurrentState = MEDIA_PLAYER_STATE_ERROR;
} else {
if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) {
ALOGV("playback completed immediately following start()");
}
}
} else {
ALOGE("start called in state %d, mPlayer(%p)", mCurrentState, mPlayer.get());
ret = INVALID_OPERATION;
}
mLockThreadId = 0;
return ret;
}

调用了MediaPlayerService 的setLooping 设置是否循环播放,接着调用设置了音频的接口,再将播放器的专题改为MEDIA_PLAYER_STARTED。最后调用MediaPlayerService start方法

status_t MediaPlayerService::Client::start()
{
ALOGV("[%d] start", mConnId);
if (mConnectedWindow.get() == NULL)
mMaybeVideoAlive = false;
sp<MediaPlayerBase> p = getPlayer();
if (p == 0) return UNKNOWN_ERROR;
p->setLooping(mLoop);
return p->start();
}

这部分代码也没有多少逻辑,调用了Nuplayer的方法。

其他的方法pause,stop,reset逻辑大体相似

最后

以上就是悦耳黄蜂为你收集整理的MediaPlayer(八)--start()流程的全部内容,希望文章能够帮你解决MediaPlayer(八)--start()流程所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部