Nag_iphone
Nag_iphone

Reputation: 967

MPMoviePlayer play on when application goes Background

I am working with iOS5.

I am working on HTTPLivestream project .Here i am using MPMovieplayer ,my problem is when application goes to background there is no audio . i am changing the plist in background mode is audio.But there is no result please help me .

AppDel

- (void)applicationDidEnterBackground:(UIApplication *)application {
  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  [[AVAudioSession sharedInstance] setActive: YES error: nil];

  UIApplication *app = [UIApplication sharedApplication];
  backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{
  }];

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  });
}

RadioControlView

  NSString *path =[[NSString alloc] initWithString:@"http://*******/alayam/alayam/playlist.m3u8"];

  audioUrl=[NSURL URLWithString:path];

  appDelegate.player =  [[MPMoviePlayerController alloc] initWithContentURL:audioUrl];
                                   [[NSNotificationCenter defaultCenter] addObserver:self 
                                                         selector:@selector(loadStateDidChange:) 
                                                             name:MPMoviePlayerLoadStateDidChangeNotification 
                                                           object:appDelegate.player];
                if ([appDelegate.player respondsToSelector:@selector(loadState)]) 
                {
                    [appDelegate.player setControlStyle:MPMovieControlModeVolumeOnly];
                    [appDelegate.player setFullscreen:YES];
                    [appDelegate.player prepareToPlay];
                 }

- (void)loadStateDidChange:(NSNotification *)notification
      {   
        [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:appDelegate.player];

      }

Thanks in Advance

Upvotes: 2

Views: 2058

Answers (3)

user187676
user187676

Reputation:

Try to activate the audio session before initializing the player, e.g. in the app launch delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application
  NSError *error = nil;
  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
  [[AVAudioSession sharedInstance] setActive:YES error:&error];

  // other init stuff ...
}

PS: You need to be on the device for audio sessions to work, Simulator won't cut it.

Upvotes: 1

Khalid Usman
Khalid Usman

Reputation: 1282

If you are testing on simulator, then it's doesn't work on simulator, please use device. If you are using device and audio is not playing in background, then can you please email me your complete source code at [email protected]. I have also done in my Application. My code is given below.

- (void)viewDidLoad 
{
    [super viewDidLoad];

    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
    [[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
}

and in plist i made an array named "Required background mode" and insert an item in array name, "App Plays Audio".

Upvotes: 1

Naina Soni
Naina Soni

Reputation: 720

You need to do remote control setting in combination with Background support setting in ios5-

// register your app for remote control

if([[UIApplication sharedApplication]  respondsToSelector:@selector(beginReceivingRemoteControlEvents)])
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];


// allows you to play in the background when app is suspended in iOS5
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:nil];

Place both the settings in application didFinishLaunching delegate

Upvotes: 2

Related Questions