如何在DataSource缓冲区大小中更改读取方法[ExoPlayer]

时间:2018-07-17 17:37:49

标签: android buffer datasource exoplayer

现在,我自定义DataSource类以使其兼容。但是读取方法中的缓冲区字节数组参数始终返回65536或4。我只想将缓冲区长度更改为4096。因为我必须解密每个4096字节数组。因此,如何更改它。如果您知道如何解决此问题,请给我一些指南或代码。谢谢。

这是自定义的数据源代码。

public class InputStreamDataSource implements DataSource {
private static final String TAG = "InputStreamDataSource";
private Context context;
private DataSpec dataSpec;
private FileInputStream inputStream;
private long bytesRemaining;
private boolean opened;

public InputStreamDataSource(Context context, DataSpec dataSpec) {
    this.context = context;
    this.dataSpec = dataSpec;
}

@Override
public long open(DataSpec dataSpec) throws IOException {
    try {
        inputStream = new FileInputStream(dataSpec.uri.getPath());
        long skipped = inputStream.skip(dataSpec.position);
        if (skipped < dataSpec.position) {
            throw new EOFException();
        }
        if (dataSpec.length != C.LENGTH_UNSET) {
            bytesRemaining = dataSpec.length;
        } else {
            bytesRemaining = inputStream.available();
            if (bytesRemaining == Integer.MAX_VALUE) {
                bytesRemaining = C.LENGTH_UNSET;
            }
        }
    } catch (IOException e) {
        throw new IOException(e);
    }

    opened = true;
    return bytesRemaining;
}

@Override
public int read(byte[] buffer, int offset, int readLength) throws IOException {
    Log.d(TAG, "read: " + buffer.length);
    if(readLength == 0) {
        return 0;
    } else if (bytesRemaining == 0) {
        return C.RESULT_END_OF_INPUT;
    }

    int bytesRead;
    try {
        int bytesToRead = bytesRemaining == C.LENGTH_UNSET ? readLength
                : (int) Math.min(bytesRemaining, readLength);
        bytesRead = inputStream.read(buffer, offset, bytesToRead);
    } catch (IOException e) {
        throw new IOException(e);
    }

    if(bytesRead == -1) {
        if(bytesRemaining != C.LENGTH_UNSET) {
            throw new IOException(new EOFException());
        }
        return C.RESULT_END_OF_INPUT;
    }

    if(bytesRemaining != C.LENGTH_UNSET) {
        bytesRemaining -= bytesRead;
    }

    return bytesRead;
}

@Override
public Uri getUri() {
    return dataSpec.uri;
}

@Override
public void close() throws IOException {

    try {
        if(inputStream != null) {
            inputStream.close();
        }
    } catch (IOException e) {
        throw new IOException(e);
    } finally {
        inputStream = null;
        if(opened) {
            opened = false;
        }
    }

}

}

0 个答案:

没有答案