Mediarecorder录音(保存为本地文件AAC格式)

package com.example.ffmpegaudio;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.media.MediaRecorder;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private MediaRecorder mediarecorder;
	private LocalSocket sender;
	private LocalSocket received;
	private LocalServerSocket lss;
	private static final int BUFFER_SIZE=5000;
	Button sendButton;
	Button receivedButton;
	private InputStream inputStream;
	private Recoder recoder;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sendButton=(Button)findViewById(R.id.send);
		receivedButton=(Button)findViewById(R.id.received);
		sendButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//启动midiarecoder录音
				recoder=new Recoder();
				recoder.start();
			}
		});
		receivedButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.v("fdsa", "stop the recording...");
				if(mediarecorder!=null){
					mediarecorder.stop();
					mediarecorder.release();
					mediarecorder=null;
				}
			}
		});
		
	}
	
	class Recoder extends Thread{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			File file=new File("/sdcard/today.aac");
			
			if (mediarecorder==null) {
				mediarecorder=new MediaRecorder();
			}
			mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
			mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
			mediarecorder.setOutputFile(file.getAbsolutePath());
			try {
				mediarecorder.prepare();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mediarecorder.start();
			Log.v("fdsa", "recoding audio....");
		}
	}

}
记得在权限中添加:读取外部存储的权限和Recorder.Audio的权限

你可能感兴趣的:(Mediarecorder录音(保存为本地文件AAC格式))