priore

Play Audio Streaming in Backgrund mode

Apr 2nd, 2013
639
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)playAudioStreamingInBacgkroundMode {
  2.     // set audio session for background music
  3.     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  4.     AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  5.    
  6.     NSError *setCategoryError = nil;
  7.     BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
  8.     if (success) {
  9.        
  10.         NSError *activationError = nil;
  11.         success = [audioSession setActive:YES error:&activationError];
  12.         if (!success) {
  13.             NSLog(@"No AudioSession available!");
  14.             NSLog(@"%@", [activationError localizedDescription]);
  15.         }
  16.     }
  17.    
  18.     // init movie player in hidden mode
  19.     NSURL *url = [NSURL URLWithString:@"http://myradiostream.com/4/3612/listen.pls"];
  20.     moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
  21.     moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;
  22.     moviePlayerController.controlStyle = MPMovieControlStyleNone;
  23.     moviePlayerController.repeatMode = MPMovieRepeatModeOne;
  24.     moviePlayerController.shouldAutoplay = YES;
  25.     moviePlayerController.view.hidden = YES;
  26.    
  27.     // set movie player notifications
  28.     [[NSNotificationCenter defaultCenter] addObserver:self
  29.                                              selector:@selector(moviePlaybackComplete:)
  30.                                                  name:MPMoviePlayerPlaybackDidFinishNotification
  31.                                                object:moviePlayerController];
  32.    
  33.     [[NSNotificationCenter defaultCenter] addObserver:self
  34.                                              selector:@selector(moviePreloadDidFinish:)
  35.                                                  name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification
  36.                                                object:nil];
  37.    
  38.     [[NSNotificationCenter defaultCenter] addObserver:self
  39.                                              selector:@selector(movieTimedMetadataUpdated:)
  40.                                                  name:MPMoviePlayerTimedMetadataUpdatedNotification
  41.                                                object:nil];
  42.     [moviePlayerController prepareToPlay];
  43. }
  44.  
  45. #pragma mark - MoviePlayer Notifications
  46.  
  47. - (void)moviePlaybackComplete:(NSNotification*)notification
  48. {
  49.     // restart when finished
  50.     if (moviePlayerController.playbackState != MPMoviePlaybackStatePlaying) {
  51.  
  52.         NSLog(@"Restart streaming");
  53.  
  54.         moviePlayerController.currentPlaybackTime = 0;
  55.         [moviePlayerController play];
  56.     }
  57. }
  58.  
  59. - (void)moviePreloadDidFinish:(NSNotification*)notification
  60. {
  61. }
  62.  
  63. - (void)movieTimedMetadataUpdated:(NSNotification*)notification
  64. {
  65.     // get metadata
  66.     if ([moviePlayerController timedMetadata] != nil) {
  67.         NSArray *metadata = [moviePlayerController timedMetadata];
  68.         MPTimedMetadata *meta = [metadata objectAtIndex:0];
  69.         NSString *songInfo = meta.value;
  70.  
  71.     }
  72. }
RAW Paste Data