views:

102

answers:

2

I made a little test app to try to isolate this issue, but it exhibits the same behavior: the applicationMusicPlayer stops playing immediately when the app enters the background. I don't know what I'm doing wrong, or if it's an Apple bug. It seems so simple that if it were an Apple bug others would have encountered it and posted on it.

  • I've set the info.plist UIBackgroundModes key to Audio
  • I've verified that the app is not terminating
  • I've tested on 4.1 beta 3 with the same results
  • I've searched the web for similar complaints. People report other MPMediaPlayerController issues/bugs, but more complex e.g. involving interaction with AVAudio.

Any/all suggestions appreciated.

Here's the core of my test app:

MPTest.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MPTestViewController : UIViewController <MPMediaPickerControllerDelegate> {
    MPMusicPlayerController *MPPlayer;
}

@property (nonatomic, retain) MPMusicPlayerController *MPPlayer;

@end

MPTest.m

#import "MPTestViewController.h"

@implementation MPTestViewController

@synthesize MPPlayer;

- (void)viewDidLoad {
    [super viewDidLoad];

    // get the application music player
    self.MPPlayer = [MPMusicPlayerController applicationMusicPlayer];

    // break to allow application didFinishLaunching to complete
    [self performSelector:@selector(presentMPPicker) withObject:nil afterDelay:0.01];
}


- (void)presentMPPicker {
    // present the picker in a modal view controller
    MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];     
    picker.delegate = self;
    [self presentModalViewController:picker animated:YES];
    [picker release];   
}


// delegate called after user picks a media item
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
    [self dismissModalViewControllerAnimated:YES];

    // tell the player what to play
    [MPPlayer setQueueWithItemCollection:mediaItemCollection];

    // start playing
    [MPPlayer play];
}

@end
A: 

Did you set the appropriate audio session type for being a media player? The OS uses session types to make decisions among competing uses for the audio channels.

hotpaw2
+1  A: 

MPMusicPlayerController does not support background audio. You will need to use something like AVAudioPlayer or AVPlayer (AVPlayer allows you to use iPod libary items via the AssetURL).

The reason is that MPMusicPlayerController uses the iPod application to play audio, thus your application is not actually playing anything.

Please see this thread on the Apple Developer Forums for more information: https://devforums.apple.com/thread/47905?start=0&amp;tstart=120

Shannon Cornish