Created
October 25, 2013 23:45
-
-
Save priore/7163533 to your computer and use it in GitHub Desktop.
Play Audio Streaming in Backgrund mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment