Приложение крашится по нажатии кнопки START при этом будет выполнять свою задачу.
Вот код:
public class MainActivity extends AppCompatActivity {
AudioManager am;
AudioTrack track;
AudioRecord record;
Button start;
Button stop;
CheckBox checkBox;
MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.MODE_IN_COMMUNICATION);
mediaRecorder = new MediaRecorder();
stop=(Button)findViewById(R.id.b_stop);
start = (Button)findViewById(R.id.b_start);
checkBox = (CheckBox)findViewById(R.id.checkBox);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
record();
playSound();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
record.stop();
track.stop();
}
});
}
private void playSound() {
short[] myAudio = new short[1024];
int t = 0;
am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.ROUTE_HEADSET);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state=intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
if (AudioManager.SCO_AUDIO_STATE_CONNECTED ==state){
record();
}
unregisterReceiver(this);
}
});
am.startBluetoothSco();
record.startRecording();
track.play();
while (true){
t=record.read(myAudio,0,1024);
track.write(myAudio,0,t);
}
}
private void registerReceiver(BroadcastReceiver broadcastReceiver) {
}
private void record() {
record = new AudioRecord(MediaRecorder.AudioSource.MIC,8000,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,1024);
track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, 1024, AudioTrack.MODE_STREAM);
}
}
А вот лог:
06-22 16:32:06.377 17807-17807/com.wexample.aaa.mysound W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-22 16:32:06.477 17807-17807/com.wexample.aaa.mysound D/PhoneWindow: *FMB* installDecor mIsFloating : false
06-22 16:32:06.477 17807-17807/com.wexample.aaa.mysound D/PhoneWindow: *FMB* installDecor flags : -2139029248
06-22 16:32:06.587 17807-17845/com.wexample.aaa.mysound D/OpenGLRenderer: Render dirty regions requested: true
06-22 16:32:06.607 17807-17807/com.wexample.aaa.mysound D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
06-22 16:32:06.607 17807-17807/com.wexample.aaa.mysound D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
06-22 16:32:06.657 17807-17807/com.wexample.aaa.mysound D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
06-22 16:32:06.667 17807-17845/com.wexample.aaa.mysound I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: SKARAJGA_AU_LINUX_ANDROID_LA.BR.1.1.2_RB1.05.00.02.031.018+PATCH[ES]_msm8916_32_refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.2_RB1.05.00.02.031.018__release_ENGG (I856e09677e)
OpenGL ES Shader Compiler Version: E031.25.03.02
Build Date: 04/06/15 Mon
Local Branch:
Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.2_RB1.05.00.02.031.018
Local Patches: 112c106f3772623daa7b4181c6cf23491044ead1 Revert "Disable ASTC on A405"
58a118cb818fdc906095a49a90977c15f9d3b223 Remove ASTC
06-22 16:32:06.667 17807-17845/com.wexample.aaa.mysound I/OpenGLRenderer: Initialized EGL, version 1.4
06-22 16:32:06.677 17807-17845/com.wexample.aaa.mysound D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 4096
06-22 16:32:06.677 17807-17845/com.wexample.aaa.mysound D/OpenGLRenderer: Enabling debug mode 0
06-22 16:32:06.837 17807-17807/com.wexample.aaa.mysound I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2c7aaccc time:32810473
06-22 16:32:07.697 17807-17807/com.wexample.aaa.mysound D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN