Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Android background music service



Permlink Replies: 15 - Last Post: Sep 28, 2017 3:21 AM Last Post By: Dave Nottage
Guest
Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 9:03 AM
I am developing an Fm streaming app in android 7 with Berlin 10.
I want to play background music, and I want to use service for that with a MediaPlayer1 component but the application crash.

Anybody can help me? Thank you

Giorgio


uses
Androidapi.JNI.App;

function TDM.AndroidServiceStartCommand(const Sender: TObject;
const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
Beacon1.Enabled := True;
MediaPlayer1.FileName:='http://icecast.unitedradio.it/Radio105.mp3';
MediaPlayer1.play;
Result := TJService.JavaClass.START_STICKY;
end;

Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 3:03 PM   in response to: Guest in response to: Guest
giorgio santi wrote:

I am developing an Fm streaming app in android 7 with Berlin 10.
I want to play background music, and I want to use service for that with a MediaPlayer1 component but the application
crash.

Anybody can help me? Thank you

At present, you cannot use anything that depends on FMX units in a service.

I suggest examining the code for TAndroidMedia in the FMX.Media.Android unit, as it will show how you can work with
JMediaPlayer in order to achieve the same result yourself.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 4:48 PM   in response to: Dave Nottage in response to: Dave Nottage
Dave Nottage wrote:

At present, you cannot use anything that depends on FMX units in a
service.

That is not true. Why would you say that?

--
Remy Lebeau (TeamB)
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 24, 2017 6:24 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

That is not true.

Yes, it is true. Here's the report: https://quality.embarcadero.com/browse/RSP-17857

Virtually all FMX units depend on FMX.Types, including FMX.Media, which has TMediaPlayer

Why would you say that?

See above.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Guest
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 29, 2017 7:34 AM   in response to: Guest in response to: Guest
Thanks for your help.
I have remove from service all FMX unit and for player i used Androidapi.Jni.Media with this sample code:

Var
FPlayer : JMediaPlayer;
begin
FPlayer := TJMediaPlayer.Create;
FPlayer.setAudioStreamType(TJAudioManager.JavaClass.STREAM_MUSIC);
FPlayer.isLooping;
FPlayer.reset;
FPlayer.setDataSource(StringToJString('http://icecast.unitedradio.it/Radio105.mp3'));
FPlayer.prepare();
FPlayer.start();

The service work well but after 5 minuts the service goes into sleep mode and the music is not listening anymore.

I tried inserting WakeLock and TJWifiManager.JavaClass.WIFI_MODE_FULL to prevent media player sleep mode with no results.

I've seen that FPlayer has this feature:

FPlayer.setWakeMode()

Maybe it might solve the problem but I didn't understand how it is used.

Giorgio

Edited by: giorgio santi on Aug 29, 2017 7:37 AM

Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Android background music service [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 29, 2017 7:49 AM   in response to: Guest in response to: Guest
giorgio santi wrote:

I've seen that FPlayer has this feature:

FPlayer.setWakeMode()

Maybe it might solve the problem but I didn't understand how it is used.

You will likely need to call it like this:

  uses
    Androidapi.Helpers, Androidapi.JNI.Os;
 
//
 
  FPlayer.setWakeMode(TAndroidHelper.Context, TJPowerManager.JavaClass.PARTIAL_WAKE_LOCK);


--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Guest
Re: Android background music service [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 12:04 AM   in response to: Dave Nottage in response to: Dave Nottage
Thank you Dave,

sorry but I had already tried with :

FPlayer.setWakeMode(TAndroidHelper.Context, TJPowerManager.JavaClass.PARTIAL_WAKE_LOCK);

But after 5 minuts the sound continues to go in sleep mode.



You will likely need to call it like this:

  uses
    Androidapi.Helpers, Androidapi.JNI.Os;
 
//
 
  FPlayer.setWakeMode(TAndroidHelper.Context, TJPowerManager.JavaClass.PARTIAL_WAKE_LOCK);

Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Android background music service [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 12:26 AM   in response to: Guest in response to: Guest
giorgio santi wrote:

sorry but I had already tried with :

FPlayer.setWakeMode(TAndroidHelper.Context, TJPowerManager.JavaClass.PARTIAL_WAKE_LOCK);

But after 5 minuts the sound continues to go in sleep mode.

Have you enabled the Wake Lock permission in Project Options?

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Guest
Re: Android background music service [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 3:37 AM   in response to: Dave Nottage in response to: Dave Nottage
Have you enabled the Wake Lock permission in Project Options?

Yes i have enabled Wake Lock permission in Project Options.

I'm confused....

Is it possible that the problem may come from the Samsung Galaxy S6?
Guest
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 1, 2017 12:47 PM   in response to: Guest in response to: Guest
update....
i found this i think it might be the solution of my problem....

From Android API 23 onward, Google has introduced Doze mode and App Standby in order to conserve battery power.
When a device enters doze mode or an app enters App Standby, all tasks done by an app is deferred.
We had an issue with an Alarm which was not firing because of the same.
If you read upon docs, you will find that using a Wake Lock is not gonna help either.

Doze restrictions The following restrictions apply to your apps while in Doze:
Network access is suspended.
The system ignores wake locks.
Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.
If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.
The system does not perform Wi-Fi scans.
The system does not allow sync adapters to run. The system does not allow JobScheduler to run

From: https://stackoverflow.com/questions/42057958/how-to-keep-application-awake-in-background

In my case I think run a service and in priority to foreground

I need to understand how to run the service in foreground mode.

Edited by: giorgio santi on Sep 1, 2017 12:48 PM

Guest
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 5, 2017 7:48 AM   in response to: Guest in response to: Guest
Hi,
i have try to setting my app for ingnore battery optmizzation with this code
but not work.....

packageName:=Jstringtostring(TAndroidHelper.Context.getPackageName());

Intent := TJIntent.Create;
Intent.setAction(TAndroidHelper.StringToJString('ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS'));
Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('package:'+packageName)));
TAndroidHelper.Activity.startActivity(Intent);

any idea....

Thank you

Giorgio

Markus Humm

Posts: 5,113
Registered: 11/9/03
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 5, 2017 11:36 AM   in response to: Guest in response to: Guest
Am 05.09.2017 um 16:48 schrieb giorgio santi:
Hi,
i have try to setting my app for ingnore battery optmizzation with this code
but not work.....

packageName:=Jstringtostring(TAndroidHelper.Context.getPackageName());

Intent := TJIntent.Create;
Intent.setAction(TAndroidHelper.StringToJString('ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS'));
Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('package:'+packageName)));
TAndroidHelper.Activity.startActivity(Intent);

any idea....


What please does "not work" mean?
Any failure code or is the battery optimization still kicking in?
Did you find any log messages about it in the logcat log? (start
monitor.bat from SDK installation to see it)

Greetings

Markus
Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 5, 2017 3:04 PM   in response to: Guest in response to: Guest
giorgio santi wrote:

i have try to setting my app for ingnore battery optmizzation with this code
but not work.....

packageName:=Jstringtostring(TAndroidHelper.Context.getPackageName());

Intent := TJIntent.Create;
Intent.setAction(TAndroidHelper.StringToJString('ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS'));
Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('package:'+packageName)));
TAndroidHelper.Activity.startActivity(Intent);


You should really be doing this:

Intent.setAction(TJSettings.JavaClass.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Guest
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 6, 2017 3:12 AM   in response to: Dave Nottage in response to: Dave Nottage
Thanks Dave ...
I'm lost in the labyrinth of delphi-android ...
Sorry for my questions but
i would like to understand how to run a background app without sleep mode restriction of android 7 or higher.
I haven't found on the net useful examples of how to do with delphi.
I think it's strange because it's important and useful to many Delphi users.

I changed my code in this mode:

var
intent: JIntent;
packageName:string;
begin
packageName:=Jstringtostring(TAndroidHelper.Context.getPackageName());
Intent := TJIntent.Create;
//Intent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString('com.embarcadero.services.cassaservice'));
Intent.setAction(TJSettings.JavaClass.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('package:'+packageName)));
SharedActivity.startActivity(Intent);


I get an error android.content.ActivityNotFoundException:
Unable to find explicit activity class { com.embarcadero.RadioTest/com.embarcadero.service.cassaservice };
have you declared this activity in your AndroidManifest.xml?


Dave Nottage wrote:
giorgio santi wrote:

i have try to setting my app for ingnore battery optmizzation with this code
but not work.....

packageName:=Jstringtostring(TAndroidHelper.Context.getPackageName());

Intent := TJIntent.Create;
Intent.setAction(TAndroidHelper.StringToJString('ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS'));
Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('package:'+packageName)));
TAndroidHelper.Activity.startActivity(Intent);


You should really be doing this:

Intent.setAction(TJSettings.JavaClass.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Guest
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 28, 2017 3:07 AM   in response to: Guest in response to: Guest
Hello everyone,

I resolved this way:

// PowerManager
 
 function GetPowerManager: JPowerManager;
var
PowerServiceNative: JObject;
begin
PowerServiceNative := SharedActivityContext.getSystemService(TJContext.JavaClass.POWER_SERVICE);
if not Assigned(PowerServiceNative) then
raise Exception.Create('Could not locate Power Service');
Result := TJPowerManager.Wrap((PowerServiceNative as ILocalObject).GetObjectID);
if not Assigned(Result) then
    raise Exception.Create('Could not access Power Manager');
end;
 
 
// Procedure for sett
procedure TForm1.ListBoxItem15Click(Sender: TObject);
var
provider:string;
Settings_secure:TJSettings_Secure;
Intent:JIntent;
packageName:string;
pw:JPowerManager;
 
begin
Intent := TJIntent.Create;
 packageName:= JStringtoString(TAndroidHelper.Context.getPackageName());
 pw:=GetPowerManager;
 
 
if pw.isIgnoringBatteryOptimizations(TAndroidHelper.Context.getPackageName)=true   then
 begin
Intent.setAction(TJSettings.javaClass.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
TAndroidHelper.Context.startActivity(Intent);
end
else
begin
Intent.setAction(TJSettings.javaClass.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString('package:'+packageName)));
TAndroidHelper.Context.startActivity(Intent);
end;
 
 memo1.Lines.Add('ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS');
 
 end;
 

Finally remember to add on AndroidManifest.template.xml file under voice <%uses-permission%> this code line:


<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
 

That's all......

I hope will be useful

Thank you

Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: Android background music service
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 28, 2017 3:21 AM   in response to: Guest in response to: Guest
giorgio santi wrote:

That's all......

I hope will be useful

Thank you

Thank you for reporting your findings!

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02