Xcode : intégration d’une webradio accessible dans le centre de contrôle audio

lundi 17 mars 2014

Voici un exemple de code qui intègre une Webradio définit par une url dans une application et qui se retrouvera dans le lecteur audio intégré de votre appareil. La Webradio tournera même si l’application est en background et sera toujours accessible dans le centre de contrôle de votre appareil et lorsque votre sera verrouillé.

Tout d’abord, il faut activer le mode audio en background:

Audio Background mode

Ensuite inclure les frameworks audio nécessaire pour notre application:

#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

Dans le .h

@interface ViewController : UIViewController <AVAudioPlayerDelegate>
@property (nonatomic) MPMoviePlayerController *audioPlayer;
@end

Dans le .m

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    NSError *setCategoryError = nil;
    NSError *activationError = nil;
    [[AVAudioSession sharedInstance] setActive:YES error:&activationError];
    [[AVAudioSession sharedInstance] setDelegate:self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(playWebradio:) forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"PLAY" forState:UIControlStateNormal];
    button.frame = CGRectMake(140, 240, 40, 40);
    [self.view addSubview:button];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button2 addTarget:self action:@selector(stopWebradio:) forControlEvents:UIControlEventTouchDown];
    [button2 setTitle:@"STOP" forState:UIControlStateNormal];
    button2.frame = CGRectMake(145, 300, 50, 40);
    [self.view addSubview:button2];
    
}
- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.audioPlayer stop];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
    
    [super viewWillDisappear:animated];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
    
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlPlay:
                [self.audioPlayer play];
                break;
            case UIEventSubtypeRemoteControlPause:
                [self.audioPlayer pause];
                break;
            case UIEventSubtypeRemoteControlTogglePlayPause:
                if (self.audioPlayer.playbackState == MPMoviePlaybackStatePlaying) {
                    [self.audioPlayer pause];
                }
                else {
                    [self.audioPlayer play];
                }
                break;
            default:
                break;
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) playWebradio:(id)sender{
    self.audioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"url_webradio"]];
    [self.audioPlayer prepareToPlay];
    [self.audioPlayer play];
}

- (void) stopWebradio:(id)sender{
    [self.audioPlayer pause];
}

Be Sociable, Share!

Tags: audio , background mode , centre de contrôle