6

I want to play recorded mp4 data using Android Media Player,but when try to play this error showed :

java.io.IOException: setDataSource failed.

at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1086)

at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)

This is my code :

final ImageView play = (ImageView) root.findViewById(R.id.voice_play);
        play.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    player = new MediaPlayer();
                    if (player.isPlaying()) {
                        player.reset();
                    }
                    player.setDataSource("file://mnt/sdcard/Android/data/com.myapp.apptalk/AudioRecorder/22-Oct-2015 11:26:14.mp4");
                    player.prepare();

                    seekbar.setMax(player.getDuration());

                    if (isPlay) {
                        player.stop();
                        play.setImageResource(R.drawable.play);
                        handler.removeCallbacks(runnable);
                        isPlay = false;
                    } else {
                        player.start();

                        runnable = new Runnable() {
                            @Override
                            public void run() {
                                seekbar.setProgress(player.getCurrentPosition());
                                handler.postDelayed(this, 1000);
                            }
                        };
                        handler = new Handler();
                        handler.post(runnable);

                        play.setImageResource(R.drawable.stop);
                        isPlay = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

Anybody can help ?

Community
  • 1
  • 1
Redturbo
  • 1,563
  • 8
  • 22
  • 34

2 Answers2

7

I just solved this problem yesterday

Instead of giving direct path to setDataSource, give the path to File

try 
            {                   
                Log.d("SetDatasource path", playRecordPath);

                 File filePath = new File(playRecordPath);

                 if (!filePath.exists()) 
                 {
                     filePath.createNewFile();
                 }

                 FileInputStream is = new FileInputStream(filePath);

                 mMediaRecordingPlayer.setDataSource(is.getFD());

                mMediaRecordingPlayer.prepare();

                is.close();
            }
            catch (IllegalArgumentException e) 
            {
                e.printStackTrace();
            } 
            catch (SecurityException e) 
            {
                e.printStackTrace();
            } 
            catch (IllegalStateException e) 
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }

And then in onPrepared method

@Override
    public void onPrepared(MediaPlayer mp) 
    {
        mMediaRecordingPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mMediaRecordingPlayer.setVolume(1.5f, 1.5f);

        mMediaRecordingPlayer.start();

        duration = mMediaRecordingPlayer.getDuration();

        mSeekBarPlayer.setMax(duration);

        mSeekBarPlayer.postDelayed(onEverySecond, 1000);
    }

while initializing your media player do this

mMediaRecordingPlayer  = new MediaPlayer();

mMediaRecordingPlayer.setOnPreparedListener(this);  

And in your Activity you have to implement this

public class MainActivity extends Activity implements OnPreparedListener 
  • now, it not show previous error, bu it always show : java.io.FileNotFoundException: file:/storage/emulated/0/AudioRecorder/blablabla.mp4: open failed: ENOENT (No such file or directory) – Redturbo Oct 26 '15 at 02:21
  • that's because the file you are recording & the path you are giving the File to play record is different, Log the path while recording & then again Log the path while giving it to the File to play record – Syed Nazar Muhammad Oct 26 '15 at 04:51
  • save the path while recording & then give the exact same path while playing the record – Syed Nazar Muhammad Oct 26 '15 at 04:53
-5

dont forget to add internet permission in the AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 
Edrisa Turay
  • 542
  • 5
  • 7
  • 6
    Why does internet permission needed if you want to play an local mp4 file? – suther Sep 19 '18 at 15:01
  • i got that same problem when i was building an online radio application and didnt read the question properly... but that's how i solved it. – Edrisa Turay Jun 01 '19 at 14:17