【附】相关架构及资料
源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,和技术大牛一起讨论交流解决问题。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
// 屏幕常亮
setKeepScreenOn(true);
// 设置dataSource
try {
mMediaPlayer.setDataSource(mContext.getApplicationContext(), Uri.parse(mUrl));
if (mSurface == null) {
mSurface = new Surface(mSurfaceTexture);
}
mMediaPlayer.setSurface(mSurface);
mMediaPlayer.prepareAsync();
} 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) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return mSurfaceTexture == null;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
使用布局:
在activity中的布局加上即可
<com.example.ycm.smallvideoplayer.smallvideoplayer.SmallVideoPlayer
结尾
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!