2012-07-19 16 views
3

我正在使用AVPlayer播放歌曲。我也能夠在後臺播放它。有一個UIButton調用showPlaylist。當我點擊它時,我需要顯示我從ipodLibraryUITableView中選擇的歌曲列表,並且我應該能夠顯示藝術作品,歌曲中剩餘的分鐘數以及藝術家的名字(如果歌曲中可用)。如何顯示從avplayer播放的ipodLibrary中選定的歌曲在uitableview中播放

我有播放和暫停按鈕:當我點擊暫停按鈕,歌曲暫停,但當我再次點擊播放按鈕時,它會進入ipodLibrary。當我點擊播放按鈕時如何恢復播放?

UITableView中有多首歌曲時,我希望它在第一首曲目完成後立即繼續下一首歌曲。我想知道如何做到這一點。

-(IBAction)playButtonPressed:(UIButton *)sender { 
    // Create picker view 
    MPMediaPickerController* picker = [[MPMediaPickerController alloc] init]; 
    picker.delegate = self; 
    if (userMediaItemCollection) { 
    MusicTableViewController *controller = [[MusicTableViewController alloc] 
    initWithNibName: @"MusicTableView" bundle: nil]; 
    controller.delegate = self; 
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController: controller animated: YES]; 
    } else { 
    MPMediaPickerController *picker = [[MPMediaPickerController alloc] 
     initWithMediaTypes: MPMediaTypeMusic]; 
    picker.delegate = self; 
    picker.allowsPickingMultipleItems = YES; 
    picker.prompt = NSLocalizedString 
     (@"Add songs to play", "Prompt in media item picker"); 
    [[UIApplication sharedApplication] setStatusBarStyle: 
     UIStatusBarStyleDefault animated: YES]; 
    [self presentModalViewController: picker animated: YES]; 
    } 
} 

-(IBAction)pauseButtonPressed:(UIButton *)sender { 
    [myPlayer pause]; 
} 

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

-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems: 
    (MPMediaItemCollection *)mediaItemCollection { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    NSURL* assetUrl = [mediaItemCollection.representativeItem 
    valueForProperty:MPMediaItemPropertyAssetURL]; 
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetUrl options:nil]; 
    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset]; 
    myPlayer = [AVPlayer playerWithPlayerItem:playerItem]; 
    [myPlayer play]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
    [self becomeFirstResponder]; 
} 

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; 
    [self resignFirstResponder]; 
} 

-(void)remoteControlReceivedWithEvent:(UIEvent *)event { 
    switch (event.subtype) { 
    case UIEventSubtypeRemoteControlTogglePlayPause: 
     if (myPlayer.rate == 0.0) { 
     [myPlayer play]; 
     } else { 
     [myPlayer pause]; 
     } 
     break; 
    case UIEventSubtypeRemoteControlPlay: 
     [myPlayer play]; 
     break; 
    case UIEventSubtypeRemoteControlPause: 
     [myPlayer pause]; 
     break; 
    default: 
     break; 
    } 
} 
+0

你可能想看看這個問題,關於你的遠程事件問題http://stackoverflow.com/questions/11566512/how-to-choose-next-song-in-remotecontrolledevents/11567222#11567222 – Dustin 2012-07-19 18:42:13

回答

3

我會看看蘋果的示例代碼"Add Music",它清楚地演示瞭如何做你所描述的一切。

本示例應用程序通過填充UITableView的示例應用程序運行,其中包含從iPod庫中選定歌曲的可變副本內容保存到MPMediaItemCollection。它還顯示瞭如何使用MPMediaItemsMPMediaItemCollections您可以顯示軌道特定屬性作爲單元格標題標籤等

  1. 用於填充表視圖,您可以將其設置是這樣的:

    MPMediaItem *mediaItem = (MPMediaItem *)[collectionMutableCopy objectAtIndex:row];

    MPMediaItemArtwork *artwork = [mediaItem valueForProperty:MPMediaItemPropertyArtwork];

    if (mediaItem) { 
        cell.textLabel.text = [mediaItem valueForProperty:MPMediaItemPropertyTitle]; 
        cell.detailTextLabel.text = [mediaItem valueForProperty:MPMediaItemPropertyArtist]; 
        if (artwork != nil) { 
         cell.imageView.image = [artwork imageWithSize:CGSizeMake (40, 40)]; 
        }else{ 
         cell.imageView.image = [UIImage imageNamed:@"no_artwork.png"]; 
        } 
    } 
    
  2. 重命名按鈕鏈接到播放按鈕「選擇音樂「並設置一個名爲」播放「的新按鈕,其動作設置爲:

    - (IBAction)playMusic { [myPlayer play]; }

編輯:MPMediaItemCollection內容創建陣列:

NSMutableArray *collectionMutableCopy = [[NSMutableArray alloc] initWithArray:myMediaItemCollection.items]; 

編輯2:取消註釋項目中的下列行:

- (void) registerForMediaPlayerNotifications

/* 
// This sample doesn't use libray change notifications; this code is here to show how 
//  it's done if you need it. 
[[NSNotificationCenter defaultCenter] removeObserver: self 
name: MPMediaLibraryDidChangeNotification 
object: musicPlayer]; 

[[MPMediaLibrary defaultMediaLibrary] endGeneratingLibraryChangeNotifications]; 

*/ 

- (void)dealloc

/* 
// This sample doesn't use libray change notifications; this code is here to show how 
//  it's done if you need it. 
[notificationCenter addObserver: self 
selector: @selector (handle_iPodLibraryChanged:) 
name: MPMediaLibraryDidChangeNotification 
object: musicPlayer]; 

[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications]; 
*/ 

替換此行:

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil]; 

有了這個

[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil]; 

UInt32 doSetProperty = 0; 
AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryMixWithOthers, 
sizeof (doSetProperty), 
&doSetProperty 
); 

然後添加以下到您的項目Info.plist文件

enter image description here

+0

^^我對錯誤格式的回覆表示歉意......語法高亮似乎沒有正常工作。 – 2012-07-19 16:55:09

+0

我可以使用相同的單元格來顯示標題名稱,藝術家名稱以及藝術品,因爲我增加了tableviewcell高度,以便它可以佔據所有這些。 糾正我,如果我錯了上面的代碼將在cellForRowAtIndexPath中實現。 – coded 2012-07-19 17:02:41

+0

我只有一個視圖,我在上面放置了按鈕,所以當我點擊showMusic按鈕時,UITableview將顯示在按鈕下方,以便它可以佔據屏幕的底部 – coded 2012-07-19 17:06:43

1

你的問題是,你的播放按鈕正在做的不只是告訴音樂播放器玩。你需要一個只需撥打[myPlayer play];除了播放按鈕之外,將所有設置都放在其他地方。

讓你的播放按鈕也選擇歌曲並不是很好的編碼習慣(最好保留一個按鈕 - >一個功能)。所以,有一個按鈕,不會在你的播放按鈕,現在是什麼,然後讓你的播放按鈕,只需要調用[myPlayer play]

至於你的其他問題,你應該看看這個:Play multiple audio files using AVAudioPlayer

只要你知道,你」實際上不應該在同一篇帖子中提出多個問題。這使得它更難以給出一個簡潔有用的答案,並可以使網站更難找到的東西。

我不確定你是否問如何顯示歌曲信息,但我想它是在你的標題。對於這一個,iPhone sdk - accessing current song information through an app

+0

所以我會添加另一個UIButton,它允許我選擇歌曲和另一個UIButton來播放歌曲。 – coded 2012-07-19 17:04:20

+1

這將是一個好主意,雖然技術上你可以使用一些變量來跟蹤發生了什麼,並相應地改變你的按鈕的行爲。 – Dustin 2012-07-19 17:05:28

+0

謝謝,關於多個音頻文件的文章,你已經提到過AVAudioPlayer,我使用AVPlayer,只要第一首歌曲的歌詞得到結束,如果在選定的播放列表中有另一首歌曲,它應該開始播放。閱讀某處使用NSNotification,你會採取什麼 – coded 2012-07-19 17:10:27