Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Application of recording and Broadcasting in Android

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章给大家分享的是有关Android如何实现边录边播应用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体内容如下

1.Android.mk

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_PACKAGE_NAME := testRecordinclude $(BUILD_PACKAGE)

2.AndroidManifest.xml

3.res/layout/main.xml

4.res/values/strings.xml

bianlubianbo start stop exit vlounm

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); } } }; }感谢各位的阅读!关于"Android如何实现边录边播应用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report