Android实现边录边播应用
程序员文章站
2022-07-02 08:22:06
本文实例为大家分享了android实现边录边播的具体代码,供大家参考,具体内容如下1.android.mklocal_path:= $(call my-dir)include $(clear_vars...
本文实例为大家分享了android实现边录边播的具体代码,供大家参考,具体内容如下
1.android.mk
local_path:= $(call my-dir) include $(clear_vars) local_module_tags := optional local_src_files := $(call all-subdir-java-files) local_package_name := testrecord include $(build_package)
2.androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.testrecord" android:versioncode="1" android:versionname="1.0"> <uses-permission android:name="android.permission.record_audio"></uses-permission> <application android:icon="@drawable/icon" android:label="bug report sender"> <activity android:name=".testrecord" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> </application> </manifest>
3.res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <button android:layout_height="wrap_content" android:id="@+id/btnrecord" android:layout_width="fill_parent" android:text="@string/btnr"></button> <button android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/btns" android:id="@+id/btnstop"></button> <button android:layout_height="wrap_content" android:id="@+id/btnexit" android:layout_width="fill_parent" android:text="@string/btne"></button> <textview android:id="@+id/textview01" android:layout_height="wrap_content" android:text="@string/textv" android:layout_width="fill_parent"></textview> <seekbar android:layout_height="wrap_content" android:id="@+id/skbvolume" android:layout_width="fill_parent"></seekbar> </linearlayout>
4.res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">bianlubianbo</string> <string name="btnr">start</string> <string name="btns">stop</string> <string name="btne">exit</string> <string name="textv">vlounm</string> </resources>
5.res/drawable/icom.png
6.src/com/testrecord/testrecord.java
package com.testrecord; import android.app.activity; import android.media.audioformat; import android.media.audiomanager; import android.media.audiorecord; import android.media.audiotrack; import android.media.mediarecorder; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.seekbar; import android.widget.toast; public class testrecord extends activity { /** called when the activity is first created. */ button btnrecord, btnstop, btnexit; seekbar skbvolume;//调节音量 boolean isrecording = false;//是否录放的标记 static final int frequency = 8000;//44100; static final int channelconfiguration = audioformat.channel_configuration_mono; static final int audioencoding = audioformat.encoding_pcm_16bit; int recbufsize,playbufsize; audiorecord audiorecord; audiotrack audiotrack; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); settitle("助听器"); recbufsize = audiorecord.getminbuffersize(frequency, channelconfiguration, audioencoding); playbufsize=audiotrack.getminbuffersize(frequency, channelconfiguration, audioencoding); // ----------------------------------------- audiorecord = new audiorecord(mediarecorder.audiosource.mic, frequency, channelconfiguration, audioencoding, recbufsize*10); audiotrack = new audiotrack(audiomanager.stream_music, frequency, channelconfiguration, audioencoding, playbufsize, audiotrack.mode_stream); //------------------------------------------ btnrecord = (button) this.findviewbyid(r.id.btnrecord); btnrecord.setonclicklistener(new clickevent()); btnstop = (button) this.findviewbyid(r.id.btnstop); btnstop.setonclicklistener(new clickevent()); btnexit = (button) this.findviewbyid(r.id.btnexit); btnexit.setonclicklistener(new clickevent()); skbvolume=(seekbar)this.findviewbyid(r.id.skbvolume); skbvolume.setmax(100);//音量调节的极限 skbvolume.setprogress(70);//设置seekbar的位置值 audiotrack.setstereovolume(0.7f, 0.7f);//设置当前音量大小 skbvolume.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { float vol=(float)(seekbar.getprogress())/(float)(seekbar.getmax()); audiotrack.setstereovolume(vol, vol);//设置音量 } @override public void onstarttrackingtouch(seekbar seekbar) { // todo auto-generated method stub } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { // todo auto-generated method stub } }); } @override protected void ondestroy() { super.ondestroy(); android.os.process.killprocess(android.os.process.mypid()); } class clickevent implements view.onclicklistener { @override public void onclick(view v) { if (v == btnrecord) { isrecording = true; new recordplaythread().start();// 开一条线程边录边放 } else if (v == btnstop) { isrecording = false; } else if (v == btnexit) { isrecording = false; testrecord.this.finish(); } } } class recordplaythread extends thread { public void run() { try { byte[] buffer = new byte[recbufsize]; audiorecord.startrecording();//开始录制 audiotrack.play();//开始播放 while (isrecording) { //从mic保存数据到缓冲区 int bufferreadresult = audiorecord.read(buffer, 0, recbufsize); byte[] tmpbuf = new byte[bufferreadresult]; system.arraycopy(buffer, 0, tmpbuf, 0, bufferreadresult); //写入数据即播放 audiotrack.write(tmpbuf, 0, tmpbuf.length); } audiotrack.stop(); audiorecord.stop(); } catch (throwable t) { toast.maketext(testrecord.this, t.getmessage(), 1000); } } }; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 生活中含有碳水化合物的食物有哪些