iOS获取系统iTunes音乐
1.1-简介
-
获取类型iTune音乐非常类似于
UIKit
框架中UIImagePickerController
图片选取器的用法,既可以直接使用系统自带的媒体选择器
也可以只获取系统的数据加载到自定义界面中 -
与
UIImagePickerController
不同的是,MPMediaPickerController
媒体选择器是放在<MediaPlayer/MediaPlayer.h>
框架中的
1.2-使用系统自带的控制器选择iTunes歌曲
#pragma mark -使用系统自带的控制器
- (void)presentiTunesMediaPickerController
{
//1.创建媒体选择器
/**MPMediaType(大概有13种,这里只列出比较常用的几种)
MPMediaTypeMusic:音乐歌曲
MPMediaTypePodcast:博客(有声杂志)
MPMediaTypeAudioITunesU:iTuneU中的有声读物
MPMediaTypeMovie:电影
*/
MPMediaPickerController *controller = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
//2.是否支持多选,默认为no
controller.allowsPickingMultipleItems = YES;
//在导航栏的上方添加一个提示文本
controller.prompt = @"传智播客-黑马程序员";
//3.设置代理(代理比较简单,只有两个方法,完成选取和取消选取)
controller.delegate = self;
//4.弹出媒体选择器
[self presentViewController:controller animated:YES completion:nil];
}
- MPMediaPickerControllerDelegate代理
#pragma mark -MPMediaPickerControllerDelegate
//完成选取
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
for (MPMediaItem *song in mediaItemCollection.items) {
//解析数据
[self resolverMediaItem:song];
}
//解除媒体选择器器
[mediaPicker dismissViewControllerAnimated:YES completion:nil];
}
//取消选取
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
//解除媒体选择器器
[mediaPicker dismissViewControllerAnimated:YES completion:nil];
}
- 解析iTunes歌曲数据
#pragma mark -解析iTune音乐数据
- (void)resolverMediaItem:(MPMediaItem *)song
{
//歌名
NSString *name = [song valueForProperty: MPMediaItemPropertyTitle];
//歌曲路径
NSString *url = [song valueForProperty: MPMediaItemPropertyAssetURL];
//歌手名字
NSString *songer = [song valueForProperty: MPMediaItemPropertyArtist];
//歌曲时长(单位:秒)
NSTimeInterval INW = [[song valueForProperty: MPMediaItemPropertyPlaybackDuration] doubleValue];
NSString *time;
if((int)INW%60<10)
{
time = [NSString stringWithFormat:@"%d:0%d",(int)INW/60,(int)INW%60];
}
else
{
time = [NSString stringWithFormat:@"%d:%d",(int)INW/60,(int)INW%60];
}
if(songer == nil)
{
songer = @"未知歌手";
}
//歌曲插图(如果没有插图,则返回nil)
MPMediaItemArtwork *artwork = [song valueForProperty: MPMediaItemPropertyArtwork];
//从插图中获取图像,参数size是图像的大小
UIImage *image = [artwork imageWithSize:CGSizeMake(50, 50)];
self.label.text = [NSString stringWithFormat:@"歌名:%@ \n 歌曲路径:%@ \n 歌手名字:%@ \n 歌曲时长%@ \n 歌曲插图:%@",name,url,songer,time,image];
}
1.3-只获取iTunes中的数据,自定义界面
- 解析数组的操作与使用系统自带控制器的方式一致
// 1.创建媒体选择队列(从ipod库中读出音乐文件)
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
// 2.创建读取条件(类似于对数据做一个筛选) Value:作用等同于MPMediaType枚举值
MPMediaPropertyPredicate *albumNamePredicate =
[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic ] forProperty: MPMediaItemPropertyMediaType];
//3.给队列添加读取条件
[everything addFilterPredicate:albumNamePredicate];
//4.从队列中获取符合条件的数组集合
NSArray *itemsFromGenericQuery = [everything items];
//5.便利解析数据
for (MPMediaItem *song in itemsFromGenericQuery) {
[self resolverMediaItem:song];
}
1.4—完整代码
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <CoreFoundation/CoreFoundation.h>
@interface ViewController ()<MPMediaPickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self presentiTunesMediaPickerController];
}
#pragma mark -使用系统自带的控制器
- (void)presentiTunesMediaPickerController
{
//1.创建媒体选择器
/**MPMediaType(大概有13种,这里只列出比较常用的几种)
MPMediaTypeMusic:音乐歌曲
MPMediaTypePodcast:博客(有声杂志)
MPMediaTypeAudioITunesU:iTuneU中的有声读物
MPMediaTypeMovie:电影
*/
MPMediaPickerController *controller = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
//2.是否支持多选,默认为no
controller.allowsPickingMultipleItems = YES;
//在导航栏的上方添加一个提示文本
controller.prompt = @"传智播客-黑马程序员";
//3.设置代理(代理比较简单,只有两个方法,完成选取和取消选取)
controller.delegate = self;
//4.弹出媒体选择器
[self presentViewController:controller animated:YES completion:nil];
}
#pragma mark -只获取iTunes中的数据,界面自定义
- (void)getiTuneLibrary
{
// 1.创建媒体选择队列(从ipod库中读出音乐文件)
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
// 2.创建读取条件(类似于对数据做一个筛选) Value:作用等同于MPMediaType枚举值
MPMediaPropertyPredicate *albumNamePredicate =
[MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic ] forProperty: MPMediaItemPropertyMediaType];
//3.给队列添加读取条件
[everything addFilterPredicate:albumNamePredicate];
//4.从队列中获取符合条件的数组集合
NSArray *itemsFromGenericQuery = [everything items];
//5.便利解析数据
for (MPMediaItem *song in itemsFromGenericQuery) {
[self resolverMediaItem:song];
}
}
#pragma mark -解析iTune音乐数据
- (void)resolverMediaItem:(MPMediaItem *)song
{
//歌名
NSString *name = [song valueForProperty: MPMediaItemPropertyTitle];
//歌曲路径
NSString *url = [song valueForProperty: MPMediaItemPropertyAssetURL];
//歌手名字
NSString *songer = [song valueForProperty: MPMediaItemPropertyArtist];
//歌曲时长(单位:秒)
NSTimeInterval INW = [[song valueForProperty: MPMediaItemPropertyPlaybackDuration] doubleValue];
NSString *time;
if((int)INW%60<10)
{
time = [NSString stringWithFormat:@"%d:0%d",(int)INW/60,(int)INW%60];
}
else
{
time = [NSString stringWithFormat:@"%d:%d",(int)INW/60,(int)INW%60];
}
if(songer == nil)
{
songer = @"未知歌手";
}
//歌曲插图(如果没有插图,则返回nil)
MPMediaItemArtwork *artwork = [song valueForProperty: MPMediaItemPropertyArtwork];
//从插图中获取图像,参数size是图像的大小
UIImage *image = [artwork imageWithSize:CGSizeMake(50, 50)];
self.label.text = [NSString stringWithFormat:@"歌名:%@ \n 歌曲路径:%@ \n 歌手名字:%@ \n 歌曲时长%@ \n 歌曲插图:%@",name,url,songer,time,image];
}
#pragma mark -MPMediaPickerControllerDelegate
//完成选取
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
for (MPMediaItem *song in mediaItemCollection.items) {
//解析数据
[self resolverMediaItem:song];
}
//解除媒体选择器器
[mediaPicker dismissViewControllerAnimated:YES completion:nil];
}
//取消选取
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
//解除媒体选择器器
[mediaPicker dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end