ダイアログでMediaPlayerを使用しました:
public class MainActivity extends Activity {
private static final String TAG = "AudioRecorder";
ImageView showImageview;
Button recordBn;
Button playBn;
Button confirmBn;
Dialog dialog;
Boolean isRecord;
Boolean isCancel;
private MediaRecorder mediaRecorder = null;
private MediaPlayer mediaPlayer = null;
private File file;
private static final String OUT_FILE_NAME = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/myshine.amr";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.file = new File(OUT_FILE_NAME);
Button button = (Button) findViewById(R.id.bu);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog = new MyDialog(MainActivity.this,
R.style.MyDialog);
dialog.setContentView(R.layout.record_dialog);
isRecord=false;
showImageview=(ImageView)dialog.findViewById(R.id.show_image);
recordBn=(Button)dialog.findViewById(R.id.dialog_button_record);
playBn=(Button)dialog.findViewById(R.id.dialog_button_play);
confirmBn=(Button)dialog.findViewById(R.id.dialog_button_cancel);
recordBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isRecord = !isRecord;
if (file.exists()){
file.delete();
}
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (isRecord) {
isCancel=true;
recordBn.setText(R.string.stop_record);
playBn.setEnabled(false);
playBn.setText(R.string.paly_voice);
confirmBn.setText(R.string.cancel);
record(null);
}else{
isCancel=false;
recordBn.setText(R.string.record);
playBn.setEnabled(true);
playBn.setText(R.string.paly_voice);
confirmBn.setText(R.string.confirm);
//结束录音
stop(null);
//初始化播放
}
}
});
playBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//If the track is playing, pause and achange playButton text to "Play"
play(null);
// if(mediaPlayer.isPlaying())
// {
// mediaPlayer.pause();
// playBn.setText(R.string.paly_voice);
// Log.i(TAG, "playing");
// }
// //If the track is not player, play the track and change the play button to "Pause"
// else
// {
// mediaPlayer.start();
// playBn.setText(R.string.pause_voice);
// Log.i(TAG, "play");
// }
}
});
confirmBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isCancel = !isCancel;
if (!isCancel) {
//取消 结束录音
stop(null);
Log.i(TAG, "endcancel");
dialog.dismiss();
}else {
stop(null);
dialog.dismiss();
Log.i(TAG, "send");
}
}
});
dialog.show();
}
});
}
public void record(View v) {
Log.d(TAG, "record");
this.mediaRecorder = new MediaRecorder();
this.mediaRecorder.setAudioChannels(1);
this.mediaRecorder.setAudioSamplingRate(8000);
// this.mediaRecorder.setAudioEncodingBitRate(64000);
this.mediaRecorder.setAudioEncodingBitRate(16);
this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());
// this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
this.mediaRecorder.prepare();
this.mediaRecorder.start();
// update the buttons
} catch (IOException e) {
Log.e(TAG, "Failed to record()", e);
}
}
public void stop(View v) {
Log.d(TAG, "stop()");
if (this.mediaPlayer != null) {
// stop/release the media player
this.mediaPlayer.stop();
this.mediaPlayer.release();
this.mediaPlayer = null;
}
if (this.mediaRecorder != null) {
// stop/release the media recorder
this.mediaRecorder.stop();
this.mediaRecorder.release();
this.mediaRecorder = null;
}
}
public void play(View v) {
Log.d(TAG, "play()");
Log.d(TAG, "ddd:"+this.file.getAbsolutePath());
if (this.file.exists()) {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(this.file.getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
playBn.setText(R.string.pause_voice);
}
});
mediaPlayer.start();
} catch (IOException e) {
Log.e(TAG, "Failed to play()", e);
}
}
}
}
それは私に与えます:
01-28 13:58:11.803: E/MediaPlayer(31717): error (-4, -4)
01-28 13:58:11.813: E/AudioRecorder(31717): Failed to play()
01-28 13:58:11.813: E/AudioRecorder(31717): java.io.IOException: Prepare failed.: status=0xFFFFFFFC
01-28 14:13:39.038: D/AudioRecorder(32641): record
01-28 14:13:39.038: D/MediaRecorder(32641): doCleanUp
01-28 14:13:43.042: D/AudioRecorder(32641): stop()
01-28 14:13:43.042: D/MediaRecorder(32641): stop:E
01-28 14:13:43.392: D/MediaRecorder(32641): doCleanUp
01-28 14:13:43.392: D/MediaRecorder(32641): stop:X
01-28 14:13:43.392: D/MediaRecorder(32641): release:E
01-28 14:13:43.432: W/MediaRecorder(32641): mediarecorder went away with unhandled events
01-28 14:13:43.432: W/MediaRecorder(32641): mediarecorder went away with unhandled events
01-28 14:13:47.296: D/AudioRecorder(32641): play()
01-28 14:13:47.296: D/AudioRecorder(32641): ddd:/mnt/sdcard/myshine.amr
01-28 14:13:47.356: E/MediaPlayer(32641): error (-4, -4)
01-28 14:13:47.356: E/AudioRecorder(32641): Failed to play()
01-28 14:13:47.356: E/AudioRecorder(32641): java.io.IOException: Prepare failed.: status=0xFFFFFFFC
01-28 14:13:47.356: E/AudioRecorder(32641): at android.media.MediaPlayer.prepare(Native Method)
01-28 14:13:47.356: E/AudioRecorder(32641): at com.example.customdialogrecord.MainActivity.play(MainActivity.java:200)
01-28 14:13:47.356: E/AudioRecorder(32641): at com.example.customdialogrecord.MainActivity$1$2.onClick(MainActivity.java:106)
01-28 14:13:47.356: E/AudioRecorder(32641): at android.view.View.performClick(View.java:2538)
01-28 14:13:47.356: E/AudioRecorder(32641): at android.view.View$PerformClick.run(View.java:9152)
01-28 14:13:47.356: E/AudioRecorder(32641): at android.os.Handler.handleCallback(Handler.java:587)
01-28 14:13:47.356: E/AudioRecorder(32641): at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 14:13:47.356: E/AudioRecorder(32641): at android.os.Looper.loop(Looper.java:130)
01-28 14:13:47.356: E/AudioRecorder(32641): at android.app.ActivityThread.main(ActivityThread.java:3691)
01-28 14:13:47.356: E/AudioRecorder(32641): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 14:13:47.356: E/AudioRecorder(32641): at java.lang.reflect.Method.invoke(Method.java:507)
01-28 14:13:47.356: E/AudioRecorder(32641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
01-28 14:13:47.356: E/AudioRecorder(32641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
01-28 14:13:47.356: E/AudioRecorder(32641): at dalvik.system.NativeStart.main(Native Method)