提交 95f1a33b 编写于 作者: S shuyu

1.4.5 增加了EXOPlayer的播放切换支持

上级 82b237d8
......@@ -2,6 +2,9 @@ package com.example.gsyvideoplayer;
import android.app.Application;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
//import com.squareup.leakcanary.LeakCanary;
/**
......@@ -20,5 +23,6 @@ public class GSYApplication extends Application {
//}
//LeakCanary.install(this);
//GSYVideoType.enableMediaCodec();
GSYVideoManager.instance().setVideoType(this, GSYVideoType.IJKEXOPLAYER);
}
}
......@@ -3,6 +3,7 @@ package com.shuyu.gsyvideoplayer;
import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
......@@ -22,9 +23,12 @@ import com.shuyu.gsyvideoplayer.utils.FileUtils;
import com.shuyu.gsyvideoplayer.utils.StorageUtils;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Map;
import tv.danmaku.ijk.media.exo.IjkExoMediaPlayer;
import tv.danmaku.ijk.media.player.AbstractMediaPlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
......@@ -46,7 +50,7 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
public static final int HANDLER_SETDISPLAY = 1;
public static final int HANDLER_RELEASE = 2;
private IjkMediaPlayer mediaPlayer;
private AbstractMediaPlayer mediaPlayer;
private HandlerThread mMediaHandlerThread;
private MediaHandler mMediaHandler;
private Handler mainThreadHandler;
......@@ -60,6 +64,8 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
private String playTag = ""; //播放的tag,防止错位置,因为普通的url也可能重复
private Context context;
private int currentVideoWidth = 0; //当前播放的视频宽的高
private int currentVideoHeight = 0; //当前播放的视屏的高
......@@ -70,6 +76,8 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
private int buffterPoint;
private int videoType = GSYVideoType.IJKPLAYER;
public static synchronized GSYVideoManager instance() {
if (videoManager == null) {
......@@ -241,16 +249,13 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
currentVideoWidth = 0;
currentVideoHeight = 0;
mediaPlayer.release();
mediaPlayer = new IjkMediaPlayer();
if (GSYVideoType.isMediaCodec()) {
Debuger.printfLog("enable mediaCodec");
mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1);
mediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", 1);
if (videoType == GSYVideoType.IJKPLAYER) {
initIJKPlayer(msg);
} else if (videoType == GSYVideoType.IJKEXOPLAYER) {
initEXOPlayer(msg);
}
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(((GSYModel) msg.obj).getUrl(), ((GSYModel) msg.obj).getMapHeadData());
mediaPlayer.setLooping(((GSYModel) msg.obj).isLooping());
mediaPlayer.setOnCompletionListener(GSYVideoManager.this);
mediaPlayer.setOnBufferingUpdateListener(GSYVideoManager.this);
mediaPlayer.setScreenOnWhilePlaying(true);
......@@ -259,15 +264,42 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
mediaPlayer.setOnErrorListener(GSYVideoManager.this);
mediaPlayer.setOnInfoListener(GSYVideoManager.this);
mediaPlayer.setOnVideoSizeChangedListener(GSYVideoManager.this);
if (((GSYModel) msg.obj).getSpeed() != 1 && ((GSYModel) msg.obj).getSpeed() > 0) {
mediaPlayer.setSpeed(((GSYModel) msg.obj).getSpeed());
}
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initIJKPlayer(Message msg) {
mediaPlayer = new IjkMediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
if (GSYVideoType.isMediaCodec()) {
Debuger.printfLog("enable mediaCodec");
((IjkMediaPlayer) mediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
((IjkMediaPlayer) mediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1);
((IjkMediaPlayer) mediaPlayer).setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", 1);
}
((IjkMediaPlayer) mediaPlayer).setDataSource(((GSYModel) msg.obj).getUrl(), ((GSYModel) msg.obj).getMapHeadData());
mediaPlayer.setLooping(((GSYModel) msg.obj).isLooping());
if (((GSYModel) msg.obj).getSpeed() != 1 && ((GSYModel) msg.obj).getSpeed() > 0) {
((IjkMediaPlayer) mediaPlayer).setSpeed(((GSYModel) msg.obj).getSpeed());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void initEXOPlayer(Message msg) {
mediaPlayer = new IjkExoMediaPlayer(context);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(context, Uri.parse(((GSYModel) msg.obj).getUrl()), ((GSYModel) msg.obj).getMapHeadData());
} catch (IOException e) {
e.printStackTrace();
}
}
public void prepare(final String url, final Map<String, String> mapHeadData, boolean loop, float speed) {
if (TextUtils.isEmpty(url)) return;
......@@ -410,7 +442,7 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
}
public IjkMediaPlayer getMediaPlayer() {
public AbstractMediaPlayer getMediaPlayer() {
return mediaPlayer;
}
......@@ -453,4 +485,18 @@ public class GSYVideoManager implements IMediaPlayer.OnPreparedListener, IMediaP
public void setPlayPosition(int playPosition) {
this.playPosition = playPosition;
}
public int getVideoType() {
return videoType;
}
/**
* 设置了视频的播放类型
* GSYVideoType IJKPLAYER = 0 or IJKEXOPLAYER = 1;
*/
public void setVideoType(Context context, int videoType) {
this.context = context.getApplicationContext();
this.videoType = videoType;
}
}
\ No newline at end of file
......@@ -35,6 +35,7 @@ import java.util.Timer;
import java.util.TimerTask;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.getTextSpeed;
import static com.shuyu.gsyvideoplayer.utils.CommonUtil.hideNavKey;
......@@ -1195,8 +1196,9 @@ public abstract class GSYVideoPlayer extends GSYBaseVideoPlayer implements View.
* 再打开已经缓存的本地文件,网络速度才会回0.因为是播放本地文件了
*/
public long getNetSpeed() {
if (GSYVideoManager.instance().getMediaPlayer() != null) {
return GSYVideoManager.instance().getMediaPlayer().getTcpSpeed();
if (GSYVideoManager.instance().getMediaPlayer() != null
&& (GSYVideoManager.instance().getMediaPlayer() instanceof IjkMediaPlayer)) {
return ((IjkMediaPlayer)GSYVideoManager.instance().getMediaPlayer()).getTcpSpeed();
} else {
return -1;
}
......
......@@ -16,6 +16,10 @@ public class GSYVideoType {
public final static int SCREEN_TYPE_4_3 = 2;
public final static int IJKPLAYER = 0;
public final static int IJKEXOPLAYER = 1;
//显示比例类型
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册