Android 录音(MediaRecorder)与播放(MediaPlayer)

通过MediaRecorder和MediaPlayer实现声音的录制和播放,代码比较简单,直接贴代码。xml文件里面只有四个按钮就不贴了。

UI


代码:

VoiceActivity.class

package com.zy.ione;

import com.zy.media.UPlayer;
import com.zy.media.URecorder;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class VoiceActivity extends Activity {

	private String path = null;
	private URecorder recorder;
	private UPlayer player;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_voice);
		
		
		path = Environment.getExternalStorageDirectory().getAbsolutePath();  
		path += "/ione.pcm";  
		
		recorder = new URecorder(path);
		player = new UPlayer(path);
		
		//开始录音
		findViewById(R.id.ione_record_start_btn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {	
				
				Toast.makeText(VoiceActivity.this, "start record", Toast.LENGTH_SHORT).show();
				
				recorder.start();
			}
		});
		
		//停止录音
        findViewById(R.id.ione_record_stop_btn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Toast.makeText(VoiceActivity.this, "stop record", Toast.LENGTH_SHORT).show();
				recorder.stop();
			}
		});
        
       //开始播放
      	findViewById(R.id.ione_play_start_btn).setOnClickListener(new OnClickListener() {
      			
      		@Override
      		public void onClick(View arg0) {	
      				
      			Toast.makeText(VoiceActivity.this, "start play", Toast.LENGTH_SHORT).show();
      			player.start();
      		}
      	});
      		
      	//停止播放
         findViewById(R.id.ione_play_stop_btn).setOnClickListener(new OnClickListener() {
      			
      		@Override
      		public void onClick(View arg0) {
      			Toast.makeText(VoiceActivity.this, "stop play", Toast.LENGTH_SHORT).show();
      			player.stop();
      		}
      	});
	}

}
操作接口
package com.zy.media;

public interface IVoiceManager {
	
	public boolean start();
	public boolean stop();
}

录音

package com.zy.media;

import java.io.IOException;

import android.media.MediaRecorder;
import android.util.Log;

public class URecorder implements IVoiceManager{
	
	private final String TAG = URecorder.class.getName();
	private String path;
	private MediaRecorder mRecorder;  
	public URecorder(String path){
		this.path = path;
		mRecorder = new MediaRecorder();
	}
	
	/*
	 * 开始录音
	 * @return boolean
	 */
	@Override
	public boolean start() {	
		//设置音源为Micphone  
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
        //设置封装格式  
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);  
        mRecorder.setOutputFile(path);  
        //设置编码格式  
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);  
  
        try {  
            mRecorder.prepare();  
        } catch (IOException e) {  
            Log.e(TAG, "prepare() failed");  
        }  
        //录音
        mRecorder.start();  
		return false;
	}

	/*
	 * 停止录音
	 * @return boolean
	 */
	@Override
	public boolean stop() {
		mRecorder.stop();  
        mRecorder.release();  
        mRecorder = null; 
		return false;
	}
	
}

播放

package com.zy.media;


import android.media.MediaPlayer;
import android.util.Log;

public class UPlayer implements IVoiceManager {
	
    private final String TAG = UPlayer.class.getName();
	private String path;
	
	private MediaPlayer mPlayer;
	public UPlayer(String path){
		this.path = path;
		mPlayer = new MediaPlayer();
	}
	
	@Override
	public boolean start() {
        try {  
			 //设置要播放的文件
			 mPlayer.setDataSource(path);
			 mPlayer.prepare();
			 //播放
			 mPlayer.start();		
		 }catch(Exception e){
			 Log.e(TAG, "prepare() failed");  
		 }

		return false;
	}

	@Override
	public boolean stop() {
		mPlayer.stop();
		mPlayer.release();
		mPlayer = null;
		return false;
	}

}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值