@@ -64,7 +64,11 @@ public boolean startCapture(MediaProjection mediaProjection) {
6464
6565 try {
6666 int bufferSize = AudioRecord .getMinBufferSize (SAMPLE_RATE , CHANNEL_CONFIG , AUDIO_FORMAT );
67- bufferSize *= BUFFER_SIZE_MULTIPLIER ; // Increase buffer for stability
67+ if (bufferSize <= 0 ) {
68+ YaaccLogger .e (getClass ().getName (), "Invalid buffer size: " + bufferSize + ", using default" );
69+ bufferSize = 4096 ;
70+ }
71+ bufferSize *= BUFFER_SIZE_MULTIPLIER ;
6872
6973 AudioPlaybackCaptureConfiguration config = new AudioPlaybackCaptureConfiguration .Builder (mediaProjection )
7074 .addMatchingUsage (android .media .AudioAttributes .USAGE_MEDIA )
@@ -82,23 +86,72 @@ public boolean startCapture(MediaProjection mediaProjection) {
8286 .setBufferSizeInBytes (bufferSize )
8387 .build ();
8488
89+ int state = audioRecord .getState ();
90+ if (state != AudioRecord .STATE_INITIALIZED ) {
91+ YaaccLogger .e (getClass ().getName (), "AudioRecord not initialized, state: " + state );
92+ // Try with smaller buffer as fallback
93+ return tryStartWithSmallerBuffer (config , mediaProjection );
94+ }
95+
96+ audioRecord .startRecording ();
97+ isCapturing = true ;
98+
99+ captureThread = new Thread (this ::captureLoop );
100+ captureThread .setPriority (Thread .MAX_PRIORITY );
101+ captureThread .start ();
102+
103+ YaaccLogger .i (getClass ().getName (), "Audio capture started, buffer: " + bufferSize );
104+ return true ;
105+
106+ } catch (IllegalArgumentException e ) {
107+ YaaccLogger .e (getClass ().getName (), "AudioRecord creation failed (invalid parameters)" , e );
108+ stopCapture ();
109+ return false ;
110+ } catch (Exception e ) {
111+ YaaccLogger .e (getClass ().getName (), "Failed to start audio capture" , e );
112+ stopCapture ();
113+ return false ;
114+ }
115+ }
116+
117+ /**
118+ * Fallback: try with smaller buffer if initial attempt fails.
119+ */
120+ private boolean tryStartWithSmallerBuffer (AudioPlaybackCaptureConfiguration config , MediaProjection mediaProjection ) {
121+ try {
122+ YaaccLogger .w (getClass ().getName (), "Trying with minimum buffer size..." );
123+ int minBuffer = AudioRecord .getMinBufferSize (SAMPLE_RATE , CHANNEL_CONFIG , AUDIO_FORMAT );
124+ if (minBuffer <= 0 ) {
125+ minBuffer = 4096 ;
126+ }
127+
128+ audioRecord = new AudioRecord .Builder ()
129+ .setAudioPlaybackCaptureConfig (config )
130+ .setAudioFormat (new AudioFormat .Builder ()
131+ .setEncoding (AUDIO_FORMAT )
132+ .setSampleRate (SAMPLE_RATE )
133+ .setChannelMask (CHANNEL_CONFIG )
134+ .build ())
135+ .setBufferSizeInBytes (minBuffer )
136+ .build ();
137+
85138 if (audioRecord .getState () != AudioRecord .STATE_INITIALIZED ) {
86- YaaccLogger .e (getClass ().getName (), "AudioRecord not initialized" );
139+ YaaccLogger .e (getClass ().getName (), "AudioRecord still not initialized with min buffer " );
87140 return false ;
88141 }
89142
90143 audioRecord .startRecording ();
91144 isCapturing = true ;
92145
93146 captureThread = new Thread (this ::captureLoop );
94- captureThread .setPriority (Thread .MAX_PRIORITY ); // High priority for audio
147+ captureThread .setPriority (Thread .MAX_PRIORITY );
95148 captureThread .start ();
96149
97- YaaccLogger .i (getClass ().getName (), "Audio capture started with buffer size: " + bufferSize );
150+ YaaccLogger .i (getClass ().getName (), "Audio capture started with fallback buffer" );
98151 return true ;
99152
100153 } catch (Exception e ) {
101- YaaccLogger .e (getClass ().getName (), "Failed to start audio capture" , e );
154+ YaaccLogger .e (getClass ().getName (), "Fallback audio capture also failed " , e );
102155 stopCapture ();
103156 return false ;
104157 }
0 commit comments