Search code examples
androidandroid-mediaplayer

Media Player is not working in Lollipop device


I am trying to play an audio file via media player. It's working in Marshmallow devices.Unfortunately, it's not working in Lollipop devices. It is showing an IOException java.io.IOException: Prepare failed.: status=0x105. I can't understand what would be the problem. I know this is common question. But I didn't get any solution from the old questions.

   try {
         mPlayer.setDataSource(mChapter.getChapterAudioLink());
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
   try {

                    mPlayer.prepare();
                    mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            playOrPause.setImageResource(android.R.drawable.ic_media_pause);
                            mAudioDialog.show();
                            mPlayer.start();
                            mDurationTextView.setText(convertSecondsToHMmSs(mPlayer.getDuration()) + "");
                            mSeekbarAudio.setMax(mPlayer.getDuration());
                        }
                    });
                    //   mPlayer.prepareAsync();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "You might not set the URI correctlyyy!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "You might not set the URI correctlyy!", Toast.LENGTH_LONG).show();
                }

I am getting the last IOException.


Solution

  • Spaces in the URL were causing this issue for me. I replaced the spaces with %20 and it works fine.

    mPlayer.setDataSource(mChapter.getChapterAudioLink().replaceAll(" ","%20"));