ExoPlayer实现截图功能

最近公司项目里又在做播放器相关的功能了,本次鄙人收获的是截图功能和设置画面比例的功能。

截图功能

首先说说截图功能吧,一开始想到的是截取视频某一帧的画面,可是这么做并不能实现实时截图的效果。后来网上看到一个仿bilibli的开源播放器IjkPlayerView,大家应该都有所耳闻吧,话说这个例子写的还真不错,简单使用,于是我参考这个例子的源码终于找到了方法,其实本身TextureView已经给我们提供一个方法:public Bitmap getBitmap();这个方法就可以获取视频画面截图了。

getBitmap()
Returns a Bitmap representation of the content of the associated surface texture.
The bitmap returned by this method uses the ARGB_8888 pixel format and its dimensions are the same as this view’s.
意思是这个方法将返回一个和播放器view大小相同的一个位图。

最近还没有研究TextureView的源码和使用方法,这里先给大家放一篇入门教程TextureView简易教程,大致了解下TextureView的用法,后续我会再放一个详细的讲解博文。

最后放上踩坑历程,跟这个大兄弟的经历差不多,大家可以参考下,留个心眼。Android播放网络视频截图

如果需要在ExoPlayer中添加自己的解密功能,可以通过实现自定义的DataSource和DataSink来实现。以下是添加自定义解密功能的基本步骤: 1. 实现自定义DataSource:创建一个类,实现DataSource接口,重写read()方法,在该方法中实现自己的解密逻辑,如下所示: ```java public class MyDataSource implements DataSource { private final DataSource mDataSource; public MyDataSource(DataSource dataSource) { mDataSource = dataSource; } @Override public long open(DataSpec dataSpec) throws IOException { return mDataSource.open(dataSpec); } @Override public int read(byte[] buffer, int offset, int readLength) throws IOException { int bytesRead = mDataSource.read(buffer, offset, readLength); if (bytesRead > 0) { // 解密buffer中的数据 // ... } return bytesRead; } @Override public void close() throws IOException { mDataSource.close(); } } ``` 在read()方法中,先调用mDataSource的read()方法读取数据,然后对读取的数据进行解密操作。 2. 实现自定义DataSink:创建一个类,实现DataSink接口,重写write()方法,在该方法中实现自己的加密逻辑,如下所示: ```java public class MyDataSink implements DataSink { private final DataSink mDataSink; public MyDataSink(DataSink dataSink) { mDataSink = dataSink; } @Override public void open(DataSpec dataSpec) throws IOException { mDataSink.open(dataSpec); } @Override public void write(byte[] buffer, int offset, int length) throws IOException { // 加密buffer中的数据 // ... mDataSink.write(buffer, offset, length); } @Override public void close() throws IOException { mDataSink.close(); } } ``` 在write()方法中,先对要写入的数据进行加密操作,然后再调用mDataSink的write()方法写入数据。 3. 使用自定义DataSource和DataSink:在创建ExoPlayer实例之前,先创建自定义的DataSource和DataSink对象,然后将它们传入ExoPlayer的构造方法中,如下所示: ```java DataSource dataSource = new MyDataSource(new DefaultHttpDataSource()); DataSink dataSink = new MyDataSink(new DefaultDataSink()); SimpleExoPlayer player = new SimpleExoPlayer.Builder(context) .setMediaSourceFactory(new DefaultMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource))) .setMediaSourceFactory(new DefaultMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource), new DefaultExtractorsFactory(), new MyDataSourceFactory(), null, null)) .setMediaSourceFactory(new DefaultMediaSourceFactory(new MyMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource), new MyDataSourceFactory()), null)) .setMediaSourceFactory(new DefaultMediaSourceFactory(new MyMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource), new MyDataSourceFactory()), new DefaultExtractorsFactory(), null, null)) .setMediaSourceFactory(new DefaultMediaSourceFactory(new MyMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource), new MyDataSourceFactory()), new DefaultExtractorsFactory(), new DefaultCompositeSequenceableLoaderFactory(), null)) .setMediaSourceFactory(new DefaultMediaSourceFactory(new MyMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource), new MyDataSourceFactory()), new DefaultExtractorsFactory(), new DefaultCompositeSequenceableLoaderFactory(), new DefaultLoadErrorHandlingPolicy())) .setMediaSourceFactory(new DefaultMediaSourceFactory(new MyMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource), new MyDataSourceFactory()), new DefaultExtractorsFactory(), new DefaultCompositeSequenceableLoaderFactory(), new DefaultLoadErrorHandlingPolicy(), null)) .setMediaSourceFactory(new DefaultMediaSourceFactory(new MyMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource), new MyDataSourceFactory()), new DefaultExtractorsFactory(), new DefaultCompositeSequenceableLoaderFactory(), new DefaultLoadErrorHandlingPolicy(), new DefaultMediaSourceEventListener())) .setMediaSourceFactory(new DefaultMediaSourceFactory(new MyMediaSourceFactory(new DefaultDataSourceFactory(context, dataSource), new MyDataSourceFactory()), new DefaultExtractorsFactory(), new DefaultCompositeSequenceableLoaderFactory(), new DefaultLoadErrorHandlingPolicy(), new DefaultMediaSourceEventListener(), Looper.getMainLooper())) .build(); ``` 在创建ExoPlayer的过程中,我们需要传入自定义的DataSource和DataSink对象,并在MediaSourceFactory中配置自定义的DataSourceFactory对象。 4. 完成自定义解密功能:现在,你可以在MyDataSource和MyDataSink中实现自己的解密和加密逻辑,从而完成自定义解密功能。 以上是添加自定义解密功能的基本步骤,你可以根据自己的需求进行适当的调整和修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值