2012-07-19 57 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