I've tried the native-audio code sample in android NDK. When i try to record some speech and then play it back it works fine. It uses the main speaker(loud speaker) for the speech playback. I want to modify the code so that the speech is played in the ear speaker instead of the main speaker. Any help?
Asked
Active
Viewed 3,022 times
1 Answers
6
I solved the problem and I've found a google group of related topic. It was really helpful. Here's the link: https://groups.google.com/forum/#!topic/android-ndk/O-hufEm20cU
I tested it in the native-audio example provided with android NDK. You've to take interface of android configuration first and then set stream type to STREAM_VOICE after creating the audio player.
// Code for working with ear speaker by setting stream type to STREAM_VOICE
SLAndroidConfigurationItf playerConfig;
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_ANDROIDCONFIGURATION, &playerConfig);
SLint32 streamType = SL_ANDROID_STREAM_VOICE;
result = (*playerConfig)->SetConfiguration(playerConfig, SL_ANDROID_KEY_STREAM_TYPE, &streamType, sizeof(SLint32));

Reaz Murshed
- 23,691
- 13
- 78
- 98
-
Thanks for sharing your comments. But as i used your code after creating a player i got crashed. Please suggest how to set streamtype with OpenSL player. I want to implement with Uri player. – Amit Thaper Jun 17 '15 at 15:03
-
Take a look at this question please. This might help you. http://stackoverflow.com/questions/24758121/buffering-uri-on-opensl-android Take a look at OpenSL ES documentation. Uri player might need a different stream type to play audio data. – Reaz Murshed Jun 18 '15 at 08:47
-
Thanks for your reply. I forgot to add interface required interface array used in audioplayer. Now i am able to integrate SL_IID_ANDROIDCONFIGURATION in openSL by adding it in required interface array. As is use SL_ANDROID_STREAM_VOICE sound still come from back speaker. What to do if i want to enable sound from front center speaker – Amit Thaper Jun 18 '15 at 10:11
-
Thanks, I am able to get audio output from front speaker. But it enabled for all the installed application in the mobile device. Can i do it for our specific android application – Amit Thaper Jun 18 '15 at 12:14
-
You can set all the properties to default before destroying the audio instance. This is just an idea. Didn't face this kind of problem before. Check if you closed your recorder and audio player properly. – Reaz Murshed Jun 20 '15 at 08:20
-
Thanks i am able to use SL_ANDROID_STREAM_VOICE. How can i adjust volume for this player. As i have used device volume button but volume not increase neither decrease. Can you please help me to resolve this? – Amit Thaper Jun 29 '15 at 08:30
-
2When you've created audio player I assume you've taken volume control interface ID. Then set boolean request TRUE. Now get volume control interface just like you've got the stream type setting interface before. Try this code, `const SLInterfaceID ids[4] = {SL_IID_ANDROIDCONFIGURATION, SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND, SL_IID_SEEK, SL_IID_VOLUME}; const SLboolean req[4] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk, 5, ids, req);` – Reaz Murshed Jun 30 '15 at 05:26
-
If the comments are helpful, please upflag those. :) – Reaz Murshed Jun 30 '15 at 05:31
-
Thanks for reply, upflag the same. I used the same as you defined but not resolved. Now i am decoding the audio file to pcm to play from center_speaker and volume button is also controlled now. But problem is it plays very fastly. Can you give your valuable comment on this? – Amit Thaper Jul 01 '15 at 06:29
-
Check if your recording sampling rate and decoding sampling rates are same. Here I've formatted the pcm data for recording like this:`SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 1, SL_SAMPLINGRATE_8, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_LEFT, SL_BYTEORDER_LITTLEENDIAN};` Where the sampling rate is 8Khz. Check if the audio is decoding with correct sampling rate. Audio is playing faster means it has decoded in higher sampling rate like 44Khz or so. – Reaz Murshed Jul 01 '15 at 07:00
-
Check my answer here to check supporting sampling rates in your device. http://stackoverflow.com/questions/8043387/android-audiorecord-supported-sampling-rates/22317382#22317382 – Reaz Murshed Jul 01 '15 at 07:01
-
I used the same code as you suggest in http://stackoverflow.com/questions/8043387/android-audiorecord-supported-sampling-rates/22317382#22317382. Pass the sample rate from Java to jni function. – Amit Thaper Jul 01 '15 at 08:17
-
Since SL_IID_ANDROIDCONFIGURATION is not added to array the above answer is not full. However the comment below completes it. If anybody gets confused on how to use it : here is the code snippet: https://gist.github.com/sauravpradhan /8c012169b37556d7b87c731390264cae Also to know you are controlling the correct volume. Use this.setVolumeControlStream(AudioManager.STREAM_VOICE_CALL) and see the selected stream volume getting affected. – Saurav Aug 04 '16 at 11:26