Android, NDK, audio routing, headset boost

Register Usage

  • Android headset sound recording

Environment

  • Samsung Galaxy 5
  • Android 5.0
  • Windows operating system with USB sound recorder

Implementation

  • Connect the headset output of your Android device to the Windows Line Line input
  • Record everything sent from your Android device.

Description of the problem

  • When you connect the audio jack while playing audio on an Android device, the audio output of the device is routed through the headset jack, and recording works correctly
  • As soon as sound playback (for example, a song) has stopped on the Android device, the next time it starts playing sound, the device’s speaker will be used, and not the headset jack (used for recording).

Attempt to resolve the problem

  • C ++ native command-line tool running under a shell account (cannot use Java for this)
  • dynamically load "libmedia.so" and name, for example. android :: AudioSystem :: setForceUse (AUDIO_POLICY_FORCE_FOR_MEDIA, AUDIO_POLICY_FORCE_HEADPHONES)
  • logcat reports:
    • 02-26 10: 39: 50.418 289 1707 ServiceManager Resolution error: android.permission.MODIFY_AUDIO_SETTINGS from uid = 2000 pid = 19577
    • 02-26 10: 39: 50.418 289 1707 The request requires android.permission.MODIFY_AUDIO_SETTINGS

Questions

  • Bearing in mind that the solution must be executed on the wrong device
    • Is there a way to grant permission "android.permission.MODIFY_AUDIO_SETTINGS" to the account "shell"?
    • Is there any other way to force the use of the system width (and not the specific concrete) sound through the headset?

code binding

HRESULT forceAudioThroughHeadPhones() { typedef int(*SET_FORCE_USE)(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); struct sigaction sa, osa; sa.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO; sa.sa_sigaction = [](int signo, siginfo_t* psi, void *data)->void { ucontext_t *uc = (ucontext_t *)data; }; sigaction(SIGILL, &sa, &osa); void* hMod = dlopen("libmedia.so", RTLD_LAZY); sigaction(SIGILL, &osa, 0); if (0 == hMod) return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); SET_FORCE_USE setForceUse = (SET_FORCE_USE)dlsym(hMod, "_ZN7android11AudioSystem11setForceUseE24audio_policy_force_use_t25audio_policy_forced_cfg_t"); if (0 == setForceUse) return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); android::status_t err; if (ERROR_SUCCESS != (err = setForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA, AUDIO_POLICY_FORCE_HEADPHONES))) return E_FAIL; if (ERROR_SUCCESS != (err = setForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM, AUDIO_POLICY_FORCE_HEADPHONES))) return E_FAIL; return S_OK; } 
  • 'setForceUse' returns -1, errno is equal to 'Zero'.

UPDATE:

I get the same permission logcat errors when using the adb shell service call ...:

 10|shell@klte:/ $ service call media.audio_policy 1 i32 2 Result: Parcel(ffffffff '....') 
0
c ++ android android-ndk android-source android-audiorecord
Feb 26 '15 at 8:59
source share

No one has answered this question yet.

See similar questions:

75
Android AudioRecord forces another thread to use MIC audio source

or similar:

221
Android: force edittext to remove focus?
147
Android studio, gradle and NDK
44
Endianness Android NDK
38
Routing audio to a Bluetooth headset (not A2DP) on Android
35
Android NDK: getting the return line
3
Access Equipment with Android NDK
2
Android AudioRecord uses a built-in microphone instead of a headset microphone
2
Audio Recording Using Android NDK
0
Capturing audio via a Bluetooth headset paired with an Android device
0
Running Strace on a real Android device gives SIGSEGV



All Articles