MediaExtractor.setDataSource throws IOException "Could not instantiate extractor"

I am on Android 4.2 and calling MediaExtractor.setDataSource, and it sometimes throws an IOException from "failed to create an extractor instance". I found where this is selected from a C ++ implementation, but this did not help.

Other people with the same problem and neither answer nor answer that do not help me:

android.media.MediaExtractor. Has anyone made this beast work? "Failed to create extractor instance" Exception

show media extractor "failed to create an extractor instance

Failed to create mediaextractor instance using setDataSource ()

In a desperate attempt to figure this out, I wrote a small little application that I could demonstrate this:

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(); } } 

There is one button in action for this application that calls "doStuff". The result is as follows:

 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 confuses a few calculations.

  • Sometimes it works, and sometimes not
  • Failed to make the first attempt.

Now I'm sure that other people use this interface, and it works for them, which either means that the devices I use (MK808) have broken the firmware, or there is a trick that I don’t have to make it reliable. Does anyone have any ideas?

The log code is surprisingly clear, except when the GC starts up and the synchronization of the GC does not correlate with the problem.

change

I changed my button so that it starts a thread that iterates over 1000 times across all three files. Everything went without problems. Big scratch. I found that if I shake the mouse while this thread is running, it starts to crash, but it starts to work again as soon as I let go. The launch of another thread, which simply interferes with doing math for a while, did not cause this. In the real world, this application has come; removing the mouse does not make the problem go away, but it is a great application and does many other things too.

+4
source share
4 answers

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

I will remember this before the error in android and answer some other questions with a copy. Not sure if this is the correct protocol in this case?

+3
source

For me, the accepted answer did not work. I had to specify the initial offset and 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(); } 
+2
source

I have the same problem for me. this is because I forgot the declared permission in the manifest.

Perhaps you can check this out.

+2
source

As Andy wrote in this text, here is a 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(); } } 
+1
source

Source: https://habr.com/ru/post/948445/


All Articles