iOS Question Start playing in background

Humberto

Active Member
Licensed User
Longtime User
Until now I can press a button and start playing music and go to background and continue playing.

When the app is in foreground I receive a silent push notification and start playing a music and if the app goes to background continue playing

When the app in in background I receive the silent push notification but the music doesn´t start

Someone can help ?

definition
Code: Copy
    #PlistExtra: <key>UIBackgroundModes</key>
    #PlistExtra: <array>
    #PlistExtra: <string>audio</string>
    #PlistExtra: <string>remote-notification</string>
    #PlistExtra: </array>


code
Code: Copy
Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   Dim xTitulo, xMsg As String
  
   xTitulo = Message.Get("title")
   xMsg  = Message.Get("message")
 
    Log($"Message arrived: ${Message}"$)

    NativeMe.RunMethod("setAudioSession", Null)
    MyPlayer1.Initialize(File.DirAssets, "manera.mp3","MyPlayEnd")
    MyPlayer1.Looping=False
    MyPlayer1.Play
   
'   Msgbox(xMsg, xTitulo )
 
      ch = CompletionHandler
'   CompletionHandler.Complete
End Sub


#If OBJC
@import MediaPlayer;
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
- (void)setAudioSession {
  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  NSError *err = nil;
  BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&err];
  if (success) {
  success = [audioSession setActive:YES error:&err];
  }
  if (!success)
  [NSException raise:@"" format:@"Error setting audio session: %@", err];
}
- (void)register {
   MPRemoteCommandCenter* center = [MPRemoteCommandCenter sharedCommandCenter];
   center.playCommand.enabled = true;
   center.pauseCommand.enabled = true;
   [center.playCommand addTarget:self action:@selector(play)];
   [center.pauseCommand addTarget:self action:@selector(pause)];
}
- (void) play {
   NSLog(@"test");
   [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"play"]];
}
- (void) pause {
   [self.bi raiseEvent:nil event:@"controlevent:"  params:@[@"pause"]];
}

#end if
 
APP start
Code: Copy
Application_Start
The default app has not been configured yet.
Configuring the default app.
Application_Active
(empty string)
Application_Pushtoken
Application_Pushtoken
FCMConnected

App in foreground receiving the push notification and playing the music

Code: Copy
Application_Remotenotification
Message arrived: (read only map) {
    MyKey1 = MyData1;
    MyKey2 = MyData2;
    aps = "(read only map) {\n    \"content-available\" = 1;\n}";
    "gcm.message_id" = "0:1485086268709749%e9301870e9301870";
    message = "Mensagem do GCM 09:58:43";
    title = "Titulo Mensagem";
}


App in background receive the push notification but no music

Code: Copy
Application_Inactive
Application_Background
Application_Remotenotification
Message arrived: (read only map) {
    MyKey1 = MyData1;
    MyKey2 = MyData2;
    aps = "(read only map) {\n    \"content-available\" = 1;\n}";
    "gcm.message_id" = "0:1485086283296603%e9301870e9301870";
    message = "Mensagem do GCM 09:58:58";
    title = "Titulo Mensagem";
}

Function that receive the push
Code: Copy
Private Sub Application_RemoteNotification (Message As Map, CompletionHandler As CompletionHandler)
   Dim xTitulo, xMsg As String

   xTitulo = Message.Get("title")
   xMsg  = Message.Get("message")
 
    Log($"Message arrived: ${Message}"$)

    NativeMe.RunMethod("setAudioSession", Null)
    MyPlayer1.Initialize(File.DirAssets, "manera24.mp3","MyPlayEnd2")
    MyPlayer1.Looping=False
    MyPlayer1.Volume = 1
    MyPlayer1.Play
   

'    Dim ln As Notification  'this is done for release mode to acknologe of the push
'    ln.Initialize(DateTime.Now + 1 * DateTime.TicksPerSecond)
'    ln.AlertBody = "This is a local notification"
'    ln.PlaySound = True
'    ln.Register
 
   
'   Msgbox(xMsg, xTitulo )
 
      ch = CompletionHandler
'   CompletionHandler.Complete
End Sub
 
Upvote 0
Erel

I found this, could you help me implement in the NativeObject ?

http://stackoverflow.com/questions/7619794/play-music-in-the-background-using-avaudioplaye
Code: Copy
I just wanted to add a note to Mehul's answer. This line:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

is actually very important. I used other tutorials to get my AVAudioPlayer up and running. Everything worked fine except my audio would not START if the app was in the background. To clarify, my audio was fine if it was already playing when the app went into the background...but it would not start if the app was already in the background.

Adding the above line of code made it so my audio would start even if the app was in the background.
 
Upvote 0
Hi .

Works, I just add this line and the music start playing even in background.



Code: Copy
- (void)setAudioSession {
  AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  NSError *err = nil;
  BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&err];
  if (success) {
  success = [audioSession setActive:YES error:&err];
  }
  if (!success)
  [NSException raise:@"" format:@"Error setting audio session: %@", err];
  [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}

Thanks
 
Upvote 0
Back
Top