使用MediaPlayer实现简单的录音
来源:互联网 发布:电脑编程多久能学会 编辑:程序博客网 时间:2024/06/07 09:36
1.布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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.ebeijia.audiotest.MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="32dp" android:text="@string/Recording" android:textAppearance="?android:attr/textAppearanceMedium" /> <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="37dp" android:scaleType="fitXY" android:src="@android:drawable/presence_audio_online" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView1" android:layout_marginTop="67dp" android:layout_toLeftOf="@+id/imageView1" android:onClick="start" android:text="@string/start" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_alignRight="@+id/textView1" android:layout_marginRight="40dp" android:onClick="stop" android:text="@string/stop" /> <Button android:id="@+id/button3" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_centerHorizontal="true" android:onClick="play" android:text="@string/play" /></RelativeLayout>
三个按钮分别用于开始录音、停止录音、播放已经录好的录音。
2.实现
import android.media.MediaPlayer;import android.media.MediaRecorder;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import java.io.IOException;public class MainActivity extends AppCompatActivity { private MediaRecorder myAudioRecorder; private String outputFile = null; private Button start,stop,play; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); //进行初始化操作 } //进行一些初始化工作 public void init() { start = (Button)findViewById(R.id.button1); stop = (Button)findViewById(R.id.button2); play = (Button)findViewById(R.id.button3); stop.setEnabled(false); play.setEnabled(false); //录音录完存放的地址 outputFile = Environment.getExternalStorageDirectory(). getAbsolutePath() + "/myrecording.3gp";; myAudioRecorder = new MediaRecorder(); myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB); myAudioRecorder.setOutputFile(outputFile); } //开始录音,这里需要捕获IO异常 public void start(View view){ try { myAudioRecorder.prepare(); myAudioRecorder.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } start.setEnabled(false); stop.setEnabled(true); Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show(); } //停止录音 public void stop(View view){ myAudioRecorder.stop(); myAudioRecorder.release(); myAudioRecorder = null; stop.setEnabled(false); play.setEnabled(true); Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show(); } //播放录好的录音 这里给MediaPlayer设置了一个监听器,只有当录音播放完毕之后,才能开始下一次的播放 public void play(View view) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{ MediaPlayer m = new MediaPlayer(); m.setDataSource(outputFile); m.prepare(); m.start(); play.setEnabled(false); m.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { play.setEnabled(true); } }); Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show(); }}
0 0
- 使用MediaPlayer实现简单的录音
- 简单的MediaPlayer使用
- MediaPlayer的简单使用
- 简单的使用MediaPlayer
- 使用MediaRecorder和MediaPlayer实现录音和播放功能-以及碰到的问题
- Android--使用AudioRecord录音,并使用MediaPlayer播放的实例
- 【Android】MediaRecorder/MediaPlayer实现录音与播放录音
- android MediaPlayer实现简单的音乐播放
- android 通过MediaRecorder实现简单的录音
- Android 实现简单的录音功能
- ios-录音功能的简单实现
- MediaRecorder和MediaPlayer的简单使用
- Android中MediaPlayer的简单使用
- MediaPlayer 播放 MP3文件的简单使用
- 使用MediaPlayer播放音乐文件的简单例子
- Android MediaPlayer的简单使用(一)
- Android MediaPlayer的简单使用(二)
- MediaRecorder与MediaPlayer的简单使用
- Maven学习 (一) 搭建Maven环境
- 【JavaWeb-2】JDBC的1个类3个接口、使用步骤、连接关闭优化、防SQL注入优化
- 201512-3 画图
- create()方法和add()方法
- HighLighting System插件的使用以及一些问题
- 使用MediaPlayer实现简单的录音
- 【读书笔记《Android游戏编程之从零开始》】1.Android 平台简介与环境搭建
- 选择矩阵(选择矩阵是稀疏的)相关计算
- Add Two Numbers
- js时间戳格式化成日期格式的多种方法
- OpenResty初步使用
- RAC数据库迁移(ASM磁盘组迁移)
- Hadoop与Spark的核心组件对比
- Linux设备驱动--LCD平台设备与驱动(tiny4412)