http://www.reibang.com/p/36ccc1be76b8
這個有我從官網(wǎng)上面嘍的導(dǎo)入教程叁幢,很簡單,自己百度谷歌下就出現(xiàn)了坪稽。
這篇主要是講曼玩,如何跑一個demo,個人建議還是將官網(wǎng)上面的那個代碼下載下來窒百,自己跑一下黍判,能夠解決很多問題。
文檔地址篙梢。
https://exoplayer.dev/guide.html
ExoPlayer播放DASH顷帖,SmoothStreaming和HLS自適應(yīng)流,以及MP4,M4A贬墩,F(xiàn)MP4榴嗅,WebM,MKV陶舞,MP3嗽测,Ogg,WAV肿孵,MPEG-TS唠粥,MPEG-PS,F(xiàn)LV和ADTS等格式( AAC)停做。它還討論了ExoPlayer事件夏块,消息即舌,自定義和DRM支持必怜。這是谷歌翻譯exoplayer能夠播放什么格式文件的介紹逢享。
這個很重要,一共有4總MediaSource眉抬,首先 我們要知道 將要播放的視頻格式需要對應(yīng)創(chuàng)建那種MediaSource。
因為我主要是做 M3u8 和MP4播放懈凹,所有蜀变,其他的歸類可能有誤,歡迎指出介评。
在demo的PlayerActivity類的buildMediaSource方法中對 視頻格式究竟屬于哪個MediaSource 進(jìn)行了分類库北。
。因為我只有m3u8和mp4 的視頻源们陆,所有我就直接創(chuàng)建的MediaSource 對象并未像官方這么創(chuàng)建寒瓦。
首先是xml然后是activity
嗯,重要的是 要設(shè)置這個:
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
至于 MyPlayerView是什么鬼坪仇,是我復(fù)制了一份PlayerView 然后把控制層代碼刪除了杂腰。
private MyPlayerView myPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.PlayerTheme_Spherical);
setContentView(R.layout.activity_my_player);
myPlayer = findViewById(R.id.myPlayer);
// myPlayer.setControllerVisibilityListener(this);//控制層
//myPlayer.setErrorMessageProvider(new PlayerErrorMessageProvider());//設(shè)置錯誤回調(diào)
myPlayer.requestFocus();
DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
String userAgent = Util.getUserAgent(this, "ExoPlayerDemo");
DefaultDataSourceFactory mediaDataSourceFactory = new DefaultDataSourceFactory(this, BANDWIDTH_METER,
new DefaultHttpDataSourceFactory(userAgent, BANDWIDTH_METER));
String fileName = "http://192.168.0.121:2100/video/20190405/8f4DyBLz/hls/index.m3u8";
Uri uri = Uri.parse(fileName);
HlsMediaSource mediaSource = null;
if (fileName.endsWith(".m3u8")){
mediaSource = new HlsMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri);
}
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);
myPlayer.setPlayer(player);
// player.addListener(this);
player.setPlayWhenReady(true);
if(mediaSource!=null) {
player.prepare(mediaSource);
}
}
然后如果是其他格式的請參考上面圖片,創(chuàng)建對應(yīng)的MediaSource就好椅文。