Android AudioTrack cannot be INITIALIZED
I have a problem with initializing android AudioTrack. I have Nexus One with android 2.3.3.
Here is my code:
int _rate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
int buffersize = AudioRecord.getMinBufferSize(_rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, _rate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize, AudioTrack.MODE_STREAM);
if (atrack.getState() == AudioTrack.STATE_UNINITIALIZED) ...
So the state of the atrack is always AudioTrack.STATE_UNINITIALIZED
The application manifest is
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="BigLeftEarActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
&开发者_StackOverflowlt;/application>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
<uses-sdk android:minSdkVersion="10" />
Could you help me? What could be wrong?
Thanks!
I found a solution:
After line
int buffersize = AudioRecord.getMinBufferSize(_rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
I added
buffersize *= 2;
and audio track was initialized.
This is not a perfect solution. Maybe it would be enough to add just one byte to the buffer, but it works.
"Feci quod potui, faciant meliora potentes Feci quod potui, faciant meliora potentes."
I think I found your issue:
You are using the AudioRecord.getMinBufferSize() for an AudioTrack. You should use the AudioTrack.getMinBUfferSize() instead, this should give you the appropriate value.
mAudioRecBufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO , AudioFormat.ENCODING_PCM_16BIT);
mAudioPlayBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO , AudioFormat.ENCODING_PCM_16BIT);
精彩评论