MediaExtractor.setDataSource throws IOException “failed to instantiate extractor”

主宰稳场 提交于 2019-12-04 07:46:17

As Andy wrote in this post by text, here is the sample code:

MediaExtractor extractor = new MediaExtractor();
File file = new File(pathToFile);
FileInputStream fis = null;
try {
    fis = new FileInputStream(file);
    FileDescriptor fd = fis.getFD();
    extractor.setDataSource(fd);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    //Release stuff
    extractor.release();
    try {
        if(fis != null) {
            fis.close();
        }
    } catch (Exception e){
        e.printStackTrace();
    }
}

It would appear that if, instead of asking it to extract from a file, I open the file myself, get an input stream, get the FileDescriptor from the input stream, and then ask it to extract from the file descriptor, it works every time. Even if I wiggle the mouse!

I'll chalk that up to a bug in android and go answer some other peoples questions with a copy paste of this. Not sure if that is the correct protocol in this case?

For me, the accepted answer didn't work. I had to specify the start offset and the length:

// Assuming a raw resource located at "res/raw/test_audio.mp3"
MediaExtractor extractor = new MediaExtractor();
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.test_audio);
try {
    extractor.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
} catch (IOException e) {
    e.printStackTrace();
}

For me, I have the same problem. it is because I forgot declared permission in manifest.

Maybe you can check this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!