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 '....')
c ++ android android-ndk android-source android-audiorecord
NadavRub Feb 26 '15 at 8:59 2015-02-26 08:59
source share