Skip to content

Commit

Permalink
1.重构IjkViewView代码 2.将弹幕播放相关代码抽离到DanmakuVideoView
Browse files Browse the repository at this point in the history
  • Loading branch information
Doikki committed Dec 22, 2017
1 parent 613a79f commit fb5eb7d
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 160 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait" />
<activity
android:name="com.devlin_n.dcplayer.activity.RecyclerViewAutoPlayActivity"
android:name="com.devlin_n.dcplayer.activity.AutoPlayRecyclerViewActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait" />
</application>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/devlin_n/dcplayer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.devlin_n.dcplayer.activity.LivePlayerActivity;
import com.devlin_n.dcplayer.activity.PlayerActivity;
import com.devlin_n.dcplayer.activity.RecyclerViewActivity;
import com.devlin_n.dcplayer.activity.RecyclerViewAutoPlayActivity;
import com.devlin_n.dcplayer.activity.AutoPlayRecyclerViewActivity;
import com.devlin_n.dcplayer.activity.VodPlayerActivity;
import com.devlin_n.videoplayer.player.BackgroundPlayService;
import com.devlin_n.videoplayer.player.VideoCacheManager;
Expand Down Expand Up @@ -122,6 +122,6 @@ public void list(View view) {
}

public void recyclerAutoPlay(View view) {
startActivity(new Intent(this, RecyclerViewAutoPlayActivity.class));
startActivity(new Intent(this, AutoPlayRecyclerViewActivity.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
* Created by Devlin_n on 2017/5/31.
*/

public class RecyclerViewAutoPlayActivity extends AppCompatActivity {
public class AutoPlayRecyclerViewActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle("RECYCLER");
actionBar.setTitle("AUTO PLAY RECYCLER VIEW");
actionBar.setDisplayHomeAsUpEnabled(true);
}
initView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.view.View;
import android.widget.Toast;

import com.devlin_n.dcplayer.widget.DanmukuVideoView;
import com.devlin_n.floatWindowPermission.FloatWindowManager;
import com.devlin_n.videoplayer.controller.StandardVideoController;
import com.devlin_n.videoplayer.player.IjkVideoView;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class DanmakuActivity extends AppCompatActivity {
private DanmakuView mDanmakuView;
private DanmakuContext mContext;
private BaseDanmakuParser mParser;
private IjkVideoView ijkVideoView;
private DanmukuVideoView danmukuVideoView;
private static final String URL_VOD = "http://mov.bn.netease.com/open-movie/nos/flv/2017/01/03/SC8U8K7BC_hd.flv";
// private static final String URL_VOD = "http://uploads.cutv.com:8088/video/data/201703/10/encode_file/515b6a95601ba6b39620358f2677a17358c2472411d53.mp4";

Expand All @@ -62,14 +63,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
actionBar.setTitle("弹幕");
actionBar.setDisplayHomeAsUpEnabled(true);
}
ijkVideoView = (IjkVideoView) findViewById(R.id.player);
danmukuVideoView = findViewById(R.id.player);

initDanMuView();
ijkVideoView
danmukuVideoView
.addDanmukuView(mDanmakuView, mContext, mParser)
// .enableCache()
// .useSurfaceView()
// .useAndroidMediaPlayer()
.addDanmukuView(mDanmakuView, mContext, mParser)
.autoRotate()
.setVideoController(new StandardVideoController(this))
.setUrl(URL_VOD)
Expand All @@ -88,25 +89,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
protected void onPause() {
super.onPause();
ijkVideoView.pause();
danmukuVideoView.pause();
}

@Override
protected void onResume() {
super.onResume();
ijkVideoView.stopFloatWindow();
danmukuVideoView.stopFloatWindow();
}

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


@Override
public void onBackPressed() {
if (!ijkVideoView.onBackPressed()) {
if (!danmukuVideoView.onBackPressed()) {
super.onBackPressed();
}
}
Expand All @@ -115,7 +116,7 @@ public void onBackPressed() {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FloatWindowManager.PERMISSION_REQUEST_CODE) {
if (FloatWindowManager.getInstance().checkPermission(this)) {
ijkVideoView.startFloatWindow();
danmukuVideoView.startFloatWindow();
} else {
Toast.makeText(DanmakuActivity.this, "权限授予失败,无法开启悬浮窗", Toast.LENGTH_SHORT).show();
}
Expand Down Expand Up @@ -209,7 +210,7 @@ public void addDanmakuWithDrawable(View view) {
}
// for(int i=0;i<100;i++){
// }
Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher_round);
int size = WindowUtil.dp2px(this, 20);
drawable.setBounds(0, 0, size, size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
videos.add(new VideoModel(URL_VOD, "这是一个标题", new StandardVideoController(this)));

ijkVideoView
.autoRotate()
// .autoRotate()
// .enableCache()
// .useSurfaceView()
.useAndroidMediaPlayer()
Expand Down
108 changes: 108 additions & 0 deletions app/src/main/java/com/devlin_n/dcplayer/widget/DanmukuVideoView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.devlin_n.dcplayer.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.devlin_n.videoplayer.player.IjkVideoView;

import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

/**
* 包含弹幕的播放器
* Created by xinyu on 2017/12/23.
*/

public class DanmukuVideoView extends IjkVideoView {
private DanmakuView mDanmakuView;
private DanmakuContext mContext;
private BaseDanmakuParser mParser;


public DanmukuVideoView(@NonNull Context context) {
super(context);
}

public DanmukuVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public DanmukuVideoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void initPlayer() {
super.initPlayer();
if (mDanmakuView != null) {
playerContainer.removeView(mDanmakuView);
playerContainer.addView(mDanmakuView, 1);
}
}


@Override
protected void startPrepare() {
super.startPrepare();
if (mDanmakuView != null) {
mDanmakuView.prepare(mParser, mContext);
}
}

@Override
protected void startInPlaybackState() {
super.startInPlaybackState();
if (mDanmakuView != null && mDanmakuView.isPrepared() && mDanmakuView.isPaused()) {
mDanmakuView.resume();
}
}

@Override
public void pause() {
super.pause();
if (isInPlaybackState()) {
if (mDanmakuView != null && mDanmakuView.isPrepared()) {
mDanmakuView.pause();
}
}
}

@Override
public void resume() {
super.resume();
if (mDanmakuView != null && mDanmakuView.isPrepared() && mDanmakuView.isPaused()) {
mDanmakuView.resume();
}
}

@Override
public void release() {
super.release();
if (mDanmakuView != null) {
// dont forget release!
mDanmakuView.release();
mDanmakuView = null;
}
}

@Override
public void seekTo(int pos) {
super.seekTo(pos);
if (isInPlaybackState()) {
if (mDanmakuView != null) mDanmakuView.seekTo((long) pos);
}
}

/**
* 添加弹幕
*/
public IjkVideoView addDanmukuView(DanmakuView danmakuView, DanmakuContext context, BaseDanmakuParser parser) {
this.mDanmakuView = danmakuView;
this.mContext = context;
this.mParser = parser;
return this;
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_danmaku_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_height="match_parent"
android:orientation="vertical">

<com.devlin_n.videoplayer.player.IjkVideoView
<com.devlin_n.dcplayer.widget.DanmukuVideoView
android:id="@+id/player"
android:layout_width="match_parent"
android:layout_height="300dp" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="recyclerAutoPlay"
android:text="RECYCLER VIEW AUTO PLAY" />
android:text="AUTO PLAY RECYCLER VIEW" />

<Button
android:layout_width="match_parent"
Expand Down
Loading

0 comments on commit fb5eb7d

Please sign in to comment.