Using audioSessionId value to instantiate AudioFx class? - android

I have read the Android APIs and tried searching over the internet about declaring a custom audioSessionId and then using that audioSessionId to initialize an AudioFx class and assign my MediaPlayer or AudioTrack the hardcoded audioSessionId.
This method would allow me to create an AudioFx first and later attach a new MediaPlayer or AudioTrack to this audioSessionId.
I'm currently able to use this method on Android 2.3.6 but on Android 4.x I'm running into issues with errors that initialization fails or on other ICS/JellyBean devices this error is silent but calling a function leads to exceptions.
Samsung Galaxy S II [Android 4.0.3]: [Issue no longer happens with Android 4.0.4]
E/AudioEffect(13250): set(): AudioFlinger could not create effect, status: -38
E/AudioEffects-JNI(13250): AudioEffect initCheck failed -5
E/AudioEffect-JAVA(13250): Error code -5 when initializing AudioEffect.
W/WrapEqualizer(13250): createEqualizer() -> Effect library not loaded
Motorola Xoom [Android 4.1.2]
Fails it seems silently after the constructor. Then calling on getProperties() it crashes.
java.lang.RuntimeException: AudioEffect: set/get parameter error
at android.media.audiofx.AudioEffect.checkStatus(AudioEffect.java:1247)
at android.media.audiofx.Equalizer.getProperties(Equalizer.java:532)
Nexus 4 [Android 4.2.1]
Using audioSessionId=0 everything works fine but using any other number the device will report the following silent error every time I try to change the preset, band level, bass boost to ON or Virtualizer to ON. The effect ID reported is different depending on the FX I'm trying to modify.
W/AudioPolicyManagerBase(165): unregisterEffect() unknown effect ID 1381
Update 08/11/12:
I'm able to use audioSessionId as 0. I know it's deprecated but it works using the permission. <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> Should I be using the AudioFx with the audio session id 0?

You should look at: this
Apparently it is an unsolved issue came up in ICS, and probably wasn't solved either in JB.

Should I be using the AudioFx with the audio session id 0?
It will probably work in some cases, but don't count on it to continue to do so on future Android versions. You'll already be compromising interoperability between your app and other apps on Jellybean. Just take a look at what the AudioFlinger does when an effect is enabled:
// suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
// another session. This gives the priority to well behaved effect control panels
// and applications not using global effects.
// Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
// global effects
if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) {
setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
}

i know this issue
if somebody want to try
do this
Equalizer eq=null;
.
.
.
.
.
//in any function before initialization do this
if(eq!=null)
eq.release();
eq=new Equalizer(0, audiosessionid);
try it once

Other than session 0 which is the "deprecated global session", my understanding of the AudioFlinger code shows that sessions are only created for classes which actually do audio IO, that is, AudioRecord, AudioTrack, MediaPlayer etc. You should create these classes, and then get their session ID, and then attach the effect.
Any other value you supply for session ID will correspond to an audio session that does not exist, and so will fail.

Related

Android Visualizer won't pick up audio from Spotify streaming music

I'm making an Android audio visualizer app for streaming Spotify music. I'm using Spotify's android-streaming-sdk which creates a local service (no need to have the Spotify app installed) and it plays music from within the app. I'm having a difficult time getting Androids Visualizer library to pick up any audio from Spotify (it works fine if I use a local .mp3 file).
//Start playing spotify track
player.playUri(MainActivity.operationCallback,
VisualizerModel.getInstance().getTrackURI(), 0, 0);
.... other code ....
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
int audioSessionId = mediaPlayer.getAudioSessionId();
visualizer = new Visualizer(audioSessionId);
visualizer.setCaptureSize(audioSampleSize);
visualizer.setDataCaptureListener(this, Visualizer.getMaxCaptureRate(), true, true);
visualizer.setEnabled(true);
If I run this, everything compiles and runs okay, but the visualizer does not pick up any audio. This makes me thing that the audio sessionId is not the same one as Spotify is playing on. However, if I switch the audioSessionId to 0 (which should listen to all the audio sessions mixed from what I understand) it crashes with this error:
E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
According to their documentation that error means: Operation failed due to bad object initialization.
I'm targetting API 24 (Android 7.0).
Please let me know if you have suggestions or an alternative to using Android's Visualizer library. I feel like my main problem is that I'm not sure how to find the audioSessionId that Spotify is playing on, and because Spotify's android-streaming-sdk is in beta (and not mentioned on their website) there is virtually no documentation on it from what I can see on Github.
Thank you for taking the time to read my issue!
I was facing the same error. Adding "android.permission.RECORD_AUDIO" to manifest and requesting for runtime permissions fixed the issue.
to protect privacy of certain audio data (e.g voice mail) the use of the visualizer requires the permission android.permission.RECORD_AUDIO
Also
Creating a Visualizer on the output mix (audio session 0) requires permission Manifest.permission.MODIFY_AUDIO_SETTINGS
Android docs for Visualizer class

Retrieving the application name that has audio focus change

I can't seem to find anything related to finding out what application got audio focus. I can correctly determine from my application what type of focus change it was, but not from any other application. Is there any way to determine what application received focus?
"What am I wanting to do?"
I have managed to record internal sound whether it be music or voice. If I am currently recording audio no matter the source, I want to determine what application took the focus over to determine what my application need's to do next.
Currently I am using the AudioManager.OnAudioFocusChangeListener for my application to stop recording internal sounds once the focus changes, but I want the application's name that gained the focus.
Short Answer: There's no good solution... and Android probably intended it this way.
Explanation:
Looking at the source code, AudioManager has no API's(even hidden APIs) for checking who has Audio Focus. AudioManager wraps calls to AudioService which holds onto the real audio state. The API that AudioService exposes through it's Stub when AudioManager binds to it also does not have an API for querying current Audio Focus. Thus, even through reflection / system level permissions you won't be able get the information you want.
If you're curious how the focus changes are kept track of, you can look at MediaFocusControl whose instance is a member variable of AudioService here.
Untested Hacky Heuristic:
You might be able to get some useful information by looking at UsageStats timestamps. Then once you have apps that were used within say ~500ms of you losing AudioFocus you can cross-check them against apps with Audio Permissions. You can follow this post to get permissions for any installed app.
This is clearly a heuristic and could require some tuning. It also requires the user to grant your app permissions to get access to the usage stats. Mileage may vary.
Looking at the MediaContorller class (new in lollipop, available in comparability library for older versions).
There are these two methods that look interesting:
https://developer.android.com/reference/android/media/session/MediaController.html#getPackageName()
https://developer.android.com/reference/android/media/session/MediaController.html#getSessionActivity()
getPackageName supposedly returns the current sessions package name:
http://androidxref.com/5.1.1_r6/xref/frameworks/base/media/java/android/media/session/MediaController.java#397
getSessionActivity gives you a PendingIntent with an activity to start (if one is supplied), where you could get the package as well.
Used together with your audio listener and a broadcast receiver for phone state to detect if the phone is currently ringing you might be able to use this in order to get a more fine grained detection than you currently have. As Trevor Carothers pointed out above, there is no way to get the general app with audio focus.
You can use dumpsys audio to find who are using audio focus. And, you can also look into the results of dumpsys media_session.
And, if you want to find who're playing music, you can choose dumpsys media.audio_flinger. For myself, I switch to this command.

AUDIO_OUTPUT_FLAG_FAST denied by client

I'm getting this AudioTrack warning
AUDIO_OUTPUT_FLAG_FAST denied by client
in my Android app for a button I'm subclassing. I can hear a click when tapping on the button so is this anything to worry about?
Note: I'm not getting the mismatching sample rate message as in this question: AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client due to mismatching sample rate
Most likely, the tap sound got a AUDIO_OUTPUT_FLAG_FAST in order to use low-latency playback if possible, but the AudioTrack class considered the track settings to be incompatible with the low-latency audio output, so the flag got removed and the track got treated as if the flag hadn't been set to begin with. So I wouldn't consider this to be something to worry about.
As for the reason why the flag got denied; I'd still say that the most probable reason is a sample rate mismatch. The log in the question you linked to appears to have been added in this commit to the AOSP. But if we look at the master branch of the code base used on many Qualcomm-based devices we see that it still has the "AUDIO_OUTPUT_FLAG_FAST denied by client" log in the case were there was a sample rate mismatch. Which logs you get depends on the exact implemetation running on your device (i.e. which device and Android version you're running).
Try changing the sample rate at
"frameworks/base/
frameworks/av/
hardware/libhardware" locations.
default sample rate is 44100 try setting the sample rate you want (your audio files have).
it will work.
cheers.
I had the same problem, my problem was caused because I forgotten type activity define in manifest file. such as : activity android:name=".NFCReaderActivity"

Detect Microphone State Change on Android to detect audio recording

I was trying to make an Android APP whose main function is to detect
if other android APPs are recording voice using Microphone. So far, I learned
that getRecordingState() from AudioRecord class can be used to get the state
of whether microphone is recording or not... but I need something like a broadcast
so I can catch the intent while the state of microphone starts to record voice...
any idea ? Thanks in advance!!
getRecordingState() returns the state for the particular AudioRecord instance that you call the method on. It doesn't give you some global state for all recorders.
There's currently no API available for applications to check globally whether there's ongoing recording from the microphone. The AudioFlinger has that information (though not about which specific application that is doing the recording), but the only way for you to get hold of it would be to modify Android itself and run your own custom Android version.
Try AudioManager.isMicrophoneMute()
http://developer.android.com/reference/android/media/AudioManager.html#isMicrophoneMute()

Android disturbance in ongoing call

I have create the aidl file ITelephony.aidl for ending the call. Now before the call should be ended, I need to generate the voice in ongoing call, so that the listener assumes that there is a problem in next work.
I have tried to solve this by myself in three ways:
Control the audio stream volume
audioManager.setStreamMute(AudioManager.STREAM_VOICE_CALL, isMute);
int index = rand.nextInt(5);
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,index,0);
Another way is using the setMute in telephonyService
http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html
but working in in 2.3
I have also tried to implement using media player for start playing when the call is going on but no luck.
So if there is any other idea then please let me know.
It seems you have the same problem I had a while ago: With Android 2.3 aka Gingerbread it's no longer possible to use the ITelephony interface because the MODIFY_PHONE_STATE permission has been marked system only.
Details may be found here.

Categories

Resources