3no
3no

Reputation: 1

iPhone: Why does applicationMusicPlayer quit playing when app enters background?

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.

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

Upvotes: 0

Views: 2191

Answers (2)

Shannon Cornish
Shannon Cornish

Reputation: 961

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&tstart=120

Upvotes: 4

hotpaw2
hotpaw2

Reputation: 70673

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.

Upvotes: 0

Related Questions