我想使用Android Media Player播放录制的mp4数据,但在尝试播放此错误时显示:
java.io.IOException:setDataSource失败。
在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1086)
在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();
}
}
});
有人可以帮忙吗?
答案 0 :(得分:4)
我昨天刚刚解决了这个问题
不是给出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);
在你的活动中你必须实现这个
public class MainActivity extends Activity implements OnPreparedListener
答案 1 :(得分:-1)
不要忘记在AndroidManifest.xml中添加互联网权限
{{1}}