我正在开发一个音频应用程序,我想知道为什么我的录音机不工作。根据 eclipse 没有可见的错误,因此我很不清楚为什么我的录制按钮不起作用
这是我的 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:onClick="buttonTapped"
android:text="Start" />
<Button
android:id="@+id/btnfinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnstart"
android:layout_centerHorizontal="true"
android:onClick="buttonTapped"
android:text="Finish" />
<Button
android:id="@+id/btnplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btnfinish"
android:layout_below="@+id/btnfinish"
android:onClick="buttonTapped"
android:text="Play" />
<Button
android:id="@+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btnstart"
android:layout_below="@+id/btnplay"
android:onClick="buttonTaped"
android:text="stop" />
</RelativeLayout>
声音类文件:
package com.example.testproject;
import java.io.File;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
public class Sound extends Activity {
private MediaPlayer mediaPlayer;
private MediaRecorder recorder;
private String OUTPUT_FILE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
OUTPUT_FILE=Environment.getExternalStorageDirectory()+"/audiorecorder.3gpp";
}
public void buttonTapped(View view){
switch(view.getId()){
case R.id.btnstart:
try {
beginRecording();
}catch(Exception e){
e.printStackTrace();
}
break;
case R.id.btnfinish:
try {
stopRecording();
}catch(Exception e){
e.printStackTrace();
}
break;
case R.id.btnplay:
try {
playRecording();
}catch(Exception e){
e.printStackTrace();
}
break;
case R.id.btnstop:
try {
stopPlayback();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
private void stopPlayback(){
}
private void playRecording() throws Exception{
ditchMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);
mediaPlayer.prepare();
mediaPlayer.start();
}
private void ditchMediaPlayer(){
if(mediaPlayer != null)
{
try{
mediaPlayer.release();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
private void stopRecording(){
if(recorder != null)
recorder.stop();
}
private void beginRecording() throws Exception{
ditchMediaRecorder();
File outputFile = new File(OUTPUT_FILE);
if(outputFile.exists())
outputFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
private void ditchMediaRecorder() {
if(recorder != null)
recorder.release();
}
}
安卓清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="11" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testproject.Testproject"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Camera"
android:label="@string/app_name" />
</application>
</manifest>