2011年8月26日

[Android]以背景服務的方式錄製聲音

一、簡介
現在大部份使用者所看見的執行畫面都是前端的程式(Activity),但是還有很多的服務需要在背景中執行,這個功能則需要使用到Service。
由於背景執行所以是要寫在Service而不是Activity,因此需要在權限頁面(AndroidManifest)中新增一個Service的服務。

二、方法
(1)  Service:這個服務本身是無介面,也並非一個獨立的Process,只是有需要長時間執行的獨立程序為了不影響主程式的運行,就會使用Service。



(2)   MediaRecorder:這個類別主要是 Android錄音錄影所使用到的,詳細資料可以參詳官方網站。

三、程式碼

Activity.java

package tw.Recording;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class RecordingActivity extends Activity {
	
	private Button start,stop;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        start=(Button)findViewById(R.id.start); 
        stop=(Button)findViewById(R.id.stop);
        
        start.setOnClickListener(new OnClickListener(){
        	//執行背景作業
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(RecordingActivity.this, RecordingService.class);
				           startService(intent);
			}});
        stop.setOnClickListener(new OnClickListener(){
        	
        	//停止背景作業
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(RecordingActivity.this, RecordingService.class);
	            stopService(intent);
			}});
    }
}

Service.java
package tw.Recording;

import java.io.File;
import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;

public class RecordingService extends Service {
	private MediaRecorder mediaRecorder = null;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	//錄音
	public void onStart(Intent intent,int startId)
	{
		//設定錄音檔名	
		String fileName = "test.amr";
		try{
			File SDCardpath=Environment.getExternalStorageDirectory();
			File myDataPath=new File(SDCardpath.getAbsolutePath()+"/download");
			
			if( !myDataPath.exists() ) myDataPath.mkdirs();
			
			File recodeFile = new File(SDCardpath.getAbsolutePath() + "/download/"+fileName);
			mediaRecorder = new MediaRecorder();
			
			//設定音源
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			//設定輸出檔案的格式
			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
			//設定編碼格式
			mediaRecorder
			.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			//設定錄音檔位置
			mediaRecorder.setOutputFile(recodeFile.getAbsolutePath());
			mediaRecorder.prepare();
			//開始錄音
			mediaRecorder.start();
		}
		catch(IOException e){
		e.printStackTrace();
		}
		super.onStart(intent, startId);
	}

	@Override
	//停止背景時
	    public void onDestroy() {
		if(mediaRecorder != null) {
			    mediaRecorder.stop();
			    mediaRecorder.release();
			    mediaRecorder = null;
			    }
	        super.onDestroy();
	    }
		

}

AndroidManifest.xml(記得加入)

   
    
    

        

    


沒有留言:

張貼留言