本文实例讲述了AndroID编程开发录音和播放录音的方法。分享给大家供大家参考,具体如下:
/* * The application needs to have the permission to write to external storage * if the output file is written to the external storage,and also the * permission to record audio. These permissions must be set in the * application's AndroIDManifest.xml file,with something like: * * <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /> * <uses-permission androID:name="androID.permission.RECORD_AUdio" /> * */package com.androID.audiorecordtest;import androID.app.Activity;import androID.Widget.linearLayout;import androID.os.Bundle;import androID.os.Environment;import androID.vIEw.VIEwGroup;import androID.Widget.button;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.content.Context;import androID.util.Log;import androID.media.MediaRecorder;import androID.media.MediaPlayer;import java.io.IOException;public class AudioRecordTest extends Activity{ private static final String LOG_TAG = "AudioRecordTest"; private static String mfilename = null; private Recordbutton mRecordbutton = null; private MediaRecorder mRecorder = null; private Playbutton mPlaybutton = null; private MediaPlayer mPlayer = null; private voID onRecord(boolean start) { if (start) { startRecording(); } else { stopRecording(); } } private voID onPlay(boolean start) { if (start) { startPlaying(); } else { stopPlaying(); } } private voID startPlaying() { mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(mfilename); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { Log.e(LOG_TAG,"prepare() @R_403_5138@"); } } private voID stopPlaying() { mPlayer.release(); mPlayer = null; } private voID startRecording() { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setoutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setoutputfile(mfilename); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { mRecorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG,"prepare() @R_403_5138@"); } mRecorder.start(); } private voID stopRecording() { mRecorder.stop(); mRecorder.release(); mRecorder = null; } class Recordbutton extends button { boolean mStartRecording = true; OnClickListener clicker = new OnClickListener() { public voID onClick(VIEw v) { onRecord(mStartRecording); if (mStartRecording) { setText("Stop recording"); } else { setText("Start recording"); } mStartRecording = !mStartRecording; } }; public Recordbutton(Context ctx) { super(ctx); setText("Start recording"); setonClickListener(clicker); } } class Playbutton extends button { boolean mStartPlaying = true; OnClickListener clicker = new OnClickListener() { public voID onClick(VIEw v) { onPlay(mStartPlaying); if (mStartPlaying) { setText("Stop playing"); } else { setText("Start playing"); } mStartPlaying = !mStartPlaying; } }; public Playbutton(Context ctx) { super(ctx); setText("Start playing"); setonClickListener(clicker); } } public AudioRecordtest() { mfilename = Environment.getExternalStorageDirectory().getabsolutePath(); mfilename += "/audiorecordtest.3gp"; } @OverrIDe public voID onCreate(Bundle icicle) { super.onCreate(icicle); linearLayout ll = new linearLayout(this); mRecordbutton = new Recordbutton(this); ll.addVIEw(mRecordbutton,new linearLayout.LayoutParams( VIEwGroup.LayoutParams.WRAP_CONTENT,VIEwGroup.LayoutParams.WRAP_CONTENT,0)); mPlaybutton = new Playbutton(this); ll.addVIEw(mPlaybutton,0)); setContentVIEw(ll); } @OverrIDe public voID onPause() { super.onPause(); if (mRecorder != null) { mRecorder.release(); mRecorder = null; } if (mPlayer != null) { mPlayer.release(); mPlayer = null; } }}
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android多媒体 *** 作技巧汇总(音频,视频,录音等)》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity *** 作技巧总结》、《Android *** 作SQLite数据库技巧总结》、《Android *** 作json格式数据技巧总结》、《Android数据库 *** 作技巧总结》、《Android文件 *** 作技巧汇总》、《Android编程开发之SD卡 *** 作方法汇总》、《Android资源 *** 作技巧汇总》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android编程开发录音和播放录音简单示例全部内容,希望文章能够帮你解决Android编程开发录音和播放录音简单示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)