Commit c65901e16266fe88c4ada1f90bb605e98c9fefbd
1 parent
e5e73e59
添加无缝切换视频的测试demo
Showing
5 changed files
with
204 additions
and
6 deletions
app/src/main/AndroidManifest.xml
| ... | ... | @@ -6,7 +6,7 @@ |
| 6 | 6 | <uses-permission android:name="android.permission.WAKE_LOCK" /> |
| 7 | 7 | <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
| 8 | 8 | <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> |
| 9 | - <uses-permission android:name="android.permission.READ_PHONE_STATE"/> | |
| 9 | + <uses-permission android:name="android.permission.READ_PHONE_STATE" /> | |
| 10 | 10 | <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
| 11 | 11 | |
| 12 | 12 | <application |
| ... | ... | @@ -23,8 +23,6 @@ |
| 23 | 23 | <category android:name="android.intent.category.LAUNCHER" /> |
| 24 | 24 | </intent-filter> |
| 25 | 25 | </activity> |
| 26 | - | |
| 27 | - | |
| 28 | 26 | <activity |
| 29 | 27 | android:name=".MediaPlayerActivity" |
| 30 | 28 | android:configChanges="keyboardHidden|screenSize|orientation" |
| ... | ... | @@ -33,12 +31,12 @@ |
| 33 | 31 | <activity |
| 34 | 32 | android:name=".VideoViewActivity" |
| 35 | 33 | android:configChanges="keyboardHidden|screenSize|orientation" |
| 36 | - android:screenOrientation="portrait"/> | |
| 34 | + android:screenOrientation="portrait" /> | |
| 37 | 35 | <activity |
| 38 | 36 | android:name=".BarrageActivity" |
| 39 | 37 | android:configChanges="keyboardHidden|screenSize|orientation" |
| 40 | - android:screenOrientation="portrait"/> | |
| 41 | - | |
| 38 | + android:screenOrientation="portrait" /> | |
| 39 | + <activity android:name=".TestActivity"></activity> | |
| 42 | 40 | </application> |
| 43 | 41 | |
| 44 | 42 | </manifest> |
| 45 | 43 | \ No newline at end of file | ... | ... |
app/src/main/java/com/cnlive/demo/video/MainActivity.java
| ... | ... | @@ -28,6 +28,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe |
| 28 | 28 | case R.id.barrageBtn: |
| 29 | 29 | startActivity(new Intent(this, BarrageActivity.class)); |
| 30 | 30 | break; |
| 31 | + case R.id.changePlayer: | |
| 32 | + startActivity(new Intent(this, TestActivity.class)); | |
| 33 | + break; | |
| 31 | 34 | } |
| 32 | 35 | } |
| 33 | 36 | } | ... | ... |
app/src/main/java/com/cnlive/demo/video/TestActivity.java
0 → 100644
| 1 | +package com.cnlive.demo.video; | |
| 2 | + | |
| 3 | +import android.support.v7.app.AppCompatActivity; | |
| 4 | +import android.os.Bundle; | |
| 5 | +import android.view.SurfaceHolder; | |
| 6 | +import android.view.SurfaceView; | |
| 7 | +import android.view.TextureView; | |
| 8 | +import android.view.View; | |
| 9 | +import android.widget.Button; | |
| 10 | +import android.widget.Toast; | |
| 11 | + | |
| 12 | +import com.cnlive.libs.video.video.MediaPlayer; | |
| 13 | +import com.cnlive.libs.video.video.base.IMediaPlayer; | |
| 14 | + | |
| 15 | +import java.io.IOException; | |
| 16 | + | |
| 17 | +public class TestActivity extends AppCompatActivity { | |
| 18 | + | |
| 19 | + private MediaPlayer firstPlayer; | |
| 20 | + private MediaPlayer secondPlayer; | |
| 21 | + | |
| 22 | + //负责配合mediaPlayer显示视频图像播放的surfaceView | |
| 23 | + private SurfaceView surface; | |
| 24 | + private SurfaceHolder surfaceHolder; | |
| 25 | + | |
| 26 | + private boolean isFirst = true; | |
| 27 | + | |
| 28 | + private Button changeBtn; | |
| 29 | + private Button prepareBtn; | |
| 30 | + | |
| 31 | + @Override | |
| 32 | + protected void onCreate(Bundle savedInstanceState) { | |
| 33 | + super.onCreate(savedInstanceState); | |
| 34 | + setContentView(R.layout.activity_test); | |
| 35 | + | |
| 36 | + surface = (SurfaceView) findViewById(R.id.surfaceView); | |
| 37 | + surfaceHolder = surface.getHolder(); | |
| 38 | + changeBtn = (Button) findViewById(R.id.prepare); | |
| 39 | + changeBtn.setOnClickListener(listener); | |
| 40 | + | |
| 41 | + prepareBtn = (Button) findViewById(R.id.create); | |
| 42 | + prepareBtn.setOnClickListener(listener); | |
| 43 | + | |
| 44 | + initFirstPlayer(true); | |
| 45 | + } | |
| 46 | + | |
| 47 | + View.OnClickListener listener = new View.OnClickListener() { | |
| 48 | + @Override | |
| 49 | + public void onClick(View v) { | |
| 50 | + //判断切换到哪一个MediaPlayer | |
| 51 | + switch (v.getId()) { | |
| 52 | + case R.id.prepare: | |
| 53 | + if (isFirst) { | |
| 54 | + //点击按钮切换到第二个MediaPlayer | |
| 55 | + firstPlayer.setDisplay(null); | |
| 56 | +// secondPlayer.setDisplay(surfaceHolder); | |
| 57 | + secondPlayer.setPlayerMute(0); | |
| 58 | + firstPlayer.release(); | |
| 59 | + firstPlayer = null; | |
| 60 | + isFirst = false; | |
| 61 | + } else { | |
| 62 | + //点击按钮切换到第一个MediaPlayer | |
| 63 | + secondPlayer.setDisplay(null); | |
| 64 | +// firstPlayer.setDisplay(surfaceHolder); | |
| 65 | + firstPlayer.setPlayerMute(0); | |
| 66 | + secondPlayer.release(); | |
| 67 | + secondPlayer = null; | |
| 68 | + isFirst = true; | |
| 69 | + } | |
| 70 | + break; | |
| 71 | + case R.id.create: | |
| 72 | + if (firstPlayer == null) { | |
| 73 | + initFirstPlayer(false); | |
| 74 | + } | |
| 75 | + if (secondPlayer == null) | |
| 76 | + initNextPlayer(); | |
| 77 | + } | |
| 78 | + } | |
| 79 | + }; | |
| 80 | + | |
| 81 | + private void initFirstPlayer(final boolean isFirstTime) { | |
| 82 | + //初始化第一个MediaPlayer,在本demo中需要传入是否是第一次初始化,如果不是第一次则需要将player设置为静音 | |
| 83 | + firstPlayer = new MediaPlayer(getBaseContext()); | |
| 84 | + firstPlayer.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() { | |
| 85 | + @Override | |
| 86 | + public void onPrepared(IMediaPlayer iMediaPlayer) { | |
| 87 | + firstPlayer.setDisplay(surfaceHolder); | |
| 88 | + firstPlayer.start(); | |
| 89 | + if (isFirstTime) { | |
| 90 | + firstPlayer.setPlayerMute(0); | |
| 91 | + } else { | |
| 92 | + firstPlayer.setPlayerMute(1); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + }); | |
| 96 | + | |
| 97 | + firstPlayer.setBufferTimeMax(3); | |
| 98 | + firstPlayer.setTimeout(5, 30); | |
| 99 | + | |
| 100 | + | |
| 101 | + try { | |
| 102 | + firstPlayer.setDataSource("rtmp://live.hkstv.hk.lxdns.com/live/hks"); | |
| 103 | + firstPlayer.prepareAsync(); | |
| 104 | + } catch (IOException e) { | |
| 105 | + e.printStackTrace(); | |
| 106 | + } | |
| 107 | + } | |
| 108 | + | |
| 109 | + private void initNextPlayer() { | |
| 110 | + //初始化第二个MediaPlayer | |
| 111 | + secondPlayer = new MediaPlayer(TestActivity.this); | |
| 112 | + secondPlayer.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() { | |
| 113 | + @Override | |
| 114 | + public void onPrepared(IMediaPlayer iMediaPlayer) { | |
| 115 | + secondPlayer.setDisplay(surfaceHolder); | |
| 116 | + secondPlayer.start(); | |
| 117 | + secondPlayer.setPlayerMute(1); | |
| 118 | + } | |
| 119 | + }); | |
| 120 | + | |
| 121 | + secondPlayer.setBufferTimeMax(3); | |
| 122 | + secondPlayer.setTimeout(5, 30); | |
| 123 | + | |
| 124 | + try { | |
| 125 | + secondPlayer.setDataSource("http://uv01.cnlive.com/record/live/82_20161126/hls/82_20161126-82_20161126.m3u8"); | |
| 126 | + secondPlayer.prepareAsync(); | |
| 127 | + } catch (IOException e) { | |
| 128 | + e.printStackTrace(); | |
| 129 | + } | |
| 130 | + | |
| 131 | + } | |
| 132 | + | |
| 133 | + | |
| 134 | + @Override | |
| 135 | + protected void onResume() { | |
| 136 | + super.onResume(); | |
| 137 | + if (firstPlayer != null) { | |
| 138 | + firstPlayer.start(); | |
| 139 | + } | |
| 140 | + if (secondPlayer != null) { | |
| 141 | + secondPlayer.start(); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + | |
| 145 | + @Override | |
| 146 | + protected void onDestroy() { | |
| 147 | + super.onDestroy(); | |
| 148 | + if (firstPlayer != null) { | |
| 149 | + if (firstPlayer.isPlaying()) { | |
| 150 | + firstPlayer.stop(); | |
| 151 | + } | |
| 152 | + firstPlayer.release(); | |
| 153 | + } | |
| 154 | + if (secondPlayer != null) { | |
| 155 | + if (secondPlayer.isPlaying()) { | |
| 156 | + secondPlayer.stop(); | |
| 157 | + } | |
| 158 | + secondPlayer.release(); | |
| 159 | + } | |
| 160 | + | |
| 161 | + } | |
| 162 | +} | ... | ... |
app/src/main/res/layout/activity_main.xml
| ... | ... | @@ -30,4 +30,12 @@ |
| 30 | 30 | android:text="弹幕" |
| 31 | 31 | android:layout_below="@+id/videoId" |
| 32 | 32 | android:onClick="onClick"/> |
| 33 | + | |
| 34 | + <Button | |
| 35 | + android:id="@+id/changePlayer" | |
| 36 | + android:layout_width="wrap_content" | |
| 37 | + android:layout_height="wrap_content" | |
| 38 | + android:text="无缝切换视频" | |
| 39 | + android:layout_below="@+id/barrageBtn" | |
| 40 | + android:onClick="onClick"/> | |
| 33 | 41 | </RelativeLayout> | ... | ... |
app/src/main/res/layout/activity_test.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="utf-8"?> | |
| 2 | +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| 3 | + xmlns:tools="http://schemas.android.com/tools" | |
| 4 | + android:id="@+id/activity_test" | |
| 5 | + android:layout_width="match_parent" | |
| 6 | + android:layout_height="match_parent" | |
| 7 | + tools:context="com.cnlive.demo.video.TestActivity"> | |
| 8 | + | |
| 9 | + <SurfaceView | |
| 10 | + android:id="@+id/surfaceView" | |
| 11 | + android:layout_width="match_parent" | |
| 12 | + android:layout_height="200dp" | |
| 13 | + android:layout_centerInParent="true" /> | |
| 14 | + | |
| 15 | + <Button | |
| 16 | + android:id="@+id/prepare" | |
| 17 | + android:layout_width="match_parent" | |
| 18 | + android:layout_height="wrap_content" | |
| 19 | + android:layout_below="@+id/create" | |
| 20 | + android:text="播放视频" /> | |
| 21 | + | |
| 22 | + <Button | |
| 23 | + android:id="@+id/create" | |
| 24 | + android:layout_width="match_parent" | |
| 25 | + android:layout_height="wrap_content" | |
| 26 | + android:text="准备视频" /> | |
| 27 | +</RelativeLayout> | ... | ... |