I have a music player application and untill now, it was working smoothly.
So, i learnt about Android Focus
from android documentation and i thought i would try this on my app for learning purposes.
App: minimum api level :16
; Maximum:26
;
I basically implemented the AudioManager.OnAudioFocusChangeListener
in my service class.
In it i included this code:
if(Build.VERSION.SDK_INT>=26){
mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(mAudioAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(this)
.build();
int res = mAudioManager.requestAudioFocus(mAudioFocusRequest);
synchronized(mFocusLock) {
if (res == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
mPlaybackNowAuthorized = false;
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlaybackNowAuthorized = true;
playSong();
} else if (res == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
mPlaybackDelayed = true;
mPlaybackNowAuthorized = false;
}
}
} else if(Build.VERSION.SDK_INT<26){
int result = 0;
if (mAudioManager != null) {
result = mAudioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
}
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlaybackNowAuthorized = true;
playSong();
}
}
My onAudioFocusChange(int focusChange)
looks something like this:
if(Build.VERSION.SDK_INT<26){
//<26 code
}else {
//26 and above api level
}
I tried implementing audiofocus
in my application and this is how i did it. The issue is when i try to run my app now , it gives me this error
unable to create a service
What's the problem with it? i think i read the documentation clearly.
Am i missing out something?
Update:
Removing the below code does make it work
else if(Build.VERSION.SDK_INT<26){
int result = 0;
if (mAudioManager != null) {
result = mAudioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
}
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlaybackNowAuthorized = true;
playSong();
}
Stacktrace:
12-18 16:27:47.740 30183-30183/com.example.tilak.imusicplay E/AndroidRuntime:
12-18 16:27:47.740 30183-30183/com.example.tilak.imusicplay E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tilak.imusicplay, PID: 30183
java.lang.RuntimeException: Unable to create service com.example.tilak.imusicplay.MusicService: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2920)
at android.app.ActivityThread.access$2000(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1458)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.tilak.imusicplay.MusicService.playSong(MusicService.java:239)
at com.example.tilak.imusicplay.MusicService.initMusicPlayer(MusicService.java:115)
at com.example.tilak.imusicplay.MusicService.onCreate(MusicService.java:66)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2910)
at android.app.ActivityThread.access$2000(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1458)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
12-18 16:27:47.742 30183-30183/com.example.tilak.imusicplay E/MQSEventManagerDelegate: failed to get MQSService.