Search code examples
androidaudio-recordingaudiotrackvisualizer

Crash when calls AudioTrack multiple times


I am working with Visualizer. It will get data from AudioTrack and display when I click a button. In the button, I will call the function DrawStart as below:

  private void DrawStart() {
        if (startDrawing) {
            initRecorder();
            mVisualizerView.link(track);    
            startRecording();       
        }
    else {
        DrawStop();
    }

}

It works well for about 10 first click. That means if I call DrawStart more than 10 times it has error

Fatal signal 11 (SIGSEGV) at 0x00030000 (code=1), thread 8164 (Visualizer)  

Could you help me to fix it? Thanks so much. There are my sub-fuctions

private void initRecorder() {
    _audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int maxJitter = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

    track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM);
    _audioManager.startBluetoothSco();
    _audioManager.setMode(AudioManager.STREAM_VOICE_CALL);      

}
private void startRecording() {
        recordingThread = new AudioRecordingThread(track,mRecorder, bufferSize,SAMPLE_RATE,new AudioRecordingHandler()  {
        // Do something
        });
        recordingThread.start();
    }

private void DrawStop() {
    if (recordingThread != null) {
        recordingThread = null;
    }
    track.release();
    startDrawing = true;
}

And

public void link(AudioTrack player)
  {
    if(player == null)
    {
      throw new NullPointerException("Cannot link to null MediaPlayer");
    }

    int playerId=player.getAudioSessionId();
    // Create the Visualizer object and attach it to our media player.
    mVisualizer = new Visualizer(playerId);
    mVisualizer.setScalingMode(Visualizer.SCALING_MODE_NORMALIZED);
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
}

Solution

  • It was fixed by setting the application's hardwareAccelerated attribute to false in AndroidManifest.xml

    <application
        android:hardwareAccelerated="false"