MediaExtractor.setDataSource throws IOException "failed to instantiate extractor"
Asked Answered
A

5

6

I'm on Android 4.2 and calling MediaExtractor.setDataSource, and it sometimes throws an IOException of "failed to instantiate extractor". I've found where this is thrown from the C++ implementation, but it hasn't helped.

Other people with the same problem and either no answer or an answer which doesn't help me are:

android.media.MediaExtractor. Anyone got this beast to work? "Failed to instantiate extractor" exception

media extractor show "failed to instantiate extractor"

Failed to instantiate mediaextractor when using setDataSource()

In a desperate attempt to figure this out I've written the smallest little app I could which demonstrates it:

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  public boolean doStuff(View view) {
    File f = getExternalFilesDir(null);
    File[] files = f.listFiles();
    for (File file : files) {
      if (file.isFile()) {
        Log.e("Andy", "trying file [" + file.getName() + "]");
        startExtractor(file);
      }
    }
    return true;
  }
  private void startExtractor(File f) {
    MediaExtractor extractor = new MediaExtractor();
    try {
      extractor.setDataSource(f.getAbsolutePath());
    } catch (IOException e) {
      Log.e("Andy", "splat " + e.getMessage());
    }
    extractor.release();
  }
}

The activity for this app has a single button, which calls "doStuff". The output looks like this:

05-12 15:27:42.639: E/Andy(18757): trying file [aaitn.mp4]
05-12 15:27:42.689: E/Andy(18757): splat Failed to instantiate extractor.
05-12 15:27:42.689: E/Andy(18757): trying file [unusual aspect.mp4]
05-12 15:27:42.709: E/Andy(18757): trying file [test.mp4]
05-12 15:27:55.039: E/Andy(18757): trying file [aaitn.mp4]
05-12 15:27:55.119: E/Andy(18757): trying file [unusual aspect.mp4]
05-12 15:27:55.149: E/Andy(18757): trying file [test.mp4]
05-12 15:28:03.209: E/Andy(18757): trying file [aaitn.mp4]
05-12 15:28:03.259: E/Andy(18757): splat Failed to instantiate extractor.
05-12 15:28:03.259: E/Andy(18757): trying file [unusual aspect.mp4]
05-12 15:28:03.279: E/Andy(18757): trying file [test.mp4]
05-12 15:28:12.289: E/Andy(18757): trying file [aaitn.mp4]
05-12 15:28:12.379: E/Andy(18757): trying file [unusual aspect.mp4]
05-12 15:28:12.419: E/Andy(18757): trying file [test.mp4]
05-12 15:28:17.879: E/Andy(18757): trying file [aaitn.mp4]
05-12 15:28:17.929: E/Andy(18757): trying file [unusual aspect.mp4]
05-12 15:28:17.949: E/Andy(18757): splat Failed to instantiate extractor.
05-12 15:28:17.949: E/Andy(18757): trying file [test.mp4]

This is confusing on a few counts.

  • It sometimes works and sometimes doesn't
  • It failed on the very first attempt.

Now, I'm fairly sure that other people are using this interface and it is working for them, which either means the devices I'm using (MK808s) have broken firmware, or there's a trick I'm missing to make it reliable. Does anyone have any ideas?

The logcat is amazingly clear except when the GC kicks in, and the GC timing doesn't correlate to the problem.

edit

I changed my button so that it would launch a thread which loops 1000 times over all three files. It ran all the way, no trouble. Much head scratching. I have found that if I wiggle the mouse while that thread is running it starts failing, but it starts working again as soon as I let go. Running yet another thread that just messes around doing maths for a while didn't cause it. In the real world app this came from, removing the mouse doesn't make the problem go away, but it's a big app and does a lot of other stuff too.

Aerosol answered 12/5, 2014 at 14:51 Comment(3)
Is there anything from the system media code in the logcat output?Commiserate
No. It's amazingly clear except when the GC kicks in, and the GC timing doesn't correlate to the problem.Aerosol
Hmm, I guess I know why my app has lowest ratings for Android 4.2. Yay for broken things.Sike
A
6

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?

Aerosol answered 14/5, 2014 at 13:22 Comment(1)
Would you care to post the code that implements this strategy?Logarithm
L
9

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();
}
Logarithm answered 13/1, 2016 at 22:48 Comment(0)
D
7

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();
    }
}
Droopy answered 7/8, 2017 at 8:24 Comment(0)
A
6

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?

Aerosol answered 14/5, 2014 at 13:22 Comment(1)
Would you care to post the code that implements this strategy?Logarithm
P
3

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

Maybe you can check this.

Postal answered 20/8, 2016 at 15:6 Comment(1)
If a missing permission only causes occasional failure to read files we have much bigger problems here :pAerosol
I
0

I had the same issue. It worked for me after I moved my files directly within the assets folder, rather than keeping it in sub-folders.

Impeditive answered 18/6, 2021 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.