您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Android实现录制音频和播放音频文件
Android实现录制音频和播放音频文件ndroid实现录制音频和播放音频文件12345678910111213141516171819202122232425262728293031323334353637383940packagecom.example.androidaudio;importjava.io.File;importjava.io.IOException;importandroid.app.Activity;importandroid.media.MediaPlayer;importandroid.media.MediaRecorder;importandroid.os.Bundle;importandroid.os.Environment;importandroid.util.Log;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;publicclassMainActivityextendsActivity{privatestaticfinalStringLOG_TAG=Audio_Test;publicFileFileName=null;publicButtonbutton_start_play;publicButtonbutton_finish_play;publicButtonbutton_start_record;publicButtonbutton_finish_record;publicMediaPlayermPlayer=null;publicMediaRecordermRecord=null;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button_start_record=(Button)findViewById(R.id.button_start_record);button_start_record.setOnClickListener(newstartRecordListener());button_finish_record=(Button)4142434445464748495051525354555657585960616263646566676869707172737475767778798081828384findViewById(R.id.button_finish_record);button_finish_record.setOnClickListener(newfinishRecordListener());button_start_play=(Button)findViewById(R.id.button_start_play);button_start_play.setOnClickListener(newstartPlayListener());button_finish_play=(Button)findViewById(R.id.button_finish_play);button_finish_play.setOnClickListener(newfinishPlayListener());}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}classstartRecordListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){Stringfile=text.amr;try{FileName=File.createTempFile(text_,.amr);}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}mRecord=newMediaRecorder();Log.d(LOG_TAG,start*****);try{mRecord.setAudioSource(MediaRecorder.AudioSource.MIC);mRecord.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);mRecord.setOutputFile(FileName.getAbsolutePath());mRecord.setAudioEncoder(MediaRecorder.AudioEncoder.A85868788899091929394959697989910010110210310410510610710810911011111211311MR_NB);Log.d(LOG_TAG,录音准备工作就绪);mRecord.prepare();Log.d(LOG_TAG,开始录音);mRecord.start();}catch(IllegalStateExceptione){Log.i(LOG_TAG,recordpreparefailed);e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}classfinishRecordListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){if(mRecord!=null){mRecord.setOnErrorListener(null);mRecord.stop();mRecord.release();mRecord=null;}}}classstartPlayListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){mPlayer=newMediaPlayer();try{mPlayer.setDataSource(FileName.getAbsolutePath());Log.i(LOG_TAG,播放音频文件准备工作结束);mPlayer.prepare();mPlayer.start();}catch(IllegalArgumentExceptione){Log.e(LOG_TAG,e.toString());e.printStackTrace();411511611711811912012112212312412512612712812913013113213313413513}catch(SecurityExceptione){Log.e(LOG_TAG,e.toString());e.printStackTrace();}catch(IllegalStateExceptione){Log.e(LOG_TAG,e.toString());e.printStackTrace();}catch(IOExceptione){Log.e(LOG_TAG,e.toString());e.printStackTrace();}}}classfinishPlayListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){Log.d(LOG_TAG,停止播放音频文件);if(mPlayer!=null){mPlayer.stop();mPlayer.release();mPlayer=null;}}}}6137138139140141142143144145146147148AndroidManefest.xml文件内容:1234567891011121314?xmlversion=1.0encoding=utf-8?manifestxmlns:android==com.example.androidaudioandroid:versionCode=1android:versionName=1.0uses-sdkandroid:minSdkVersion=8android:targetSdkVersion=17/applicationandroid:allowBackup=trueandroid:icon=@drawable/ic_launcherandroid:label=@string/app_name151617181920212223242526272829android:theme=@style/AppThemeactivityandroid:name=com.example.androidaudio.MainActivityandroid:label=@string/app_nameintent-filteractionandroid:name=android.intent.action.MAIN/categoryandroid:name=android.intent.category.LAUNCHER//intent-filter/activity/applicationuses-permissionandroid:name=android.permission.RECORD_AUDIO//manifest布局文件:12345678910111213141516171819202122RelativeLayoutxmlns:android=:tools=:layout_width=wrap_contentandroid:layout_height=match_parentandroid:paddingBottom=@dimen/activity_vertical_marginandroid:paddingLeft=@dimen/activity_horizontal_marginandroid:paddingRight=@dimen/activity_horizontal_marginandroid:paddingTop=@dimen/activity_vertical_margintools:context=.MainActivityTextViewandroid:id=@+id/textView1android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=@string/hello_world/Buttonandroid:id=@+id/button_finish_playandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_alignLeft=@+id/button_start_play232425262728293031323334353637383940
本文标题:Android实现录制音频和播放音频文件
链接地址:https://www.777doc.com/doc-2897251 .html