Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.IOException: setDataSource failed when play recorded mp4

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 ?

like image 559
Redturbo Avatar asked Oct 19 '22 23:10

Redturbo


1 Answers

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 
like image 78
Syed Nazar Muhammad Avatar answered Nov 15 '22 04:11

Syed Nazar Muhammad