媒體分兩個部分整理
1 媒體的播放過程,視訊/音樂是如何播放的
google官網
2 媒體的架構,如何在api基礎上對播放狀态控制
1 播放流程
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you
public static MediaPlayer create(Context context, int resid,
AudioAttributes audioAttributes, int audioSessionId) {
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
if (afd == null) return null;
MediaPlayer mp = new MediaPlayer();
final AudioAttributes aa = audioAttributes != null ? audioAttributes :
new AudioAttributes.Builder().build();
mp.setAudioAttributes(aa);
mp.setAudioSessionId(audioSessionId);
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
return mp;
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// fall through
}
return null;
}
構造函數
public MediaPlayer() {
super(new AudioAttributes.Builder().build(),
AudioPlaybackConfiguration.PLAYER_TYPE_JAM_MEDIAPLAYER);
Looper looper;
if ((looper = Looper.myLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else if ((looper = Looper.getMainLooper()) != null) {
mEventHandler = new EventHandler(this, looper);
} else {
mEventHandler = null;
}
mTimeProvider = new TimeProvider(this);
mOpenSubtitleSources = new Vector<InputStream>();
/* Native setup requires a weak reference to our object.
* It's easier to create it here than in C++.
*/
native_setup(new WeakReference<MediaPlayer>(this));
baseRegisterPlayer();
}
調用JNI方法
static {
System.loadLibrary("media_jni");
native_init();
}
省略Native方法
public void setDataSource(FileDescriptor fd, long offset, long length)
throws IOException, IllegalArgumentException, IllegalStateException {
_setDataSource(fd, offset, length);
}
private native void _setDataSource(FileDescriptor fd, long offset, long length)
throws IOException, IllegalArgumentException, IllegalStateException;
省略Native方法
AudioTrack和MediaPlayer播放音頻有什麼差別
AudioTrack僅播放PCM格式音頻
google meida架構使用
導入依賴
開源項目
官方服務和用戶端例子
https://github.com/googlearchive/android-MediaBrowserService
https://github.com/google/ExoPlayer
開源音樂播放器
https://github.com/saulmm/Material-Movies
https://github.com/ryanhoo/StylishMusicPlayer
https://github.com/harjot-oberai/MusicDNA
https://github.com/naman14/Timber
https://github.com/wangchenyan/ponymusic
https://github.com/andremion/Music-Player
參考資料
介紹AudioTrackd到AF的流程
整體流程從MediaPlayer - > MediaPlayerService -> NuPlayer - > AT - > AF -> h
https://blog.csdn.net/zyuanyun/article/details/60890534