java.io.IOException: 播放录制的 mp4 时 setDataSource 失败

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

我想使用 Android Media Player 播放录制的 mp4 数据,但是当尝试播放时显示此错误:

java.io.IOException: setDataSource failed.

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

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

这是我的代码:

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();
                }
            }
        });

有人可以帮忙吗?

我昨天刚解决了这个问题

不要给出 setDataSource 的直接路径,而是给出 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();
            }

然后在onPrepared方法中

@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);
    }

在初始化您的媒体播放器时执行此操作

mMediaRecordingPlayer  = new MediaPlayer();

mMediaRecordingPlayer.setOnPreparedListener(this);  

并且在你的 Activity 中你必须实现这个

public class MainActivity extends Activity implements OnPreparedListener 

不要忘记在 AndroidManifest.xml

中添加互联网权限
<uses-permission android:name="android.permission.INTERNET" />