Kaynağa Gözat

完善原生demo

杨充 4 yıl önce
ebeveyn
işleme
6db5f0637c

+ 17 - 1
VideoPlayer/src/main/java/org/yczbj/ycvideoplayerlib/player/VideoPlayerConfig.java

@@ -1,6 +1,8 @@
 package org.yczbj.ycvideoplayerlib.player;
 
 
+import android.content.Context;
+
 import androidx.annotation.Nullable;
 
 import com.yc.kernel.factory.PlayerFactory;
@@ -8,7 +10,7 @@ import com.yc.kernel.impl.media.MediaPlayerFactory;
 
 import org.yczbj.ycvideoplayerlib.surface.SurfaceFactory;
 import org.yczbj.ycvideoplayerlib.surface.TextureViewFactory;
-
+import org.yczbj.ycvideoplayerlib.tool.BaseToast;
 
 
 /**
@@ -28,6 +30,7 @@ public class VideoPlayerConfig {
 
     public final static class Builder {
 
+        private Context mContext;
         /**
          * 默认是关闭日志的
          */
@@ -41,6 +44,14 @@ public class VideoPlayerConfig {
         private SurfaceFactory mRenderViewFactory;
         private boolean mAdaptCutout = true;
 
+        /**
+         * 是否监听设备方向来切换全屏/半屏, 默认不开启
+         */
+        public Builder setContext(Context context) {
+            mContext = context;
+            return this;
+        }
+
         /**
          * 是否监听设备方向来切换全屏/半屏, 默认不开启
          */
@@ -119,6 +130,7 @@ public class VideoPlayerConfig {
         }
     }
 
+    public final Context mContext;
     public final boolean mPlayOnMobileNetwork;
     public final boolean mEnableOrientation;
     public final boolean mEnableAudioFocus;
@@ -149,6 +161,10 @@ public class VideoPlayerConfig {
             mRenderViewFactory = builder.mRenderViewFactory;
         }
         mAdaptCutout = builder.mAdaptCutout;
+        mContext = builder.mContext;
+        if (mContext!=null){
+            BaseToast.init(mContext);
+        }
     }
 
 

+ 2 - 1
VideoPlayer/src/main/java/org/yczbj/ycvideoplayerlib/ui/view/CustomBottomView.java

@@ -253,7 +253,8 @@ public class CustomBottomView extends FrameLayout implements InterControlView,
                 mSeekBar.setEnabled(false);
             }
             int percent = mControlWrapper.getBufferedPercentage();
-            if (percent >= 95) { //解决缓冲进度不能100%问题
+            if (percent >= 95) {
+                //解决缓冲进度不能100%问题
                 mSeekBar.setSecondaryProgress(mSeekBar.getMax());
                 mPbBottomProgress.setSecondaryProgress(mPbBottomProgress.getMax());
             } else {

+ 4 - 0
app/build.gradle

@@ -50,6 +50,10 @@ dependencies {
     implementation 'com.github.bumptech.glide:glide:4.9.0'                 //谷歌图片加载库
     implementation 'jp.wasabeef:glide-transformations:2.0.1'
 
+    //exo的UI库
+    implementation 'com.google.android.exoplayer:exoplayer-core:2.11.3'
+    implementation 'com.google.android.exoplayer:exoplayer-ui:2.11.3'
+
     //弹幕
     implementation 'com.github.ctiao:DanmakuFlameMaster:0.9.25'
     implementation 'com.github.ctiao:ndkbitmap-armv7a:0.9.21'

+ 13 - 0
app/src/main/AndroidManifest.xml

@@ -101,6 +101,19 @@
         <activity android:name="org.yczbj.ycvideoplayer.newPlayer.pip.PipListActivity"
             android:configChanges="orientation|keyboardHidden|screenSize"
             android:screenOrientation="portrait"/>
+        <activity android:name=".demo.DemoActivity"/>
+        <activity android:name=".demo.IjkActivity"
+            android:configChanges="orientation|keyboardHidden|screenSize"
+            android:screenOrientation="portrait"/>
+        <activity android:name=".demo.MediaActivity"
+            android:configChanges="orientation|keyboardHidden|screenSize"
+            android:screenOrientation="portrait"/>
+        <activity android:name=".demo.MediaActivity2"
+            android:configChanges="orientation|keyboardHidden|screenSize"
+            android:screenOrientation="portrait"/>
+        <activity android:name=".demo.ExoActivity"
+            android:configChanges="orientation|keyboardHidden|screenSize"
+            android:screenOrientation="portrait"/>
     </application>
 
 </manifest>

+ 1 - 0
app/src/main/java/org/yczbj/ycvideoplayer/BaseApplication.java

@@ -56,6 +56,7 @@ public class BaseApplication extends Application {
                 ScreenDensityUtils.MATCH_BASE_WIDTH,ScreenDensityUtils.MATCH_UNIT_DP);
         //播放器配置,注意:此为全局配置,按需开启
         VideoViewManager.setConfig(VideoPlayerConfig.newBuilder()
+                .setContext(this)
                 .setLogEnabled(true)//调试的时候请打开日志,方便排错
                 .setPlayerFactory(IjkPlayerFactory.create())
                 .build());

+ 2 - 1
app/src/main/java/org/yczbj/ycvideoplayer/MainActivity.java

@@ -10,6 +10,7 @@ import android.widget.TextView;
 import androidx.annotation.Nullable;
 import androidx.appcompat.app.AppCompatActivity;
 
+import org.yczbj.ycvideoplayer.demo.DemoActivity;
 import org.yczbj.ycvideoplayer.newPlayer.activity.TypeActivity;
 import org.yczbj.ycvideoplayer.oldPlayer.OldActivity;
 
@@ -52,7 +53,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
     public void onClick(View v) {
         switch (v.getId()) {
             case R.id.tv_1:
-                startActivity(TypeActivity.class);
+                startActivity(DemoActivity.class);
                 break;
             case R.id.tv_2:
                 startActivity(TypeActivity.class);

+ 62 - 0
app/src/main/java/org/yczbj/ycvideoplayer/demo/DemoActivity.java

@@ -0,0 +1,62 @@
+package org.yczbj.ycvideoplayer.demo;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.TextView;
+
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+
+import org.yczbj.ycvideoplayer.R;
+import org.yczbj.ycvideoplayer.newPlayer.activity.TypeActivity;
+import org.yczbj.ycvideoplayer.oldPlayer.OldActivity;
+
+public class DemoActivity extends AppCompatActivity implements View.OnClickListener {
+
+
+    private TextView mTv1;
+    private TextView mTv12;
+    private TextView mTv2;
+    private TextView mTv3;
+
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_demo_player);
+
+        mTv1 = findViewById(R.id.tv_1);
+        mTv12 = findViewById(R.id.tv_1_2);
+        mTv2 = findViewById(R.id.tv_2);
+        mTv3 = findViewById(R.id.tv_3);
+
+        mTv1.setOnClickListener(this);
+        mTv12.setOnClickListener(this);
+        mTv2.setOnClickListener(this);
+        mTv3.setOnClickListener(this);
+    }
+
+    @Override
+    public void onClick(View v) {
+        switch (v.getId()) {
+            case R.id.tv_1:
+                startActivity(MediaActivity.class);
+                break;
+            case R.id.tv_1_2:
+                startActivity(MediaActivity2.class);
+                break;
+            case R.id.tv_2:
+                startActivity(IjkActivity.class);
+                break;
+            case R.id.tv_3:
+                startActivity(ExoActivity.class);
+                break;
+        }
+    }
+
+    private void startActivity(Class c){
+        startActivity(new Intent(this,c));
+    }
+
+}

+ 53 - 0
app/src/main/java/org/yczbj/ycvideoplayer/demo/ExoActivity.java

@@ -0,0 +1,53 @@
+package org.yczbj.ycvideoplayer.demo;
+
+import android.net.Uri;
+import android.os.Bundle;
+
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+
+import com.google.android.exoplayer2.DefaultLoadControl;
+import com.google.android.exoplayer2.ExoPlayerFactory;
+import com.google.android.exoplayer2.SimpleExoPlayer;
+import com.google.android.exoplayer2.source.ProgressiveMediaSource;
+import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
+import com.google.android.exoplayer2.ui.PlayerView;
+import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
+
+import org.yczbj.ycvideoplayer.ConstantVideo;
+import org.yczbj.ycvideoplayer.R;
+
+public class ExoActivity extends AppCompatActivity {
+
+    private PlayerView mVideoView;
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+        mVideoView.onPause();
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        mVideoView.onResume();
+    }
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_exo_player);
+        mVideoView = findViewById(R.id.video_view);
+
+        SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this,new DefaultTrackSelector(),new DefaultLoadControl());
+        player.setPlayWhenReady(true);
+        mVideoView.setPlayer(player);
+
+        Uri uri = Uri.parse(ConstantVideo.VideoPlayerList[0]);
+        DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("user-agent");
+        ProgressiveMediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
+        // 播放
+        player.prepare(mediaSource);
+    }
+
+}

+ 19 - 0
app/src/main/java/org/yczbj/ycvideoplayer/demo/IjkActivity.java

@@ -0,0 +1,19 @@
+package org.yczbj.ycvideoplayer.demo;
+
+import android.os.Bundle;
+
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+
+import org.yczbj.ycvideoplayer.R;
+
+public class IjkActivity extends AppCompatActivity {
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_ijk_player);
+    }
+
+
+}

+ 74 - 0
app/src/main/java/org/yczbj/ycvideoplayer/demo/MediaActivity.java

@@ -0,0 +1,74 @@
+package org.yczbj.ycvideoplayer.demo;
+
+import android.media.MediaPlayer;
+import android.net.Uri;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import android.widget.MediaController;
+import android.widget.VideoView;
+
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+
+import org.yczbj.ycvideoplayer.ConstantVideo;
+import org.yczbj.ycvideoplayer.R;
+import org.yczbj.ycvideoplayerlib.tool.BaseToast;
+
+public class MediaActivity extends AppCompatActivity {
+
+    private VideoView mVideo;
+    private Button mBtnStart;
+    private Button mBtnPause;
+    private MediaController mMediaController;
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_media_player);
+        initView();
+        initListener();
+    }
+
+    private void initView() {
+        mVideo = findViewById(R.id.video);
+        mBtnStart = findViewById(R.id.btn_start);
+        mBtnPause = findViewById(R.id.btn_pause);
+
+        Uri uri = Uri.parse(ConstantVideo.VideoPlayerList[2]);
+        mVideo.setVideoURI(uri);
+        mMediaController = new MediaController(this);
+        mVideo.setMediaController(mMediaController);
+        mVideo.start();
+        mVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
+            @Override
+            public void onPrepared(MediaPlayer mp) {
+                //         mp.setLooping(true);
+                mp.start();// 播放
+                BaseToast.showRoundRectToast("开始播放!");
+            }
+        });
+        mVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
+            @Override
+            public void onCompletion(MediaPlayer mp) {
+                BaseToast.showRoundRectToast("播放完毕!");
+            }
+        });
+    }
+
+    private void initListener() {
+        mBtnStart.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                mVideo.start();
+            }
+        });
+        mBtnPause.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                mVideo.pause();
+            }
+        });
+    }
+
+}

+ 99 - 0
app/src/main/java/org/yczbj/ycvideoplayer/demo/MediaActivity2.java

@@ -0,0 +1,99 @@
+package org.yczbj.ycvideoplayer.demo;
+
+import android.media.AudioManager;
+import android.media.MediaPlayer;
+import android.net.Uri;
+import android.os.Bundle;
+import android.view.SurfaceView;
+import android.view.View;
+import android.widget.Button;
+import android.widget.MediaController;
+import android.widget.SeekBar;
+import android.widget.VideoView;
+
+import androidx.annotation.Nullable;
+import androidx.appcompat.app.AppCompatActivity;
+
+import org.yczbj.ycvideoplayer.ConstantVideo;
+import org.yczbj.ycvideoplayer.R;
+import org.yczbj.ycvideoplayerlib.tool.BaseToast;
+
+public class MediaActivity2 extends AppCompatActivity {
+
+    private SurfaceView mSvMainSurface;
+    private SeekBar mSeekBar;
+    private Button mStart;
+    private MediaPlayer mediaPlayer;
+
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_media_player2);
+        initView();
+        initMedia();
+        initSurfaceView();
+    }
+
+
+    private void initView() {
+        mSvMainSurface = findViewById(R.id.sv_main_surface);
+        mSeekBar = findViewById(R.id.seekBar);
+        mStart = findViewById(R.id.start);
+        mStart.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                if (mStart.getText().toString().equals("暂停")){
+                    mediaPlayer.start();
+                    mStart.setText("播放");
+                } else {
+                    mediaPlayer.pause();
+                    mStart.setText("暂停");
+                }
+            }
+        });
+    }
+
+    private void initMedia() {
+        mediaPlayer=new MediaPlayer();
+        //设置类型
+        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
+        /* 得到文件路径 *//* 注:文件存放在SD卡的根目录,一定要进行prepare()方法,使硬件进行准备 */
+        try{
+            Uri uri = Uri.parse(ConstantVideo.VideoPlayerList[2]);
+            /* 为MediaPlayer 设置数据源 */
+            mediaPlayer.setDataSource(this,uri);
+            /* 准备 */
+            mediaPlayer.prepare();
+            //将播放器捕捉的画面展示到SurfaceView画面上
+            mediaPlayer.setDisplay(mSvMainSurface.getHolder());
+        }catch(Exception ex){
+            ex.printStackTrace();
+        }
+        mediaPlayer.start();
+        // 把图标变为暂停图标
+        mStart.setText("暂停");
+        //获取音乐的总时长
+        int duration=mediaPlayer.getDuration();
+        //设置进度条的最大值为音乐总时长
+        mSeekBar.setMax(duration);
+
+    }
+
+
+    private void initSurfaceView() {
+        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {}
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {}
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {
+                //获取拖动结束之后的位置
+                int progress=seekBar.getProgress();
+                //跳转到某个位置播放
+                mediaPlayer.seekTo(progress);
+            }
+        });
+    }
+
+}

+ 74 - 0
app/src/main/res/layout/activity_demo_player.xml

@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="@color/whiteBg">
+
+    <androidx.appcompat.widget.Toolbar
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        xmlns:app="http://schemas.android.com/apk/res-auto"
+        android:id="@+id/toolbar"
+        android:layout_width="match_parent"
+        android:layout_height="50dp"
+        android:background="@color/colorTheme"
+        android:theme="@style/AppTheme.ActionBar"
+        app:contentInsetStart="0.0dp"
+        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
+        android:visibility="visible">
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="center"
+            android:text="播放视频的原生案例"
+            android:textSize="18sp"
+            android:textColor="@color/blackText"/>
+    </androidx.appcompat.widget.Toolbar>
+
+
+    <ScrollView
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="vertical"
+            android:padding="10dp">
+            <TextView
+                android:id="@+id/tv_1"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="10dp"
+                android:padding="10dp"
+                android:background="@color/colorAccent"
+                android:text="1.最原生的播放器,使用VideoView"/>
+            <TextView
+                android:id="@+id/tv_1_2"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="10dp"
+                android:padding="10dp"
+                android:background="@color/colorAccent"
+                android:text="1.2 最原生的播放器,使用SurfaceView+MediaPlayer"/>
+            <TextView
+                android:id="@+id/tv_2"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="10dp"
+                android:padding="10dp"
+                android:background="@color/colorAccent"
+                android:text="2.ijk原生播放器"/>
+            <TextView
+                android:id="@+id/tv_3"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_marginTop="10dp"
+                android:padding="10dp"
+                android:background="@color/colorAccent"
+                android:text="3.exo原生播放器"/>
+        </LinearLayout>
+    </ScrollView>
+
+
+</LinearLayout>

+ 11 - 0
app/src/main/res/layout/activity_exo_player.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical" android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <com.google.android.exoplayer2.ui.PlayerView
+        android:id="@+id/video_view"
+        android:layout_width="match_parent"
+        android:layout_height="300dp"/>
+
+</LinearLayout>

+ 8 - 0
app/src/main/res/layout/activity_ijk_player.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical" android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+
+
+</LinearLayout>

+ 29 - 0
app/src/main/res/layout/activity_media_player.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+
+    <VideoView
+        android:id="@+id/video"
+        android:layout_width="match_parent"
+        android:layout_height="240dp"
+        android:background="@android:color/black"/>
+
+    <Button
+        android:id="@+id/btn_start"
+        android:layout_marginTop="5dp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="播放" />
+
+    <Button
+        android:id="@+id/btn_pause"
+        android:layout_marginTop="5dp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:text="暂停" />
+
+</LinearLayout>

+ 29 - 0
app/src/main/res/layout/activity_media_player2.xml

@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <SurfaceView
+        android:layout_width="match_parent"
+        android:layout_height="300dp"
+        android:id="@+id/sv_main_surface" />
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal">
+        <SeekBar
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_weight="1"
+            android:id="@+id/seekBar" />
+
+        <Button
+            android:id="@+id/start"
+            android:layout_marginTop="5dp"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:text="播放" />
+    </LinearLayout>
+</LinearLayout>

+ 4 - 1
read/01.视频播放器介绍文档.md

@@ -361,7 +361,10 @@
 ### 16.版本更新文档记录
 
 
-
+### 17.参考项目和博客记录
+- 项目
+    - b站demo:https://github.com/bilibili/ijkplayer/tree/master/android
+    - 谷歌demo:https://github.com/google/ExoPlayer