美文网首页
2019-09-14 ijkplayer视频缓存加密方案

2019-09-14 ijkplayer视频缓存加密方案

作者: 兣甅 | 来源:发表于2019-09-14 18:02 被阅读0次
为了防止系统或其他软件播放app内部的缓存视频,需要对缓存的视频进行加密和解密操作

1.对缓存视频进行加密,缓存使用AndroidVideoCache方案

FileCache.java文件进行加密操作,比较简单的数据反向

public class FileCache implements Cache {
public static final int ENCRYPT_SIZE = 32;
 @Override
  public synchronized void complete() throws ProxyCacheException {
    if (isCompleted()) {
      return;
    }
     //--------------------------加密开始--------------------------//
    try {
      dataFile.seek(0);
      byte[] oldByte = new byte[ENCRYPT_SIZE];
      byte[] newByte = new byte[ENCRYPT_SIZE];
      if (dataFile.read(oldByte) >= ENCRYPT_SIZE) {
        for (int i = 0; i < oldByte.length; i++) {
          newByte[oldByte.length - 1 - i] = oldByte[i];
        }
        dataFile.seek(0);
        dataFile.write(newByte);
     //--------------------------加密结束--------------------------//
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    close();
    ......
}

2.读取缓存,解码,需要实现ijk的IMediaDataSource


public class FileMediaDataSource implements IMediaDataSource {
  private RandomAccessFile mFile;
  private long mFileSize;

  public FileMediaDataSource(File file) throws IOException {
    mFile = new RandomAccessFile(file, "r");
    mFileSize = mFile.length();
  }

  @Override
  public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
    if (mFile.getFilePointer() != position) {
      mFile.seek(position);
    }
    if (size == 0) {
      return 0;
    }
    int read = mFile.read(buffer, 0, size);
    //--------------------------解密开始--------------------------//
    byte[] bufferHead = new byte[FileCache.ENCRYPT_SIZE];
    if (read >= bufferHead.length && position == 0) {
      //加密算法为前面一小段反向
      System.arraycopy(buffer, 0, bufferHead, 0, bufferHead.length);
      for (int i = 0; i < bufferHead.length; i++) {
        buffer[bufferHead.length - 1 - i] = bufferHead[i];
      }
    }
   //--------------------------解密结束--------------------------//
    return read;
  }

  @Override
  public long getSize() throws IOException {
    return mFileSize;
  }

  @Override
  public void close() throws IOException {
    mFileSize = 0;
    mFile.close();
    mFile = null;
  }
}

3.设置播放的DataSource\color{red}{★★★注意:本地播放地址才走这个设置方法★★★}

HttpProxyCacheServer mCacheServer ;//自己设置AndroidVideoCache代理
IjkMediaPlayer ijkPlayer;//ijk的播放器
 if (mCacheServer.isCached(mUrl)) {
        File file = mCacheServer.getCacheFile(mUrl);//默认是private的,自己处理下
          try {
            ijkPlayer.setDataSource(new FileMediaDataSource (file));
          } catch (IOException e) {
            e.printStackTrace();
            mMediaPlayer.setDataSource(proxyPath, mHeaders);
          }
     }

相关文章

网友评论

      本文标题:2019-09-14 ijkplayer视频缓存加密方案

      本文链接:https://www.haomeiwen.com/subject/xkyayctx.html