ExoPlayer 的小解析

来源:互联网 发布:网络招生技巧 编辑:程序博客网 时间:2024/06/01 08:07

前言

     随着近年来,网络技术的飞速发展,视频能满足用户的视觉效果和声音的传达;市面上越来越多的app 可以进行视频播放,其中视频格式多种多样:如avi ,mp4,m3u8,rmvb等;类型又分为视频直播(hls,rstp,rtmp...)和非直播(3gp,mp4,avi ...)。其中这俩种我都项目中都接触了,且一入坑就是2年.作为android开发者, 一个好用的开源播放器插件  可以事半功倍 ,一石二鸟 。ExoPlayer 特别好用,  特别好理解里面代码逻辑, 特别好针对性界面开发。

结构介绍 

    ExoPlayer是一个应用程序级的媒体播放器的Android。它提供了一种替代的Android API的MediaPlayer播放音频和视频当在联网时。ExoPlayer支持目前不支持Android的MediaPlayer API功能,包括DASH 和smoothstreaming自适应回放。

打开 开源项目地址


分别为:

all -注册的AndroidManifest.xml

core-核心处理逻辑

dash-处理dash 流媒体逻辑

hls-处理hls流媒体逻辑

smoothstreaming-处理smoothstreaming逻辑

ui-处理界面的业务

快速开始

1.build.xml添加
   compile 'com.google.android.exoplayer:exoplayer:r2.3.0'
2.layout.xml 添加

   <com.google.android.exoplayer2.ui.SimpleExoPlayerView        android:id="@+id/simpleExoPlayerView"        android:layout_gravity="left|top"        android:layout_centerInParent="true"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        />

3.主要Activity 

package com.example.test_ijk;import android.content.Context;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import com.google.android.exoplayer2.DefaultLoadControl;import com.google.android.exoplayer2.ExoPlayerFactory;import com.google.android.exoplayer2.LoadControl;import com.google.android.exoplayer2.SimpleExoPlayer;import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;import com.google.android.exoplayer2.extractor.ExtractorsFactory;import com.google.android.exoplayer2.source.ExtractorMediaSource;import com.google.android.exoplayer2.source.MediaSource;import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;import com.google.android.exoplayer2.trackselection.TrackSelection;import com.google.android.exoplayer2.trackselection.TrackSelector;import com.google.android.exoplayer2.ui.SimpleExoPlayerView;import com.google.android.exoplayer2.upstream.DataSource;import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;import com.google.android.exoplayer2.util.Util;/** * android google exoplay */public class MainActivity extends AppCompatActivity {    String url="";    MediaSource videoSource;    SimpleExoPlayer player ;    Context context;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        context=MainActivity.this;        setContentView(R.layout.activity_main);        // 1.创建一个默认TrackSelector        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();        TrackSelection.Factory videoTrackSelectionFactory =                new AdaptiveTrackSelection.Factory(bandwidthMeter);        TrackSelector trackSelector =                new DefaultTrackSelector(videoTrackSelectionFactory);        // 2.创建一个默认的LoadControl        LoadControl loadControl = new DefaultLoadControl();        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,                Util.getUserAgent(context, "yourApplicationName"), bandwidthMeter);        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();        url="http://mpv.videocc.net/ce0812b122/a/ce0812b122bf0fb49d79ebd97cbe98fa_1.mp4";            //test mp4            videoSource = new ExtractorMediaSource(Uri.parse(url),                    dataSourceFactory, extractorsFactory, null, null);        // 3.创建播放器        player = ExoPlayerFactory.newSimpleInstance(context,trackSelector,loadControl);        SimpleExoPlayerView simpleExoPlayerView= (SimpleExoPlayerView) findViewById(R.id.simpleExoPlayerView);        // 将player关联到View上        simpleExoPlayerView.setPlayer(player);        player.prepare(videoSource);// Prepare the player with the source.    }    @Override    protected void onDestroy() {        super.onDestroy();        player.release();    }}


个人亲测:HLS 和MP4均测试无问题;



源码赏析

1.SimpleExoPlayerView继承了FrameLayout



2.ExtractorMediaSource 实现android 原生mediosource,listenr;


3.ExoPlayerImpl是ExoPlayer实现类


4.SimpleExoPlayer 视频播放主要处理类



总结

ExoPlayer 作为谷歌官方出品,嵌入简单,可移值性强,且已经衍生到2.3 版本,后续也会更新,相信大家对它也会充满信心。
语文不是很好,有问题可以提出,看到必回,讲我知道的,一起讨论不知道的。


 
原创粉丝点击