How to play audio by a specific audio device in Android? - android

I've looked up around for a way to achieve mainly 3 different things with Android:
How to getting listed all the audio output and input devices?
I've found out that there's a method added since API 23 on AudioManager instances according to Android documentation which does this, but there's no documentation about a solution for previous versions. This question remains unanswered here at StackOverflow and several places online.
How to pick up a specific audio output device to stream audio?
The purpose I want to acomplish with this is to select one of the available output devices and stream out the audio by it. Is there a way to do so?
How to record by a specific input device?
The same that before, specific device usage, but in the other way around, I mean, to select one of the available microphones and use it to record audio.
Please help me with some guidence, source code or idea, all of them will be very welcome.

Related

Android recording an apps sound output

Hello guys I have a question
I have to admit before I ask my question I never used Android Sdk before but I have coded java for couple of years.
I have a fm radio app.It's an internet radio and I want to record it's output. Is it possible to use an external app to record some other app's output? And if yes, It also has some pre recorded shows which you can listen within the app. They do not get saved into my device when I listen however is it possible to download those shows? Like finding source of the audio and downloading it using my external app.
I'm pretty sure that the recorded shows are downloaded from the internet. I know some audio grabbers as browser extensions in Pc. So I'm asking, if such thing is possible in Android as well.
See below:
https://stackoverflow.com/a/25741006/850347
Seems to be currently there is no way to achieve this. I have read this article and it suggests to recompile the Android source code with some changes.
Or, you can use visualizer.
https://stackoverflow.com/a/25816052/850347
The closest API available to you for these purposes is Visualizer. Which only captures "partial and low quality audio content".

Why it is not possible to play an audio file on a voice call in android

This question might seem to be a repetition of the questions such as following:
How to play an audio file on a voice call in android
Background Audio for a Call in Progress - Possible?
The answers of these questions suggests that it is not possible to play a pre-recorded audio on a voice call in android. I want to know why it is not possible? What is the limitation (hardware/software)? Is it really a limitation or done purposely? Can we alter the source code of android to make it possible?
I think this is a limitation, imposed for security reasons and restricted at the OS level.
Let's analyze the security threat, first of all. If you were able to play custom audio files to the callee, a whole world of cons opens up: you could trick customer supports, you could pretend to be someone else, you could give unauthorized purchase confirmations, and so on. For this reason, neither Android nor iOS allows this functionality.
On Android, you won't be able to do so in a programmatic way, simply because the current APIs won't allow you to do so. It is stated in the official documentation as well, as pointed out here. If you dig into the source code, you can probably enable this feature by accessing the microphone output during a phone call, but that would require running your custom version of Android. A good starting point would be the AudioTrack source, available here.
EDIT: a good example of an audio mod involves enabling the Nexus 5 earpiece as a second loudspeaker (requires root). Can be found here.
After a thorough research, what I have come to know is that there are more than one limitations/hurdles to make it possible. These limitations/hurdles are at three different levels.
First limitation is at API level, because there is no high-level API to play sound files in the conversation audio during a call as mentioned in Android official documentation.
Second limitation is at Radio Interface Layer (RIL). RIL passes on complete control of the call to Radio Daemon (rild) of the Linux library which then further passes the control to the vendor RIL. That means we cannot manipulate voice call in android source code.
Even if we are able to remove these two limitations, we may still not be able to play audio file to an ongoing voice call. Because there is a third limitation. Every vendor has their own library of RIL that communicates with Radio Daemon (rild). This requires that vendor RIL to be open source which is not actually. Hardware vendors do not usually make their device drivers code available.
Detail discussion on this topic is present at this link.
This is software related due to the prioritization of audio routing in Android.
Take a look into the CallManager where you can dig into the method setAudioMode(). After the audio mode was set to MODE_IN_COMMUNICATION the following code is called
audioManager.requestAudioFocusForCall(AudioManager.STREAM_VOICE_CALL,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
From this point on the telephony service has the highest priority and won't let any other audio play in parallel.
Note: You can play back the audio data only to the standard output device. Currently, that is the mobile device speaker or a Bluetooth headset. You cannot play sound files in the conversation audio during a call.
See official link
http://developer.android.com/guide/topics/media/mediaplayer.html
By implementing the AudioManager.OnAudioFocusChangeListener you can get the state of the audiomanager. so by this if any music is playing in the background you can get the AudioManager states(playing and pausing is completely in developer hands) similarly......
Some of the native music players in android device where handling this, they restrict the music when call is in TelephonyManager.EXTRA_STATE_OFFHOOK.so this scenario is also completely in developer hand (whether to handle or not) if he is not handling both will play parallel y

Audio Matching (Audio Fingerprinting)

I'm writing an android app that lets user record his voice through microphone & save it in storage & link it to a specific content (like a Contact). Later, user call that voice again & the app should compare it with saved audio files & find the one that matches the voice.
I searched a lot & found some libraries that do this online, like EchoPrint that generates fingerprint from recorded audio & sends it to opensource server & returns the result. But I need to do this offline.
Has anybody know such library?
If you are aiming to compare an old recording of a user with a new call as it comes in, audio fingerprinting solutions like Dejavu in Python on a server or Echoprint in C++ won't help you. They are for doing recognition and retrieval on recorded audio segments plus noise.
They cannot deal with the variabilites in human voice. See an explanation here.
If that's the case, what you are referring to is speaker recognition, which is much harder and involves quite a bit of machine learning. It would be tough to do this for a large corpus of users (especially offline on a phone), but for determining between a couple users, it might be doable.
Below is a good Library. Which is Easy to use. But you need to convert your Audio Files to Wave Format prior to this.
https://code.google.com/p/musicg/

Is it possible to record from the external mic when recording video in Android?

I've been asking around in developer circles about this, but so far no one has been able to confirm if it is possible. The default behavior for recording video in Android is to not use the external mic. I'd like to create an app that uses an external mic if it is available, but it seems like this might be tricky for some reason. Anyone have insight into this?
It seems like it would just be a matter of selecting it at this point in the recording setup:
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
but it seems like there may be some oddness in doing that.
Thanks,
Jon
Default behavior in Android 12 (and I understand starting at 8 or 9) is that external microphones become the primary and default source for input.
It probably depends on the app, but at least video in the default camera app takes my external mic as default.
Take a look at the documentation here (look in the section titled "Performing Audio Capture") it covers setting up the audio capture.

Audio Framework: stream, strategies, devices

I am trying to get a better understanding of android audio framework (froyo onwards in particular).
I've almost got the distinction into streams. The AudioFlinger maps each stream into a "strategy" and then routes a strategy to the proper "device" (BT, Headset, Speaker..)
However I still struggle to understand:
1) How multiple apps play onto the same stream? can they just 'play' or do they have to handle the focus (request, abandon and listen for changes)?
2) When multiple streams are playing...what is the priority of one stream respect to another? surely the in-call stream has top priority but are there any "rules" to understand how things really work.
If anyone with more confidence on this matter has got any doc/resource to study.. it would be great.
Thanks!
Marco
The answer to this question
Concurrent Sound on Android Device
may help you.
Also, here
http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
is written how to initialize the SoundManager with the number of concurrent streams you want to play.
If you need to understand the default behavoiur you can see the Cplusplus code at
/hardware/libhardware_legacy/audio/AudioPolicyManagerBase.cpp
However each vendor can make changes and ship a different flavour

Categories

Resources