歌曲没有被setQueueWithItemCollection api播放

时间:2012-01-13 19:37:14

标签: iphone ios xcode4

我在iphone 4上使用iOS 5.0测试以下简单代码。我实现了以下示例代码来播放用户选择的歌曲。用户点击页面上的按钮,显示他的播放列表/歌曲....但是一旦选择了歌曲并且选择器被解除,则该歌曲不会播放。是什么给了什么?

来自我的* .h文件

@interface MusicPlayerDemoViewController : UIViewController <MPMediaPickerControllerDelegate> {
    MPMusicPlayerController *musicPlayer;
}

@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;



- (IBAction)selectMusic:(id)sender;

来自我的* .m文件

- (IBAction)selectMusic:(id)sender 
{
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
    mediaPicker.delegate = self;
    mediaPicker.allowsPickingMultipleItems = NO; // this is the default   
    [self presentModalViewController:mediaPicker animated:YES];
    [mediaPicker release];
}                                   

// Media picker delegate methods
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {

// We need to dismiss the picker
  [self dismissModalViewControllerAnimated:YES];


  [musicPlayer setQueueWithItemCollection:mediaItemCollection];
  [musicPlayer play];
}

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
    [self dismissModalViewControllerAnimated:YES];
}

解决方案:

在ViewDidLoad中添加以下代码。我没有实例化播放器。所有作品

[self setMusicPlayer: [MPMusicPlayerController applicationMusicPlayer]];

    // By default, an application music player takes on the shuffle and repeat modes
    //      of the built-in iPod app. Here they are both turned off.
    [musicPlayer setShuffleMode: MPMusicShuffleModeOff];
    [musicPlayer setRepeatMode: MPMusicRepeatModeNone];

0 个答案:

没有答案