Question

J'ai juste besoin de confirmer si une application iPhone peut lire ou diffuser la radio en arrière-plan.

J'ai créé une application radio qui fonctionne bien.

Je veux ajouter une fonctionnalité par laquelle l'utilisateur peut lire la radio dans un processus d'arrière-plan, c'est-à-dire qu'il n'a pas à garder l'application ouverte pour lire la radio.

Ils peuvent fermer l'application et la radio joue toujours.

J'ai lu il y a longtemps qu'Apple a une API privée par laquelle une application peut exécuter la radio en arrière-plan, mais comme elle est privée, je ne peux pas l'utiliser.

Aucune suggestion?

Était-ce utile?

La solution

Le SDK iOS doit être supérieur à 4,0; D'abord dans vos codes délégués:

- (void)applicationDidEnterBackground:(UIApplication *)application {

    if (isAbove4_0)

 {

        CTCallCenter *_center = [[CTCallCenter alloc] init];

        _center.callEventHandler = ^(CTCall *call) 
       {

            //NSLog(@"call:%@", call.callState);
            if ([call.callState isEqualToString:CTCallStateIncoming]) 

                {

                [RadioPlayer pausePlayer];

        }           
    };
    }

}

Ensuite, j'ai utilisé MPMoViePlayerController, et AVPlayer est OK;

if (isAbove4_0) {
    if (_mpmovieplayer == nil) {
        _mpmovieplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [_mpmovieplayer setMovieControlMode:MPMovieControlModeHidden];
    }

    [_mpmovieplayer setContentURL:url];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    // This is necessary if you want to play a sequence of songs, otherwise your app will be
    // killed after the first one finishes.
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;

    [_mpmovieplayer play];

    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid)
        [[UIApplication sharedApplication] endBackgroundTask: bgTaskId];
    bgTaskId = newTaskId;

}

PS: Mon anglais est pauvre, j'espère que cela pourrait vous être utile :)

Autres conseils

Vous pouvez utiliser l'un de ces streamer audio pour jouer en arrière-plan

https://github.com/mattgallagher/audiostreamer

https://github.com/digitaldj/audiostreamer

Cela pourrait vous aider.

C'est comme ça que ça marche pour moi.

Ajouter le suivi à votre fichier app-fo.plist

<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>

Puis dans l'application: didfinishlaunchingwithoptions ajouter suivant

UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
     /* just fail if this happens. */
     NSLog(@"BackgroundTask Expiration Handler is called");
     [application endBackgroundTask:backgroundTask];
}];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top