Skip to content

Commit

Permalink
1.新增清晰度切换功能
Browse files Browse the repository at this point in the history
2.优化代码
3.修复一些Bug
  • Loading branch information
jianjunxiao committed Jul 7, 2017
1 parent 59b60cd commit c05df0c
Show file tree
Hide file tree
Showing 51 changed files with 1,603 additions and 280 deletions.
160 changes: 94 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 支持本地和网络视频播放.
* 完美切换小窗口、全屏,可在RecyclerView、ListView中无缝全屏.
* 手势滑动调节播放进度、亮度、声音.
* 可自定义控制界面
* 支持清晰度切换.
* 可自定义控制界面.

### Usage
下载niceviewoplayer库,在AndroidSutio中作为Mudule添加依赖。
Expand All @@ -21,56 +22,99 @@ allprojects {
}
dependencies {
compile 'com.github.xiaoyanger0825:NiceVieoPlayer:v1.6'
compile 'com.github.xiaoyanger0825:NiceVieoPlayer:v1.7'
}
```

**在Activity中使用时,该Activity需要继承自AppCompatActivity。在Fragment中使用时,外层的Activity也同样需要继承自AppCompatActivity**
#### 1.在Activity中使用
在Activity中使用时,该Activity需要继承自`AppCompatActivity`,在onStop中需要释放播放器,并且要在onBackPress中处理按下返回键的逻辑:

1.在Activity中使用:
```
private void init() {
mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.PLAYER_TYPE_IJK); // or NiceVideoPlayer.PLAYER_NATIVE
mNiceVideoPlayer.setUp(mVideoUrl, null);
// 默认的控制器,如需自定义,可继承NiceVideoPlayer自己实现
TxVideoPlayerController controller = new TxVideoPlayerController(this);
controller.setTitle(mTitle);
controller.setImage(mImageUrl);
mNiceVideoPlayer.setController(controller);
}
// 按下Home键暂停播放,回到界面继续播放。
@Override
protected void onStop() {
NiceVideoPlayerManager.instance().pauseNiceVideoPlayer();
super.onStop();
}
@Override
protected void onRestart() {
NiceVideoPlayerManager.instance().restartNiceVideoPlayer();
super.onRestart();
}
// 按返回键
// 当前是全屏或小窗口,需要先退出全屏或小窗口。
@Override
public void onBackPressed() {
if (NiceVideoPlayerManager.instance().onBackPressd()) {
return;
}
super.onBackPressed();
}
```
private void init() {
mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.TYPE_IJK); // or NiceVideoPlayer.TYPE_NATIVE
mNiceVideoPlayer.setUp(mVideoUrl, null);
@Override
protected void onDestroy() {
// 很重要,在Activity和Fragment的onStop方法中一定要调用,释放的播放器。
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
super.onDestroy();
}
```
2.在RecyclerView列表中使用需要监听itemView detach:
TxVideoPlayerController controller = new TxVideoPlayerController(this);
controller.setTitle(mTitle);
controller.setImage(mImageUrl);
mNiceVideoPlayer.setController(controller);
}
@Override
protected void onStop() {
super.onStop();
// 在onStop时释放掉播放器
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
@Override
public void onBackPressed() {
// 在全屏或者小窗口时按返回键要先退出全屏或小窗口,
// 所以在Activity中onBackPress要交给NiceVideoPlayer先处理。
if (NiceVideoPlayerManager.instance().onBackPressd()) return;
super.onBackPressed();
}
```
详细可参考demo中的`TinyWindowPlayActivity``ChangeClarityActivity``RecyclerViewActivity`.
#### 2.在Fragment中使用
在Fragment中使用时,该Fragment外层的Activity需要继承自`AppCompatActivity`,并且也要处理返回键按下逻辑:
```
public class XXXActivity extends AppCompatActivity {
...
@Override
public void onBackPressed() {
// 在全屏或者小窗口时按返回键要先退出全屏或小窗口,
// 所以在Activity中onBackPress要交给NiceVideoPlayer先处理。
if (NiceVideoPlayerManager.instance().onBackPressd()) return;
super.onBackPressed();
}
...
}
```
同时在Fragment中的onStop方法中释放播放器:
```
public class XXXFragenment extends Fragment {
...
@Override
public void onStop() {
super.onStop();
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
...
}
```
详细可参考demo中的`UseInFragActivity``DemoFragment`.

#### 3.切换清晰度
如果需要切换清晰度,需要在`controller`中配置清晰度相关的等级和视频链接地址:
```
private void init() {
mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.TYPE_IJK); // IjkPlayer or MediaPlayer
TxVideoPlayerController controller = new TxVideoPlayerController(this);
controller.setTitle(mTitle);
controller.setClarity(getClarites(), 0); // 设置清晰度以及默认播放的清晰度
Glide.with(this)
.load(mImageUrl)
.placeholder(R.drawable.img_default)
.crossFade()
.into(controller.imageView());
mNiceVideoPlayer.setController(controller);
}
public List<Clarity> getClarites() {
List<Clarity> clarities = new ArrayList<>();
clarities.add(new Clarity("标清", "270P", "http://play.g3proxy.lecloud.com/vod/v2/MjUxLzE2LzgvbGV0di11dHMvMTQvdmVyXzAwXzIyLTExMDc2NDEzODctYXZjLTE5OTgxOS1hYWMtNDgwMDAtNTI2MTEwLTE3MDg3NjEzLWY1OGY2YzM1NjkwZTA2ZGFmYjg2MTVlYzc5MjEyZjU4LTE0OTg1NTc2ODY4MjMubXA0?b=259&mmsid=65565355&tm=1499247143&key=f0eadb4f30c404d49ff8ebad673d3742&platid=3&splatid=345&playid=0&tss=no&vtype=21&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
clarities.add(new Clarity("高清", "480P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
clarities.add(new Clarity("超清", "720P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
clarities.add(new Clarity("蓝光", "1080P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
return clarities;
}
```
详细参考demo中的`ChangeClarityActivity`

#### 4.在RecyclerView列表中使用
在ReclerView列表中使用时需要监听itemView的detach,以此释放掉对应的播放器
```
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
@Override
Expand All @@ -87,32 +131,16 @@ mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttach
}
});
```
3.自定义控制界面
详细参考demo中的`RecyclerViewActivity`.
#### 5.播放时Home键按下以及回到播放界面的处理
按照上面的做法,在onStop直接释放掉播放器,那么在播放时按下Home键播放器也会被释放掉,如果在此回到播放界面,播放器回到最初始的状态。如果需要在播放的时候按下Home键只是暂停播放器,重新回到播放界面时又继续播放,那么可以参考demo中的`CompatHomeKeyActiivty`,或者对应的Activity集成自`CompatHomeKeyActiivty`,详细参考demo中的`ProcessHome1Activity`。当然,如果是在Fragment中,参考`CompatKeyFragment`,或者继承自`CompatKeyFragment`(外层的Activity还是继承自AppCompat,并处理onBackPress),详细参考demo中的`ProcessHome2Activity`.

#### 5.自定义控制界面
```
public class CustomController extends NiceVideoPlayerController {
// 实现自己的控制界面
...
}
...
public class MainActivity extends AppCompatActivity {
private NiceVideoPlayer mNiceVideoPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 使用自定义的CustomContorller
mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
CustomContorller controller = new CostomController(this);
...// mNiceVideoPlayerh和controller的其他设置
mNiceVideoPlayer.setController(controller);
}
}
```
### Proguard
```
Expand Down
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

productFlavors {
}
}
Expand All @@ -37,4 +37,5 @@ dependencies {
testCompile rootProject.ext.dependencies.junit
compile rootProject.ext.dependencies.recyclerview
compile rootProject.ext.dependencies.glide
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
37 changes: 33 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,47 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".example.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait">
<activity android:name=".example.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".example.TinyWindowPlayActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="可使用小窗口模式播放"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".example.RecyclerViewActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="在RecylerView视频列表中"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".example.ChangeClarityActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="在全屏时可以切换清晰度"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".example.UseInFragActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="在fragment中使用"
android:screenOrientation="portrait">
</activity>
<activity
android:label="Activity中时处理播放时按下Home键"
android:name=".example.ProcessHome1Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait">
</activity>
<activity
android:label="Fragment中时处理播放时按下Home键"
android:name=".example.ProcessHome2Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait">
</activity>
</application>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.xiao.nicevieoplayer.example;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.xiao.nicevideoplayer.Clarity;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevideoplayer.TxVideoPlayerController;
import com.xiao.nicevieoplayer.R;

import java.util.ArrayList;
import java.util.List;

public class ChangeClarityActivity extends AppCompatActivity {

private NiceVideoPlayer mNiceVideoPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_clarity);
init();
}

private void init() {
mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.TYPE_IJK); // IjkPlayer or MediaPlayer
TxVideoPlayerController controller = new TxVideoPlayerController(this);
controller.setTitle("Beautiful China...");
controller.setLenght(117000);
controller.setClarity(getClarites(), 0);
Glide.with(this)
.load("http://imgsrc.baidu.com/image/c0%3Dshijue%2C0%2C0%2C245%2C40/sign=304dee3ab299a9012f38537575fc600e/91529822720e0cf3f8b77cd50046f21fbe09aa5f.jpg")
.placeholder(R.drawable.img_default)
.crossFade()
.into(controller.imageView());
mNiceVideoPlayer.setController(controller);
}

public List<Clarity> getClarites() {
List<Clarity> clarities = new ArrayList<>();
clarities.add(new Clarity("标清", "270P", "http://play.g3proxy.lecloud.com/vod/v2/MjUxLzE2LzgvbGV0di11dHMvMTQvdmVyXzAwXzIyLTExMDc2NDEzODctYXZjLTE5OTgxOS1hYWMtNDgwMDAtNTI2MTEwLTE3MDg3NjEzLWY1OGY2YzM1NjkwZTA2ZGFmYjg2MTVlYzc5MjEyZjU4LTE0OTg1NTc2ODY4MjMubXA0?b=259&mmsid=65565355&tm=1499247143&key=f0eadb4f30c404d49ff8ebad673d3742&platid=3&splatid=345&playid=0&tss=no&vtype=21&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
clarities.add(new Clarity("高清", "480P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
clarities.add(new Clarity("超清", "720P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
clarities.add(new Clarity("蓝光", "1080P", "http://play.g3proxy.lecloud.com/vod/v2/MjQ5LzM3LzIwL2xldHYtdXRzLzE0L3Zlcl8wMF8yMi0xMTA3NjQxMzkwLWF2Yy00MTk4MTAtYWFjLTQ4MDAwLTUyNjExMC0zMTU1NTY1Mi00ZmJjYzFkNzA1NWMyNDc4MDc5OTYxODg1N2RjNzEwMi0xNDk4NTU3OTYxNzQ4Lm1wNA==?b=479&mmsid=65565355&tm=1499247143&key=98c7e781f1145aba07cb0d6ec06f6c12&platid=3&splatid=345&playid=0&tss=no&vtype=13&cvid=2026135183914&payff=0&pip=08cc52f8b09acd3eff8bf31688ddeced&format=0&sign=mb&dname=mobile&expect=1&tag=mobile&xformat=super"));
return clarities;
}

@Override
protected void onStop() {
super.onStop();
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}

@Override
public void onBackPressed() {
if (NiceVideoPlayerManager.instance().onBackPressd()) return;
super.onBackPressed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.xiao.nicevieoplayer.example;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevieoplayer.R;
import com.xiao.nicevieoplayer.example.adapter.VideoAdapter;
import com.xiao.nicevieoplayer.example.util.DataUtil;

/**
* Created by XiaoJianjun on 2017/7/7.
* 如果你需要在播放的时候按下Home键能暂停,回调此Fragment又继续的话,需要继承自CompatHomeKeyFragment
*/
public class DemoFragenment extends Fragment {

private RecyclerView mRecyclerView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_fragment_demo, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}

private void init() {
mRecyclerView = (RecyclerView) getView().findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setHasFixedSize(true);
VideoAdapter adapter = new VideoAdapter(getActivity(), DataUtil.getVideoListData());
mRecyclerView.setAdapter(adapter);
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
@Override
public void onChildViewAttachedToWindow(View view) {

}

@Override
public void onChildViewDetachedFromWindow(View view) {
NiceVideoPlayer niceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
if (niceVideoPlayer == NiceVideoPlayerManager.instance().getCurrentNiceVideoPlayer()) {
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
}
});
}

@Override
public void onStop() {
super.onStop();
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
}
Loading

0 comments on commit c05df0c

Please sign in to comment.