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

点点圈 提交于 2019-12-12 10:55:40

问题


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 ?


回答1:


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 



回答2:


dont forget to add internet permission in the AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 


来源:https://stackoverflow.com/questions/33273523/java-io-ioexception-setdatasource-failed-when-play-recorded-mp4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!