Android媒体录制,Android多媒体-MediaRecorder 录制音视频

不多说,直接上代码,有具体凝视

MyAudioRecord.java

import java.io.File;

import java.io.IOException;

import android.app.Activity;

import android.content.ContentValues;

import android.content.Intent;

import android.media.MediaPlayer;

import android.media.MediaRecorder;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

/**

* 这个是利用MediaRecorder类来实现自己的音频录制程序

*

* 为了能够录制音频我们须要RECORD_AUDIO权限

* 为了能够写入SDCard,我们须要WRITE_EXTERNAL_STORAGE权限

* @author Administrator

*

*/

public class MyAudioRecord extends Activity {

private TextView stateView;

private Button btnStart,btnStop,btnPlay,btnFinish;

private MediaRecorder recorder;

private MediaPlayer player;

private File audioFile;

private Uri fileUri;

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.my_audio_record);

stateView = (TextView)this.findViewById(R.id.view_state);

stateView.setText("准备開始");

btnStart = (Button)this.findViewById(R.id.btn_start);

btnStop = (Button)this.findViewById(R.id.btn_stop);

btnPlay = (Button)this.findViewById(R.id.btn_play);

btnFinish = (Button)this.findViewById(R.id.btn_finish);

btnStop.setEnabled(false);

btnPlay.setEnabled(false);

}

public void onClick(View v){

int id = v.getId();

switch(id){

case R.id.btn_start:

//開始录制

//我们须要实例化一个MediaRecorder对象,然后进行对应的设置

recorder = new MediaRecorder();

//指定AudioSource 为MIC(Microphone audio source ),这是最长用的

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//setAudioSource/setVedioSource

//指定OutputFormat,我们选择3gp格式

//其它格式,MPEG-4:这将指定录制的文件为mpeg-4格式

//RAW_AMR:录制原始文件,这仅仅支持音频录制,同一时候要求音频编码为AMR_NB

//THREE_GPP:录制后文件是一个3gp文件,支持音频和视频录制

recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

//指定Audio编码方式,眼下仅仅有AMR_NB格式

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

//接下来我们须要指定录制后文件的存储路径

File fpath = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/data/files/");

fpath.mkdirs();//创建目录

try {

//创建暂时文件

audioFile = File.createTempFile("recording", ".3gp", fpath);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

recorder.setOutputFile(audioFile.getAbsolutePath());

//以下就開始录制了

try {

recorder.prepare();

} catch (IllegalStateException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

recorder.start();

stateView.setText("正在录制");

btnStart.setEnabled(false);

btnPlay.setEnabled(false);

btnStop.setEnabled(true);

break;

case R.id.btn_stop:

recorder.stop();

recorder.release();

//然后我们能够将我们的录制文件存储到MediaStore中

ContentValues values = new ContentValues();

values.put(MediaStore.Audio.Media.TITLE, "this is my first record-audio");

values.put(MediaStore.Audio.Media.DATE_ADDED, System.currentTimeMillis());

values.put(MediaStore.Audio.Media.DATA, audioFile.getAbsolutePath());

fileUri = this.getContentResolver().insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values);

//录制结束后,我们实例化一个MediaPlayer对象,然后准备播放

player = new MediaPlayer();

player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override

public void onCompletion(MediaPlayer arg0) {

//更新状态

stateView.setText("准备录制");

btnPlay.setEnabled(true);

btnStart.setEnabled(true);

btnStop.setEnabled(false);

}

});

//准备播放

try {

player.setDataSource(audioFile.getAbsolutePath());

player.prepare();

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalStateException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//更新状态

stateView.setText("准备播放");

btnPlay.setEnabled(true);

btnStart.setEnabled(true);

btnStop.setEnabled(false);

break;

case R.id.btn_play:

//播放录音

//注意,我们在录音结束的时候,已经实例化了MediaPlayer,做好了播放的准备

player.start();

//更新状态

stateView.setText("正在播放");

btnStart.setEnabled(false);

btnStop.setEnabled(false);

btnPlay.setEnabled(false);

//在播放结束的时候也要更新状态

break;

case R.id.btn_finish:

//完毕录制,返回录制的音频的Uri

Intent intent = new Intent();

intent.setData(fileUri);

this.setResult(RESULT_OK, intent);

this.finish();

break;

}

}

}

AndroidManifest.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值