EXOPlaye播放器播放直播Demo

第一步配置gadle

    implementation 'com.google.android.exoplayer:exoplayer-core:2.11.7'
    implementation 'com.google.android.exoplayer:exoplayer-dash:2.11.7'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.11.7'
    implementation 'com.google.android.exoplayer:exoplayer-hls:2.11.7'
    implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.11.7'
    implementation 'com.google.android.exoplayer:extension-rtmp:2.11.7'

第二步layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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.AspectRatioFrameLayout
        android:id="@+id/video_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        >
        <SurfaceView
            android:id="@+id/surface_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            />
    </com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
    <ProgressBar
        android:id="@+id/mProgressBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="@style/Widget.AppCompat.ProgressBar"
        android:visibility="gone"
        >
    </ProgressBar>
</androidx.constraintlayout.widget.ConstraintLayout>

第三步activity

package com.example.exoplayetest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
public class MainActivity extends AppCompatActivity{
    private SimpleExoPlayer exoPlayer;
    private AspectRatioFrameLayout videoFrame;//用来控制视频的宽高比
    private SurfaceView surfaceView; //播放区
    private ProgressBar mProgressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        surfaceView = (SurfaceView) findViewById(R.id.surface_view);
        videoFrame = (AspectRatioFrameLayout) findViewById(R.id.video_frame);
        mProgressBar=findViewById(R.id.mProgressBar);
        exoPlayer=new SimpleExoPlayer.Builder(getApplicationContext()).build();
        exoPlayer.prepare(mHlsMediaSource.getMediaSource(this));
        exoPlayer.addListener(new ExoPlayerrListener());
        // videoFrame.setAspectRatio(1.9f);
        exoPlayer.setVideoSurface(surfaceView.getHolder().getSurface());
        exoPlayer.setPlayWhenReady(true);
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
    class ExoPlayerrListener implements Player.EventListener {
        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {

        }
        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            switch (playbackState){
                case 2://缓冲
                    mProgressBar.setVisibility(View.VISIBLE);
                    break;
                case 3://就绪
                    mProgressBar.setVisibility(View.GONE);
                    break;
                case 4://完成
                    break;
            }
        }
    }
}

mediasource

package com.example.exoplayetest;

import android.content.Context;
import android.net.Uri;

import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.google.android.exoplayer2.util.Util;

public class mHlsMediaSource {
    public static HlsMediaSource getMediaSource(Context context){
        Uri contentUri = Uri.parse(Constant.ccty1);
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, context.getPackageName()));
        CacheDataSourceFactory cacheDataSourceFactory=new CacheDataSourceFactory(new SimpleCache(context.getFilesDir(),new NoOpCacheEvictor(),new ExoDatabaseProvider(context)),dataSourceFactory);
        com.google.android.exoplayer2.source.hls.HlsMediaSource.Factory factory=new com.google.android.exoplayer2.source.hls.HlsMediaSource.Factory(cacheDataSourceFactory);
        return factory.createMediaSource(contentUri);
    }
}

constant

public class Constant {
    public static String ccty1="http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8";
    public static String ccty3="http://ivi.bupt.edu.cn/hls/cctv2hd.m3u8";
    public static String ccty5="http://ivi.bupt.edu.cn/hls/cctv5hd.m3u8";
    public static String ccty5_="http://ivi.bupt.edu.cn/hls/cctv5phd.m3u8";
    public static String ccty6="http://ivi.bupt.edu.cn/hls/cctv6hd.m3u8";
    public static String RTMP ="rtsp://192.168.1.11/test.hls";
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android视频播放器Demo是一个演示Android平台上如何实现视频播放功能的应用程序。它具有以下几个主要特点和功能。 首先,Android视频播放器Demo可以播放各种常见的视频格式,如MP4、AVI、FLV等。它使用了Android自带的MediaPlayer类来实现视频的解码和播放。用户只需选择一个视频文件,点击播放按钮,即可开始播放。 其次,Android视频播放器Demo提供了一些基本的播放控制功能。用户可以通过界面上的按钮来暂停、继续播放,调节音量和亮度等。此外,还支持手势控制,用户可以在屏幕上滑动来调整播放进度和屏幕亮度。 另外,Android视频播放器Demo还提供了全屏播放和窗口播放两种模式。在全屏模式下,视频会占据整个屏幕,用户可以更好地享受视频内容。而在窗口模式下,用户可以随时切换到其他应用程序,同时仍然可以继续观看视频。 此外,Android视频播放器Demo还提供了一些定制化的功能。用户可以自定义播放界面的主题和颜色,以适应个人的喜好。同时,还内置了一些视频播放常用的功能,如播放列表、循环播放等,以提供更好的用户体验。 总结来说,Android视频播放器Demo是一个功能齐全、易于使用的应用程序,可以帮助开发者快速了解Android平台上视频播放的实现方式。通过观看和修改Demo的源代码,开发者可以进一步掌握视频播放相关的知识和技术,从而在自己的应用程序中实现高质量的视频播放功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值