} catch (IOException e) {
e.printStackTrace();
}
}
使用ijplayer播放视频很简单,只要为其设置数据源即可。但是为了能让视频显示出来,也就是能在view上播放出来,我们需要使用Surface。
创建对象private Surface mSurface;,传入刚才存储的SurfaceTexture对象:mSurface = new Surface(mSurfaceTexture);,然后把这个surface对象传递给播放器即可,最后使用播放器开始播放,注意这个方法是同步的。
完成以上步骤,简单的视频播放器就可以完成了。
代码:
我们把代码进行整理如下:
播放器
public class SmallVideoPlayer extends FrameLayout implements TextureView.SurfaceTextureListener {
private TextureView mTextureView;
private SurfaceTexture mSurfaceTexture;
private Surface mSurface;
private AudioManager mAudioManager;
private IMediaPlayer mMediaPlayer;
private Context mContext;
private String mUrl;
public SmallVideoPlayer(@NonNull Context context) {
this(context, null);
}
public SmallVideoPlayer(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, -1);
}
public SmallVideoPlayer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
}
public void setUp(String url) {
mUrl = url;
}
public void start() {
initAudioManager();
initMediaPlayer();
initTextureView();
addTextureView();
}
private void initAudioManager() {
if (mAudioManager == null) {
mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mAudioManager.requestAudioFocus(new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN).build());
} else {
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
}
}
private void initMediaPlayer() {
if (mMediaPlayer == null) {
mMediaPlayer = new IjkMediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
}
private void initTextureView() {
if (mTextureView == null) {
mTextureView = new TextureView(mContext);
mTextureView.setSurfaceTextureListener(this);
}
}
private void addTextureView() {
removeView(mTextureView);
LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
addView(mTextureView, 0, params);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
if (mSurfaceTexture == null) {
mSurfaceTexture = surface;
openMediaPlayer();
} else {
mTextureView.setSurfaceTexture(mSurfaceTexture);
}
}
private void openMediaPlayer() {
// 屏幕常亮
setKeepScreenOn(true);
// 设置dataSource
try {
mMediaPlayer.setDataSource(mContext.getApplicationContext(), Uri.parse(mUrl)/, mHeaders/);
if (mSurface == null) {
mSurface = new Surface(mSurfaceTexture);
}
mMediaPlayer.setSurface(mSurface);
mMediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
学习分享
①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以点击我的【Github】免费下载,最后觉得有帮助、有需要的朋友可以点个赞
[外链图片转存中…(img-oDYrjYD5-1710573120376)]
[外链图片转存中…(img-pV7htONE-1710573120376)]
[外链图片转存中…(img-3cWtgPF4-1710573120376)]