Name is required.
Email address is required.
Invalid email address
Answer is required.
Exceeding max length of 5KB

Android - Play audio in background service (startForeground)


Hello,
I've tried JW player and need to just check below-mentioned things before proceeding to use in product and before going to license and finance matters.

How can we Play an Audio track in background service(If user exit from application)?

I tried something like (Creating an instance of JWPlayerView in my activity and bound service and given created instance of the JWPlayerView from Activity/Fragment to
Service where it should keep playing if user exit from the application.

But if service Stopped from Android System and somehow JWPlayerView instance is stopped or crashed then how can I recreate the instance of the JWPlayerView if user exit from the application?

if user exit from an application(onPause & service is bounded) than I am setting setBackgroundAudio(true) to JWPlayerView instance.

Is there any other class or player which can play without a view and use in background service.?

Or is there any sample which handles above-mentioned cases.?

Please provide any solution or suggestion on above.

Thanks.

8 Community Answers

George

JW Player Support Agent  
0 rated :

Hi,

You’ll want a ForegroundService:
http://qa.jwplayer.com.s3.amazonaws.com/~george/BackgroundAudioService.zip

t...

User  
0 rated :

Hello George,

Thanks for the response.From the given link at least I have some hope to play audio in background service too. But still some of the issue I am facing from the sample source which you gave. Here I have given details related to the issue and what I tried from my side too.

By using sample code from the given link I am getting crash from JWPlayerView class and here I've given link for the crash log
https://gist.github.com/adneerav/853af0cc717ead6b16eec443c7eb9145

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.longtailvideo.jwplayer.c.g.a(boolean)' on a null object reference


What else I tried... ?

Removed mPlayerView.onPause(); from onPause method of activity where its giving NPE. and also make some changes to bind and start service.

VideoAcitivity with changes done from my side
https://gist.github.com/adneerav/dd822d42cf5e5164259e3b117a082646

Foreground service with changes done from my side
https://gist.github.com/adneerav/f76bac3d8f6904f59938b23f7a7a58fc

Here application is not crashing & Service starting in foreground also,but after playing audio in service and if reopen the app after that Player's pause & play method is not functioning from service.

Steps to reproduce Play and pause method not responding.
1)Play Audio from activity
2)Exit from application
3)Play & pause from notification (in this it will work)
4)Open application from notification tap or from launcher icon
5)Repeat Step 4 here(Play button from notification not responding).


Please provide details on above and guide on what I am missing or doing wrong. So I can proceed with the next process.

Thanks

George

JW Player Support Agent  
0 rated :

Are you updating the reference to the JWPlayerView in the service when you return to the activity? Perhaps that’s why it’s not working?

t...

User  
0 rated :

Hello George,

Thanks for the response.

"Are you updating the reference to the JWPlayerView in the service when you return to the activity? "


JWPlayerView instance will be created the first time and pass it to service and from the next time if the application is open I am just using the same instance of JWPlayerView which is given to the Service.

If you can check gist link for VideoActivity & ForegroundService classes.I am using the same reference from service if it's created(If not than JWPlayerView instance will be created).

From the ForegroundService below method, to get same JWPlayer Instance which is passed from the VideoActivity.

public JWPlayerView getPlayerView() {
return mPlayerView;
}

In "onServiceConnected" method of VideoAcitivity, I've written to get the same reference of JWPlayerView.

VideoAcitivity
https://gist.github.com/adneerav/dd822d42cf5e5164259e3b117a082646

Foreground service
https://gist.github.com/adneerav/f76bac3d8f6904f59938b23f7a7a58fc

So do I need to create JWPlayerView instance Runtime rather than adding from XML layout? If yes then please provide any sample tutorial or link from where I can get an idea about this reference related issue.

Please provide more details on this.

Thanks,

t...

User  
0 rated :

Hello George,

Adding more details to this thread.

I've created JWPlayer instance runtime with PlayerConfig and required setup and removed JWPlayerView from the XML. Also, make changes to add & remove jwplayer instance to the container(contentview).

Seems things are working as expected but getting one broadcast receiver leak exception from jwplayer SDK's class which is a final class here I've given the crash log details link.

Crash Log: https://gist.github.com/adneerav/a646533f3abf493c9ad9df653c425151

VideoActivity with JWPlayer create Runtime instance: https://gist.github.com/adneerav/50ce0d367a1c6deb60b5a377667c7a9e

And what could be the threat or risk if I create Player instance run time?

Can you please provide more details on this now. ?

George

JW Player Support Agent  
0 rated :

Hi,

If you have a paid account please submit a support ticket from your dashboard. Also, I was unable to reproduce the crash in this project:
http://qa.jwplayer.com.s3.amazonaws.com/~george/BackgroundAudioService.zip

The project above does not have any issues continuing playback from the notification tray.

t...

User  
0 rated :

Hello George,

Thanks for the response.

I don't have Paid account right now. But if the JW player supporting features which are required (& needy) for our Product then we can convert Free to Paid account.And just to check feasibility before converting into PAID I am requesting for your support.

I am stuck with one the main feature like Playing audio in background service.This is one the main feature of Product and of course for any Audio Player application.

So if JW team can provide little bit more support before going in the financial process and paid account process than it would be really helpful.

Now as you mentioned, "The project above does not have any issues continuing playback from the notification tray."

I have just imported the project in Studio and found below result

1)After Starting song if I exit from the application(Close the application by BackPress or in any other way) > Notification GONE from the notification tray and Stopped playing. (Below I've given Recorded Video URL, Dropbox public URL)

2)For me, its giving crash and I have given the link for the crash log

---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Please check the video to reproduce crash log.
From recorded video its visible that notification is not maintained and service is not in the foreground too.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample source code download URL: http://qa.jwplayer.com.s3.amazonaws.com/~george/BackgroundAudioService.zip

Video URL to generated crash: https://goo.gl/osrSuo

Crash Log URL: https://goo.gl/wiBwxT

Tested Devices and Android versions Details

Nexus 5X: 6.0.0
Moto G: 5.1.0
Samsung S3: 4.3


Request to give support on this as I need to confirm functionalities and player feasibilities before converting to PAID account.

Thanks.

George

JW Player Support Agent  
0 rated :

That’s interesting, that wasn’t the case when I was testing it, and now it is. The OS used to call onStop only when exiting the activity, now however it’s calling onDestroy.

Overall, this is a development question, this is not an issue with the SDK. You will have to read through the Android docs and see why an Activity marked as a singleTask will call onDestroy etc.

JW Player support does not offer custom code solutions, I just happened to have a demo for background audio.

This question has received the maximum number of answers.