问题描述
我要玩采用Android媒体播放器MP4记录的数据,但在尝试播放此错误表明:
I want to play recorded mp4 data using Android Media Player,but when try to play this error showed :
java.io.IOException异常:的setDataSource失败。
在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1086)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1086)
在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
这是我的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 ?
推荐答案
我刚刚解决了这个问题,昨天
I just solved this problem yesterday
而不是给予的setDataSource直接路径,给文件路径
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();
}
然后在prepared方法
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
这篇关于java.io.IOException异常:失败的setDataSource时播放录制MP4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!