1

I want to implement an autoplay audio feature when silent push receive. I am receiving an audio URL when silent push come.I want to play an audio when app in background via receiving silent push. But my problem is that, I am successfully receiving audio URL via silent push, but I am not able to play audio when app in background mode. I am using "STKAudioPlayer" for playing an audio, which is downloaded from GitHub . I am implementing below code for playing an audio

NSString *finalUrl = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"forUrl"]];
[stkaudioPlayer play:finalUrl];

And below code is for receiving a silent push notification and here I am implementing to playing an Audio.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"BACK GROUND MODE PUSH");
if([userInfo[@"aps"][@"content-available"] intValue]== 1) //it's the silent notification
{


    [self saveInUserDefault];

stkaudioPlayer = [[STKAudioPlayer alloc]init]; //player initialize

    //Save audio url
    NSString *str= [userInfo valueForKey:@"AudioURL"];
    [[NSUserDefaults standardUserDefaults]setObject:str forKey:@"forUrl"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    // for Playing an audio
NSString *finalUrl = [NSString stringWithFormat:@"%@",str];
[stkaudioPlayer play:finalUrl];

    completionHandler(UIBackgroundFetchResultNewData);

  // NSLog(@"I am Here");

    return;
}
else
{

    NSLog(@"User ... %@",userInfo);
        return;
}}

If my implementation is wrong way, so please help me out with this topic.

Abir
  • 31
  • 7

1 Answers1

1

Go to targets -> Capabilities -> Background modes and check have you tunes on background fetch and remote notification ? if not then turn them on.

After that , in your didReceiveRemoteNotification , you have to add one sound file that you want to play and just take a name of sound file from your push data(you need to so that change in push data also) , after that do like this : - (i am using AVAudioPlayer)

Import - AudioToolbox/AudioToolbox.h

@property (strong, nonatomic) AVAudioPlayer *sound1Player;

NSString *beep=[userInfo valueForKey:@"AudioURL"];

NSString *path = [[NSBundle mainBundle] pathForResource:beep ofType:@"mp3"];

NSURL *url=[NSURL fileURLWithPath:path]; sound1Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

[[AVAudioSession sharedInstance] setActive: YES error: nil];

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

sound1Player.delegate=self;

sound1Player.numberOfLoops=0;

[sound1Player prepareToPlay];

[sound1Player play];

(Here AudioURL is the name of your audio file which that you want to play and which is there in your project)

or if you want to just play one sound then no need to take it from push data just take it from your code by giving name

Raj Oriya
  • 78
  • 4
  • "targets -> Capabilities -> background fetch and remote notification" these are ON. you are using 'AVAudioPlayer' and your audio file is in bundle. In my case, audio url comes from notification. So I am not using 'AVAudioPlayer'. Now I am using 'AVPlayer'. When I received Audio URL from silent notification that time I got an error message from debug section. These are... "[0x37fd8000] >aurioc> 807: failed: '!pla' (enable 2, outf< 2 ch, 44100 Hz, Int16, inter> inf< 2 ch, 0 Hz, Float32, non-inter>". – Abir Oct 03 '17 at 09:30
  • Can you tell me one thing are you trying to play different - different sound at every time ? or you need to do whatever you got push data that will play ? – Raj Oriya Oct 03 '17 at 09:34
  • Actually I am trying to play whatever got from silent push data. Suppose I am receiving an Audio URL(Exm. http://www.sample-videos.com/audio/mp3/crowd-cheering.mp3). I want to play this URL when I received from silent notification(Also play when app in background mode). But here finally I got the URL also but it can't playing through AVPlayer, only when the app is in background. – Abir Oct 03 '17 at 09:52
  • Replace this method with your app delegate's applicationDidEnterBackground method and try - (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication *app = [UIApplication sharedApplication]; UIBackgroundTaskIdentifier bgTask = 0; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; }]; [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; } – Raj Oriya Oct 03 '17 at 09:56
  • I have used your code. but it's not working and also got the same error message from debug section. That is : [0x37fd8000] >aurioc> 807: failed: '!pla' (enable 2, outf< 2 ch, 44100 Hz, Int16, inter> inf< 2 ch, 0 Hz, Float32, non-inter>.... Are you clear, what is my concept? – Abir Oct 03 '17 at 10:11
  • I think you need to download it from url first then play that audio like - do { try audioPlayer = AVAudioPlayer(contentsOf: urlRecodered) audioUrl = urlRecodered print("url--->\(audioUrl)") dataOfAudio = NSData(contentsOf: audioUrl) audioPlayer!.delegate = self } where urlRecodered is the url which is downloaded from push data url – Raj Oriya Oct 03 '17 at 10:35
  • is it necessary to download the audio first then able to play autoplayback? If other way have then please tell me. Because if url length is big then it's taken so much time to play an audio. And one more thing, Is it possible to play an audio when app in background mode? I go throw many documents but couldn't find any. – Abir Oct 03 '17 at 10:53
  • Yes it is possible to play audio when app is in the background . I am using my sound file which is placed in my code and i am calling it when i want and it plays sound even app is in the background. – Raj Oriya Oct 03 '17 at 10:59
  • Yes I do that, and it's working. But if no sound file is present into xcode bundle than it's not playing from my side. Only got an error message from debug sections. – Abir Oct 03 '17 at 11:08
  • If sound file is not into your xcode then it will not play sound – Raj Oriya Oct 03 '17 at 11:29
  • @Abir are you happy with answer ? then you can give me up vote :) – Raj Oriya Oct 03 '17 at 11:46