如何从 ipodlibrary 中获取歌曲并使用 AVPlayer 播放 [英] How to fetch songs from ipodlibrary and play using AVPlayer

查看:56
本文介绍了如何从 ipodlibrary 中获取歌曲并使用 AVPlayer 播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 ipod 库中选择歌曲并使用 avplayer 播放我希望音乐即使在应用程序进入后台后也能继续播放我是 iOS 编程新手谁能帮我..

i want to select songs from ipod Library and play it using avplayer i want the music to continue playing even after the app goes to background i am new to iOS programming can anyone help me out ..

谢谢

推荐答案

要允许用户从他们的音乐库中挑选一首(或多首)歌曲,请使用 MPMediaPickerController 类.

To allow the user to pick a song (or songs) from their music library, use the MPMediaPickerController class.

-(void) pickSong {

    // Create picker view
    MPMediaPickerController* picker = [[MPMediaPickerController alloc] init];
    picker.delegate = self;

    // Check how to display
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {

        // Show in popover
        [popover dismissPopoverAnimated:YES];
        popover = [[UIPopoverController alloc] initWithContentViewController:picker];
        [popover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    } else {

        // Present modally
        [self presentViewController:picker animated:YES completion:nil];

    }

}

更改 self.navigationItem.rightBarButtonItem 如果您没有通过标题栏右侧的按钮显示它.

Change self.navigationItem.rightBarButtonItem if you're not showing it from a button on the right side of title bar.

然后你需要通过实现委托来监听结果:

Then you need to listen for the result by implementing the delegate:

在用户取消选择时调用:

-(void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {

    // Dismiss selection view
    [self dismissViewControllerAnimated:YES completion:nil];
    [popover dismissPopoverAnimated:YES];
    popover = nil;

}

在用户选择某些内容时调用:

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

    // Dismiss selection view
    [self dismissViewControllerAnimated:YES completion:nil];
    [popover dismissPopoverAnimated:YES];
    popover = nil;

    // Get AVAsset
    NSURL* assetUrl = [mediaItemCollection.representativeItem valueForProperty:MPMediaItemPropertyAssetURL];
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetUrl options:nil];

    // Create player item
    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];

    // Play it
    AVPlayer* myPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    [myPlayer play]; 

}

您将需要一个 UIPopoverController* popover; 在您的类 .h 文件中.此外,您应该将 myPlayer 保留在某处...

You'll need a UIPopoverController* popover; in your class .h file. Also you should retain myPlayer somewhere...

要让音乐在后台继续播放,请在 UIBackgroundModes 键下的 Info.plist 数组中添加一个 audio 字符串.

To allow music to continue in the background, add an audio string to the array in your Info.plist under the UIBackgroundModes key.

这篇关于如何从 ipodlibrary 中获取歌曲并使用 AVPlayer 播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆