2013-01-21 56 views
2

我正在編程獲取iPhone音樂庫以顯示,用戶選擇一首歌曲,然後這首歌曲標題反映在UILabel中。如果只是這麼簡單!我試圖將MPMediaItem變成NSString,然後UILabel反映了這一點,但我只是得到(空)返回!爲什麼從MPMediaPicker(iOS)返回(null)

- (IBAction)showMediaPicker:(id)sender { 
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny]; 

    mediaPicker.delegate = self; 
    //mediaPicker.allowsPickingMultipleItems = YES; 
    mediaPicker.prompt = @"Select Your Favourite Song!"; 

    [self presentModalViewController:mediaPicker animated:YES]; 
} 

- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItem *) mediaItemCollection { 

    NSString *titleString = [mediaItemCollection valueForProperty:MPMediaItemPropertyTitle]; 
    titleLabel.text = [NSString stringWithFormat:@"Title: %@",titleString]; 


    [self dismissModalViewControllerAnimated: YES]; 
} 

在此先感謝

回答

2

你試圖抓住標題爲mediaItemCollection。您需要獲取單個歌曲標題是這樣的:

MPMediaItem *selectedSong = [mediaItemCollection items] objectAtIndex:0]; 
NSString *titleString = [selectedSong valueForProperty:MPMediaItemPropertyTitle]; 

另外順便說一下,你有你的MPMediaPickerControllerDelegate錯誤的委派安裝:

它應該是:

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection 

- 你有MPMediaItemCollection作爲一個MPMediaItem

+0

+1這是一個了不起的幫助,謝謝!我在寫這篇文章後不久就注意到了錯誤的代表設置。再一次,謝謝你! – iphoneapplelover123

+0

很高興幫助!如果您滿意,請接受答案。謝謝! –