package fk.androiddemo_034;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends Activity {
private static final String LOG_TAG = "AudioRecordTest";
//语音文件
private String FileName = null;
//界面控件
private Button startRecord;
private Button startPlay;
private Button stopRecord;
private Button stopPlay;
//语音操作对象
private MediaPlayer mPlayer = null;
private MediaRecorder mRecorder = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//开始录音
startRecord = (Button)findViewById(R.id.startRecord);
startRecord.setOnClickListener(new startRecordListener());
//结束录音
stopRecord = (Button)findViewById(R.id.stopRecord);
stopRecord.setOnClickListener(new stopRecordListener());
//开始播放
startPlay = (Button)findViewById(R.id.startPlay);
startPlay.setOnClickListener(new startPlayListener());
//结束播放
stopPlay = (Button)findViewById(R.id.stopPlay);
stopPlay.setOnClickListener(new stopPlayListener());
//设置sdcard的路径
FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
FileName += "/FK_AudioTest.3gp";//会存放到手机外部存储最外层目录
}
//开始录音
class startRecordListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(FileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
}
//停止录音
class stopRecordListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
//播放录音
class startPlayListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer = new MediaPlayer();
try{
mPlayer.setDataSource(FileName);
mPlayer.prepare();
mPlayer.start();
}catch(IOException e){
Log.e(LOG_TAG,"播放失败");
}
}
}
//停止播放录音
class stopPlayListener implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer.release();
mPlayer = null;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android音频操作:录音和播放
共71个文件
xml:39个
bin:6个
png:5个
5星 · 超过95%的资源 需积分: 19 43 下载量 181 浏览量
2017-01-18
17:22:04
上传
评论
收藏 365KB ZIP 举报
温馨提示
此demo演示了在android上录音和播放的操作,主要使用:MediaPlayer,MediaRecorder 工具类。
资源推荐
资源详情
资源评论
收起资源包目录
AndroidDemo_034.zip (71个子文件)
AndroidDemo_034
gradlew.bat 2KB
gradlew 5KB
gradle.properties 747B
.gradle
2.14.1
tasks
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 20KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.lock 17B
localClassSetAnalysis.bin 23KB
taskArtifacts
fileHashes.bin 140KB
cache.properties.lock 17B
taskArtifacts.bin 79KB
fileSnapshotsToTreeSnapshotsIndex.bin 25KB
fileSnapshots.bin 1.1MB
cache.properties 31B
AndroidDemo_034.iml 869B
app
proguard-rules.pro 683B
src
test
java
fk
androiddemo_034
ExampleUnitTest.java 412B
androidTest
java
fk
androiddemo_034
ExampleInstrumentedTest.java 766B
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
drawable
mipmap-hdpi
ic_launcher.png 3KB
values
colors.xml 214B
strings.xml 81B
styles.xml 380B
dimens.xml 216B
mipmap-xxhdpi
ic_launcher.png 8KB
mipmap-mdpi
ic_launcher.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
layout
activity_main.xml 1KB
values-w820dp
dimens.xml 364B
java
fk
androiddemo_034
MainActivity.java 3KB
AndroidManifest.xml 833B
app.iml 13KB
.gitignore 8B
build.gradle 895B
libs
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 233B
.idea
gradle.xml 721B
runConfigurations.xml 564B
misc.xml 3KB
copyright
profiles_settings.xml 76B
modules.xml 367B
encodings.xml 159B
compiler.xml 686B
workspace.xml 143KB
libraries
javawriter_2_1_1.xml 330B
jsr305_2_0_1.xml 330B
rules_0_5.xml 742B
javax_annotation_api_1_2.xml 358B
support_core_utils_24_2_1.xml 798B
support_compat_24_2_1.xml 952B
support_media_compat_24_2_1.xml 994B
runner_0_5.xml 748B
support_fragment_24_2_1.xml 966B
support_vector_drawable_24_2_1.xml 661B
support_annotations_24_2_1.xml 537B
hamcrest_library_1_3.xml 342B
support_core_ui_24_2_1.xml 959B
espresso_idling_resource_2_2_2.xml 703B
animated_vector_drawable_24_2_1.xml 666B
support_v4_24_2_1.xml 420B
hamcrest_core_1_3.xml 526B
appcompat_v7_24_2_1.xml 782B
junit_4_12.xml 477B
android_android_24.xml 262B
hamcrest_integration_1_3.xml 354B
espresso_core_2_2_2.xml 648B
javax_inject_1.xml 324B
exposed_instrumentation_api_publish_0_5.xml 721B
.gitignore 127B
local.properties 542B
settings.gradle 16B
build.gradle 521B
共 71 条
- 1
资源评论
- 馒头神山2018-07-18挺好。可以拿来做参考。
风之盔
- 粉丝: 137
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功