如何将视频文件转换为字节数组,反之亦然?

时间:2019-05-18 06:43:34

标签: java android arrays exoplayer

我试图将视频转换为字节数组,反之亦然并使用exoplayer播放。使用下面的代码能够将视频文件转换为字节数组(请参见下面的代码)。但是当我想从数组播放时,它会产生源错误(请参见下面的代码)。我尝试了很多,看到了很多代码,但是我没有时间玩。感谢您的帮助。

    private void convert(Uri uri) throws IOException {
    InputStream inputStream = getContentResolver().openInputStream(uri);
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

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

    Log.d(TAG, "convert: convert is done");
}

 public void prepareExoPlayerFromByteArray(byte[] data){
   exoPlayer = ExoPlayerFactory.newSimpleInstance(context,new 
   DefaultTrackSelector(null),new DefaultLoadControl());
   playerView.setPlayer(exoPlayer);
    final MByteArrayDataSource byteArrayDataSource = new 
    MByteArrayDataSource(data);
     Log.i(TAG,"ByteArrayDataSource constructed");
    Uri videoByteUri = new UriByteDataHelper().getUri(data);
    DataSpec dataSpec = new DataSpec(videoByteUri);
    try
    {
        byteArrayDataSource.open(dataSpec);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    DataSource.Factory factory = new DataSource.Factory() {
        @Override
        public DataSource createDataSource() {
            return byteArrayDataSource;
        }
    };
    Log.i(TAG,"DataSource.Factory constructed");
    MediaSource mediaSource = new 
    ExtractorMediaSource(videoByteUri,factory,new 
    DefaultExtractorsFactory(),null,null);
    Log.i(TAG,"Video source constructed");
    exoPlayer.prepare(mediaSource);
    exoPlayer.setPlayWhenReady(true);

    }

 05-18 10:42:01.457 20071-20885/com.example.convertarray 
 E/ExoPlayerImplInternal: Source error.
    com.google.android.exoplayer2.source.UnrecognizedInputFormatException: 
 None of the available extractors (MatroskaExtractor, FragmentedMp4Extractor, 
 Mp4Extractor, Mp3Extractor, AdtsExtractor, Ac3Extractor, TsExtractor, 
 FlvExtractor, OggExtractor, PsExtractor, WavExtractor) could read the stream.
        at 
 com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractorHolder.selectExtractor(ExtractorMediaPeriod.java:705)
        at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:628)
        at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:295)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)

0 个答案:

没有答案