由于ijkplayer不能识别android.resource类型的资源在播放raw中的文件的时候用IjkMediaPlayer不能正常播放,实现IMediaDataSource为IjkMediaPlayer提供资源。

class RawDataSourceProvider implements IMediaDataSource{

    AssetFileDescriptor mDescriptor;

    byte[]  mMediaBytes;

    long mPosition;

    public RawDataSourceProvider(AssetFileDescriptor descriptor) {
        this.mDescriptor = descriptor;
    }

    @Override
    public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
        if(position + 1 >= mMediaBytes.length){
            return -1;
        }

        int length;
        if(position + size < mMediaBytes.length){
            length = size;
        }else{
            length = (int) (mMediaBytes.length - position);
            if(length > buffer.length)
                length = buffer.length ;

            length--;
        }

        System.arraycopy(mMediaBytes, (int) position, buffer, offset, length);
        mPosition = position;
        return length;
    }

    @Override
    public long getSize() throws IOException {
        long length  = mDescriptor.getLength();

        if(mMediaBytes == null){
            Source source = Okio.source(mDescriptor.createInputStream());
            mMediaBytes = Okio.buffer(source).readByteArray();
        }


        return length;
    }

    @Override
    public void close() throws IOException {
        if(mDescriptor != null)
            mDescriptor.close();

        mDescriptor = null;
        mMediaBytes = null;
    }
}



ijkplayer实现IMediaDataSource
《架构文摘》每天一篇架构领域重磅好文,涉及一线互联网公司应用架构(高可用、高性 能、高稳定)、大数据、机器学习等各个热门领域。

相关文章:

  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2021-11-06
  • 2021-07-21
  • 2021-12-22
  • 2021-05-19
猜你喜欢
  • 2021-04-24
  • 2021-09-08
  • 2021-03-31
  • 2022-12-23
  • 2021-06-28
  • 2021-11-09
  • 2021-12-04
相关资源
相似解决方案