在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中进行录音,代码如下:

本文介绍了在Android中如何使用MediaRecorder进行录音并保存,以及如何通过MediaPlayer进行播放。首先,XML配置文件设置是基础,然后在MainActivity中利用MediaRecorder进行录音。接着,创建RecordPlayer类来播放录制的音频。不过要注意,为了正常运行,需要在AndroidManifest.xml中添加相应的权限。提供了一个错误修复方案,完成这些步骤后,录音和播放功能就能正常使用。
1万+

被折叠的 条评论
为什么被折叠?



