Objective-C Tips & Trick - Prioregroup.com
[Objective-C] Play Audio Streaming in Backgrund mode
       
back

- (void)playAudioStreamingInBacgkroundMode {
    // set audio session for background music
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    
    NSError *setCategoryError = nil;
    BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    if (success) {
        
        NSError *activationError = nil;
        success = [audioSession setActive:YES error:&activationError];
        if (!success) {
            NSLog(@"No AudioSession available!");
            NSLog(@"%@", [activationError localizedDescription]);
        }
    }
    
    // init movie player in hidden mode
    NSURL *url = [NSURL URLWithString:@"http://myradiostream.com/4/3612/listen.pls"];
    moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
    moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;
    moviePlayerController.controlStyle = MPMovieControlStyleNone;
    moviePlayerController.repeatMode = MPMovieRepeatModeOne;
    moviePlayerController.shouldAutoplay = YES;
    moviePlayerController.view.hidden = YES;
    
    // set movie player notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePreloadDidFinish:)
                                                 name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieTimedMetadataUpdated:)
                                                 name:MPMoviePlayerTimedMetadataUpdatedNotification
                                               object:nil];
    [moviePlayerController prepareToPlay];
}

#pragma mark - MoviePlayer Notifications

- (void)moviePlaybackComplete:(NSNotification*)notification
{
    // restart when finished
    if (moviePlayerController.playbackState != MPMoviePlaybackStatePlaying) {

        NSLog(@"Restart streaming");

        moviePlayerController.currentPlaybackTime = 0;
        [moviePlayerController play];
    }
}

- (void)moviePreloadDidFinish:(NSNotification*)notification
{
}

- (void)movieTimedMetadataUpdated:(NSNotification*)notification
{
    // get metadata
    if ([moviePlayerController timedMetadata] != nil) {
	    NSArray *metadata = [moviePlayerController timedMetadata];
    	MPTimedMetadata *meta = [metadata objectAtIndex:0];
    	NSString *songInfo = meta.value;

    }
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com