前两天忽略想到的一个功能,手机作为麦克风扩音器,我认为这是一个很有意思的事情,在网上也找到了一个这样的apk但我还是想自己动手实现。
实现麦克风的原来很简单,通过录音功能将声音录下来再通过播放功能将录到音频流实时播放出来,最终达到麦克风扩音功能。
效果如下
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:voiceView="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright">
<com.carlos.voiceline.mylibrary.VoiceLineView
android:id="@+id/voicLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@android:color/white"
voiceView:maxVolume="200"
voiceView:middleLine="@android:color/black"
voiceView:middleLineHeight="1dp"
voiceView:fineness="three"
voiceView:rectSpace="2dp"
voiceView:rectWidth="5dp"
voiceView:sensibility="four"
voiceView:viewMode="line"
voiceView:lineSpeed="90"
voiceView:voiceLine="@android:color/holo_green_dark" />
<Button
android:id="@+id/start"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="录音"
/>
</RelativeLayout>
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
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.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.carlos.voiceline.mylibrary.VoiceLineView;
public class MainActivity extends AppCompatActivity implements Runnable {
String TAG = "MainActivity";
static final int frequency = 44100;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int recBufSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding)*2;
int plyBufSize = AudioTrack.getMinBufferSize(frequency,
channelConfiguration, audioEncoding)*2;
private boolean isAlive = true;
private VoiceLineView voiceLineView;
private Button start;
AudioRecord audioRecord;
AudioTrack audioTrack;
Thread thread = null;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg != null)
voiceLineView.setVolume(msg.what);//主线程更新分贝大小
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
voiceLineView = (VoiceLineView) findViewById(R.id.voicLine);
start = (Button) findViewById(R.id.start);
checkPermission(this);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(thread == null) {
thread = new Thread(MainActivity.this);//启动线程