android进行录音功能并保存播放
16lz
2022-05-20
在android中进行录音相对来说是比较简单的,使用系统提供的MediaRecorder类进行录音并保存,然后调用MediaPlayer进行播放。以下为xml配置文件代码:
<RelativeLayout xmlns:android="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" > <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:text="@string/start" /> <Button android:id="@+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" /> <Button android:id="@+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" /> <Button android:id="@+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" /> <Button android:id="@+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" /></RelativeLayout>
在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; @Override protected 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); } @Override public 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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.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 stub player.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() { @Override public 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开发中调用相应的功能时,必须在主配置文件中授予相应的权限,在配置文件中添加如下代码:
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
好了,现在就可以使用了,觉得有用的朋友请点个赞。
demo下载地址:http://download.csdn.net/detail/myname_kk/9280417
更多相关文章
- android中listview的setAdapter()和getAdapter()
- Android简单调用相机Camera功能,实现打开照相功能
- Fragments的初识---android开发
- Android中如何调用其它应用
- android 视频播放器的制作
- Android中有用的组件被选中和按下去的变换样式
- 调用Android系统设置中的Intent
- android ndk 调用第三方so
- Android实现伴奏录音合成MP3