[請益] 錄音程式
最近教授給了我個題目
殊不知才寫第一題就卡到陰
是要寫一個錄音跟錄影的app
儲存權限設了
畫面可以跑出來 但是一按下按鈕可怕的事情就發生了
log cat : RunTimeException setAudioSourceFaild
以下是程式碼
package test.example.videorecorderactivity;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
public class VideoRecorderActivity extends Activity {
private Button StartMediaRecord;
private Button StartSoundRecord;
private Button StopMediaRecord;
private Button StopSoundRecord;
private Button Exit;
private MediaRecorder mediaRecorder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_recorder);
StartMediaRecord = (Button)findViewById(R.id.StartMediaRecord);
StartSoundRecord = (Button)findViewById(R.id.StartSounadRecord);
StopMediaRecord = (Button)findViewById(R.id.StopMediaRecord);
StopSoundRecord = (Button)findViewById(R.id.StopSoundRecord);
Exit = (Button)findViewById(R.id.Exit);
StartSoundRecord.setEnabled(true );
StopSoundRecord.setEnabled(false);
StartMediaRecord.setEnabled(true );
StopMediaRecord.setEnabled(false );
Exit.setEnabled(true );
mediaRecorder = null;
StartSoundRecord.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
String SoundRecordfileName = "SoundRecord.amr";
try {
//取得SD卡位置
File SDCardpath = Environment.getExternalStorageDirectory();
//SD卡位置下的/download目錄
File myDataPath = new File( SDCardpath.getAbsolutePath() + "/download" );
//判斷若資料夾不存在則新建download資料夾
if( !myDataPath.exists() ) myDataPath.mkdirs();
mediaRecorder = new MediaRecorder();
//設定音源為麥克風
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//設定輸出檔案格式為THREE_GPPRAW_AMR:3pg格式,H263視頻/ARM音頻編碼
//RAW_AMR:只支持音頻且音頻編碼要求維AMR_NB
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
//設定音頻文件的編碼
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//設定輸出文件的路徑
File recodeFile = new File(SDCardpath.getAbsolutePath() +
"/download/"+SoundRecordfileName);
mediaRecorder.setOutputFile(recodeFile.getAbsolutePath());
mediaRecorder.prepare();
mediaRecorder.start();
}
catch (IOException e) {e.printStackTrace();}
}});
}
}
懇請各位大大救命
程式碼幾乎都是上網找資料打的
因為其實只有去補習班上過幾堂課翻翻書學過而已
學校並沒有開android的課程
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.160.218.201
推
12/09 14:58, , 1F
12/09 14:58, 1F
→
12/09 14:58, , 2F
12/09 14:58, 2F
→
12/09 14:59, , 3F
12/09 14:59, 3F