android进行录音功能并保存播放

在android中进行录音相对来说是比较简单的,使用系统提供的MediaRecorder类进行录音并保存,然后调用MediaPlayer进行播放。以下为xml配置文件代码:

"http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.kk.soundrecording.MainActivity" >"@+id/start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginTop="40dp"android:text="@string/start" />"@+id/stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/start"android:layout_centerHorizontal="true"android:layout_marginTop="40dp"android:text="@string/stop" />"@+id/paly"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/stop"android:layout_centerHorizontal="true"android:layout_marginTop="40dp"android:text="@string/paly" />"@+id/pause_paly"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/paly"android:layout_centerHorizontal="true"android:layout_marginTop="40dp"android:text="@string/pause_paly" />"@+id/stop_paly"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/pause_paly"android:layout_centerHorizontal="true"android:layout_marginTop="40dp"android:text="@string/stop_paly" />

在MainActivity中进行录音,代码如下:

package com.example.kk.soundrecording;import java.io.File;
import java.io.IOException;
import com.example.kk.util.RecordPlayer;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;/*** * @author kk**/public class MainActivity extends Activity implements OnClickListener {// 开始录音private Button start;// 停止按钮private Button stop;// 播放按钮private Button paly;// 暂停播放private Button pause_paly;// 停止播放private Button stop_paly;// 录音类private MediaRecorder mediaRecorder;// 以文件的形式保存private File recordFile;private RecordPlayer player; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);recordFile = new File("/mnt/sdcard", "kk.amr");initView();Listener();}private void initView() {start = (Button) findViewById(R.id.start);stop = (Button) findViewById(R.id.stop);paly = (Button) findViewById(R.id.paly);pause_paly = (Button) findViewById(R.id.pause_paly);stop_paly = (Button) findViewById(R.id.stop_paly);}private void Listener() {start.setOnClickListener(this);stop.setOnClickListener(this);paly.setOnClickListener(this);pause_paly.setOnClickListener(this);stop_paly.setOnClickListener(this);}@Overridepublic void onClick(View v) {player = new RecordPlayer(MainActivity.this);int Id = v.getId();switch (Id) {case R.id.start:startRecording();break;case R.id.stop:stopRecording();break;case R.id.paly:playRecording();break;case R.id.pause_paly:pauseplayer();break;case R.id.stop_paly:stopplayer();break;}}private void startRecording() {mediaRecorder = new MediaRecorder();// 判断,若当前文件已存在,则删除if (recordFile.exists()) {recordFile.delete();}mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);mediaRecorder.setOutputFile(recordFile.getAbsolutePath());try {// 准备好开始录音mediaRecorder.prepare();mediaRecorder.start();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void stopRecording() {if (recordFile != null) {mediaRecorder.stop();mediaRecorder.release();}}private void playRecording() {player.playRecordFile(recordFile);}private void pauseplayer() {player.pausePalyer();}private void stopplayer() {// TODO Auto-generated method stubplayer.stopPalyer();}
}

同时,新建一个RecordPlayer类,用来播放保存好的录音,如下:

package com.example.kk.util;import java.io.File;import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;import com.example.kk.soundrecording.R;/*** * * @author kk   录音播放类**/public class RecordPlayer {private static MediaPlayer mediaPlayer;private Context mcontext;public RecordPlayer(Context context) {this.mcontext = context;}// 播放录音文件public void playRecordFile(File file) {if (file.exists() && file != null) {if (mediaPlayer == null) {Uri uri = Uri.fromFile(file);mediaPlayer = MediaPlayer.create(mcontext, uri);}mediaPlayer.start();//监听MediaPlayer播放完成mediaPlayer.setOnCompletionListener(new OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer paramMediaPlayer) {// TODO Auto-generated method stub//弹窗提示Toast.makeText(mcontext,mcontext.getResources().getString(R.string.ok),Toast.LENGTH_SHORT).show();}});}}// 暂停播放录音public void pausePalyer() {if (mediaPlayer.isPlaying()) {mediaPlayer.pause();Log.e("TAG", "暂停播放");}}// 停止播放录音public void stopPalyer() {// 这里不调用stop(),调用seekto(0),把播放进度还原到最开始if (mediaPlayer.isPlaying()) {mediaPlayer.pause();mediaPlayer.seekTo(0);Log.e("TAG", "停止播放");}}
}

此时,功能代码都已实现,但是运行时会报错!为什么呢,这个是被很多初学者会忘记的,那就是android开发中调用相应的功能时,必须在主配置文件中授予相应的权限,在配置文件中添加如下代码:

   "android.permission.RECORD_AUDIO" />"android.permission.WRITE_EXTERNAL_STORAGE" />"android.permission.WAKE_LOCK" />   

好了,现在就可以使用了,觉得有用的朋友请点个赞。

demo下载地址:http://download.csdn.net/detail/myname_kk/9280417


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部