exoplayer

exoplayer

 

 

  • 构建视频播放器播放mp4流  
implementation 'com.google.android.exoplayer:exoplayer:2.8.4'
implementation 'com.google.android.exoplayer:exoplayer-core:2.8.4'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.2.8.4'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.2.8.4'
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>
public class ExoPlayerActivity extends AppCompatActivity {
    public static final String IMAGE_URL = "http://dl.sunshinefm.cn/mobileimg/application/cac/f49/5805ea42cd2dd.png";

    private PlayerView playerView;
    private SimpleExoPlayer player;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playerView = findViewById(R.id.player_view);
    }

    @Override
    protected void onStart() {
        super.onStart();
        player = ExoPlayerFactory.newSimpleInstance(this,
                new DefaultTrackSelector());

        playerView.setPlayer(player);
        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this,"exo-demo"));
        ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse(IMAGE_URL));
        player.prepare(mediaSource);
        player.setPlayWhenReady(true);
    }
@Override
protected void onStop() {
    super.onStop();
    playerView.setPlayer(null);
    player.release();
    player = null;
}

 

  • 如何插入广告与视频内容在一起
implementation 'com.google.android.exoplayer:extension-ima:2.2.8.4'

public class ExoPlayerActivity extends AppCompatActivity {
    //http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8
    public static final String IMAGE_URL = "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8";

    public static final String AD_URL = "http://sbslive.cnrmobile.com/storage/storage4/76/23/10/14de52115893fb5371b7e064f910ab7b.3gp";

    private PlayerView playerView;
    private SimpleExoPlayer player;
    ImaAdsLoader adsLoader;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        playerView = findViewById(R.id.player_view);


        adsLoader = new ImaAdsLoader(this, Uri.parse(AD_URL));
    }

    @Override
    protected void onStart() {
        super.onStart();
        player = ExoPlayerFactory.newSimpleInstance(this,
                new DefaultTrackSelector());

        playerView.setPlayer(player);
        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this,"exo-demo"));
        ExtractorMediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                .createMediaSource(Uri.parse(IMAGE_URL));

        AdsMediaSource adsMediaSource = new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader,
                playerView.getOverlayFrameLayout());
        player.prepare(adsMediaSource);
        player.setPlayWhenReady(true);
    }

    @Override
    protected void onStop() {
        super.onStop();
        playerView.setPlayer(null);
        player.release();
        player = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adsLoader.release();
    }
} 

 

  • 音频播放在后台服务
public class AudioPlayerService extends Service {


    String[] urls = {"http://live.cc.52ytv.cn/fm/2/3809088728/54abb17d92e5e.m3u8","http://live.cc.52ytv.cn/fm/2/3809088729/54abb17d92eb3.m3u8"};
    String[] imags = {"http://ubmcmm.baidustatic.com/media/v1/0f000D3Tc9-rZbcEkUN9O0.jpg","http://ubmcmm.baidustatic.com/media/v1/0f000D3Tc9-rZbcEkUN9O0.jpg"};
    String[] title ={"name","text"};
    private SimpleExoPlayer player;
    private PlayerNotificationManager playerNotificationManager;

    @Override
    public void onCreate() {
        super.onCreate();
        final Context context = this;
        player = ExoPlayerFactory.newSimpleInstance(this,
                new DefaultTrackSelector());

        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this,"AudioDemo"));

        ConcatenatingMediaSource concatenatingMediaSource = new ConcatenatingMediaSource();
        for (int i=0;i<urls.length;i++){
            MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).
                    createMediaSource(Uri.parse(urls[i]));
            concatenatingMediaSource.addMediaSource(mediaSource);
        }
        player.prepare(concatenatingMediaSource);
        player.setPlayWhenReady(true);

        playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
                context,"CHANNEL",R.string.app_name,1000,new PlayerNotificationManager.MediaDescriptionAdapter() {
            @Override
            public String getCurrentContentTitle(Player player) {
                return title[player.getCurrentWindowIndex()];
            }

            @Nullable
            @Override
            public PendingIntent createCurrentContentIntent(Player player) {
                Intent intent = new Intent(context,ExoPlayerActivity.class);
                return PendingIntent.getActivity(context,0,intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
            }

            @Nullable
            @Override
            public String getCurrentContentText(Player player) {
                return title[player.getCurrentWindowIndex()];
            }

            @Nullable
            @Override
            public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) {
                Resources res = getResources();
                Bitmap bmp = BitmapFactory.decodeResource(res, R.mipmap.ic_launcher);
                return bmp;
            }
        });

        playerNotificationManager.setNotificationListener(new PlayerNotificationManager.NotificationListener() {
            @Override
            public void onNotificationStarted(int notificationId, Notification notification) {
                startForeground(notificationId,notification);
            }

            @Override
            public void onNotificationCancelled(int notificationId) {
                stopSelf();
            }
        });
        playerNotificationManager.setPlayer(player);
    }


    @Override
    public void onDestroy() {
        playerNotificationManager.setPlayer(null);
        player.release();
        player = null;
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

  

  • 使用android MediaSessions
  • exoplayer新功能下载媒体离线播放

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值