سؤال

I've got a little problem with MediaPlayer. Problem appears on Android 4.0.3 - 4.0.4. Whats happening is that my onPrepare() method is not fired at all on those two versions. Tested on 4.3 and 4.4 and no problem at all.

I have no idea what might be the reason for it to not work. So any ideas, thoughts etc are welcome.

PLAY CODE:

       try {
            if (!player.isPlaying()) {
                player.reset();
                player.setOnPreparedListener(this);
                player.setOnCompletionListener(mFragment);
                player.setDataSource(String.valueOf(tmpUri));
                player.prepareAsync();
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

start() is in on prepare method.

Logcat error:

  ERROR/MediaPlayer(10830): Error (1,-2147483648)
  ERROR/MediaPlayer(10830): mOnErrorListener is null. Failed to   send MEDIA_ERROR message.
هل كانت مفيدة؟

المحلول

Ive found the answer to my problem, so I'm going to share my info.

Anyway, the problem was that on Android 4.0.4 and 4.0.3 when reading mp3 files from device memory setDataSource() did not read the length of file right. Which made MediaPlayer go psycho and call Error (1,-2147483648).

Android was giving all files length = 0x7ffffffffffffffL.

Why? I don't know ;)

Fix:

 File file = new File(uri);
 long length = file.getTotalSpace()
 FileInputStream is = new FileInputStream(file);
 player.setDataSource(is.getFD(), 0l, length);
 player.prepareAsync();

That's it! Hope it will help someone with similar problem, peace!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top