如何显示ipodLibrary中从ipableLibrary中播放的ipodLibrary中的选定歌曲 [英] how to display selected songs from ipodLibrary played from avplayer in uitableview
问题描述
我正在使用AVPlayer播放歌曲.我也可以在后台播放它.有一个名为showPlaylist
的UIButton
.当我点击它时,我需要显示从UITableView
中的ipodLibrary
中选择的歌曲列表,并且我应该能够显示插图,歌曲中剩余的分钟数和艺术家的名字(如果在列表中可用).歌曲.
I am playing songs by using AVPlayer. I am able to play it in background as well. There is a UIButton
called showPlaylist
. When I tap on it I need to display list of songs which I have selected from ipodLibrary
in UITableView
and I should be able to display artwork, number of mins remaining in the song and the artist's name if it is available in the song.
我有播放和暂停按钮:当我单击暂停按钮时,歌曲会暂停,但是当我再次点击播放按钮时,它会转到ipodLibrary.当我点击播放"按钮时如何恢复播放?
I have play and pause buttons: when I click the pause button, the song is paused but when I tap on the play button again, it goes to the ipodLibrary. How to resume play when I tap on the play button?
并且当该UITableView
中有多首歌曲时,我希望它在第一首曲目完成后立即继续到下一首曲目.我想知道如何做到这一点.
And when there are multiple songs in that UITableView
, I want it to continue to the next track as soon as the first track completes. I was wondering how to do that.
-(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;
}
}
推荐答案
我将研究Apple的示例代码清楚地演示了如何完成您描述的所有操作.
I would look into Apple's sample code "Add Music" which clearly demonstrated how to do everything you've described.
此示例应用程序将用从iPod库中保存到MPMediaItemCollection
中的选定歌曲的可变副本的内容填充UITableView
的内容来全面介绍您.它还显示了如何使用MPMediaItems
和MPMediaItemCollections
将特定于曲目的属性显示为单元格标题标签等.
This sample application runs you through the ins and outs of populating a UITableView
with the contents of a mutable copy of selected songs from the iPod library saved into a MPMediaItemCollection
. It also shows how using MPMediaItems
and MPMediaItemCollections
you can display track specific attributes as the cells title label, etc.
-
要填充表格视图,您可以将其设置如下:
For populating the table view, you could set it up something like this:
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"];
}
}
将链接到播放按钮的按钮重命名为选择音乐",并创建一个名为播放"的新按钮,并将其操作设置为:
Rename the button linked to the play button as "Select Music" and make a new button called "Play" with its action set to:
- (IBAction)playMusic
{
[myPlayer play];
}
- (IBAction)playMusic
{
[myPlayer play];
}
编辑:根据MPMediaItemCollection
的内容创建数组:
Creating array from contents of MPMediaItemCollection
:
NSMutableArray *collectionMutableCopy = [[NSMutableArray alloc] initWithArray:myMediaItemCollection.items];
在项目中取消注释以下行:
EDIT 2: Uncomment the following lines in the project:
在- (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文件中
Then add the following to your projects info.plist file
这篇关于如何显示ipodLibrary中从ipableLibrary中播放的ipodLibrary中的选定歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!