二、ijkplayer:修复播放本地raw目录文件

2019-10-25  本文已影响0人  緦菍亭芷

参考文档

1、https://www.jianshu.com/p/9f4379e14ab6

播放视频用的是官方DEMO下的IjkVideoView, 如果按照传统方法播放raw资源文件下的视频是不行的.

A.获取raw资源文件方式

    String path = "android.resource://" + videoLayout.getContext().getPackageName() + "/" + R.raw.ball_1;

    videoLayout.setPath(path);

B.实现接口IMediaDataSource

public class RawDataSourceProvider implements IMediaDataSource {
    private AssetFileDescriptor mDescriptor;

    private byte[]  mMediaBytes;

    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);

        return length;
    }

    @Override
    public long getSize() throws IOException {
        long length  = mDescriptor.getLength();
        if(mMediaBytes == null){
            InputStream inputStream = mDescriptor.createInputStream();
            mMediaBytes = readBytes(inputStream);
        }


        return length;
    }

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

        mDescriptor = null;
        mMediaBytes = null;
    }

    private byte[] readBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }

        return byteBuffer.toByteArray();
    }

    public static RawDataSourceProvider create(Context context, Uri uri){
        try {
            AssetFileDescriptor fileDescriptor = context.getContentResolver().openAssetFileDescriptor(uri, "r");
            return new RawDataSourceProvider(fileDescriptor);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

C. 在VideoLayout的openVideo()中进行修改

        Uri mUri = Uri.parse(mPath);
        String scheme = mUri.getScheme();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                (TextUtils.isEmpty(scheme) || scheme.equalsIgnoreCase("file"))) {
            IMediaDataSource dataSource = new FileMediaDataSource(new File(mUri.toString()));
            mMediaPlayer.setDataSource(dataSource);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            if (scheme.equals(ContentResolver.SCHEME_ANDROID_RESOURCE)) {
                RawDataSourceProvider rawDataSourceProvider = RawDataSourceProvider.create(getContext(), mUri);
                mMediaPlayer.setDataSource(rawDataSourceProvider);
            } else {
                mMediaPlayer.setDataSource(getContext(), mUri, mHeader);
            }
        } else {
            mMediaPlayer.setDataSource(mUri.toString());
        }
        mMediaPlayer.prepareAsync();

我自己封装的

上一篇下一篇

猜你喜欢

热点阅读