您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在iOS中獲取系統(tǒng)iTunes音樂,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
一、創(chuàng)建
MPMediaPickerController *mpc = [[MPMediaPickerControlleralloc]initWithMediaTypes:MPMediaTypeMusic]; mpc.delegate = self;//委托 mpc.prompt =@"Please select a music";//提示文字 mpc.allowsPickingMultipleItems=NO;//是否允許一次選擇多個(gè)
上述代碼創(chuàng)建了一個(gè) MPMediaPickerController 并設(shè)置了相關(guān)屬性。其中初始化的時(shí)候有一個(gè)參數(shù)是媒體類型,媒體類型可以是下面這些值:
enum { // audio MPMediaTypeMusic = 1 << 0, MPMediaTypePodcast = 1 << 1, MPMediaTypeAudioBook = 1 << 2, MPMediaTypeAudioITunesU = 1 << 3, // available in iOS 5.0 MPMediaTypeAnyAudio = 0x00ff, // video (available in iOS 5.0) MPMediaTypeMovie = 1 << 8, MPMediaTypeTVShow = 1 << 9, MPMediaTypeVideoPodcast = 1 << 10, MPMediaTypeMusicVideo = 1 << 11, MPMediaTypeVideoITunesU = 1 << 12, MPMediaTypeAnyVideo = 0xff00, MPMediaTypeAny = ~0 }; typedef NSInteger MPMediaType;
二、委托函數(shù)
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{ /*insert your code*/ for ( MPMediaItem* itemin [mediaItemCollection items]) { } [selfdismissModalViewControllerAnimated:YES]; [mediaPicker release]; }
在上面這個(gè)函數(shù)里你可以處理所選取的內(nèi)容。下面這個(gè)函數(shù)負(fù)責(zé)處理選中后取消的動作:
-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{ /*insert your code*/ [selfdismissModalViewControllerAnimated:YES]; [mediaPicker release]; }
三、顯示
你可以在任何需要顯示的時(shí)候調(diào)用下面代碼:
[selfpresentModalViewController:mpc animated:YES];
使用系統(tǒng)自帶的控制器選擇iTunes歌曲
#pragma mark -使用系統(tǒng)自帶的控制器 - (void)presentiTunesMediaPickerController { //1.創(chuàng)建媒體選擇器 /**MPMediaType(大概有13種,這里只列出比較常用的幾種) MPMediaTypeMusic:音樂歌曲 MPMediaTypePodcast:博客(有聲雜志) MPMediaTypeAudioITunesU:iTuneU中的有聲讀物 MPMediaTypeMovie:電影 */ MPMediaPickerController *controller = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic]; //2.是否支持多選,默認(rèn)為no controller.allowsPickingMultipleItems = YES; //在導(dǎo)航欄的上方添加一個(gè)提示文本 controller.prompt = @"傳智播客-黑馬程序員"; //3.設(shè)置代理(代理比較簡單,只有兩個(gè)方法,完成選取和取消選?。? controller.delegate = self; //4.彈出媒體選擇器 [self presentViewController:controller animated:YES completion:nil]; }
1、MPMediaPickerControllerDelegate代理
#pragma mark -MPMediaPickerControllerDelegate //完成選取 - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection { for (MPMediaItem *song in mediaItemCollection.items) { //解析數(shù)據(jù) [self resolverMediaItem:song]; } //解除媒體選擇器器 [mediaPicker dismissViewControllerAnimated:YES completion:nil]; } //取消選取 - (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker { //解除媒體選擇器器 [mediaPicker dismissViewControllerAnimated:YES completion:nil]; }
2、解析iTunes歌曲數(shù)據(jù)
#pragma mark -解析iTune音樂數(shù)據(jù) - (void)resolverMediaItem:(MPMediaItem *)song { //歌名 NSString *name = [song valueForProperty: MPMediaItemPropertyTitle]; //歌曲路徑 NSString *url = [song valueForProperty: MPMediaItemPropertyAssetURL]; //歌手名字 NSString *songer = [song valueForProperty: MPMediaItemPropertyArtist]; //歌曲時(shí)長(單位:秒) 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]; //從插圖中獲取圖像,參數(shù)size是圖像的大小 UIImage *image = [artwork imageWithSize:CGSizeMake(50, 50)]; self.label.text = [NSString stringWithFormat:@"歌名:%@ \n 歌曲路徑:%@ \n 歌手名字:%@ \n 歌曲時(shí)長%@ \n 歌曲插圖:%@",name,url,songer,time,image]; }
只獲取iTunes中的數(shù)據(jù),自定義界面
解析數(shù)組的操作與使用系統(tǒng)自帶控制器的方式一致
// 1.創(chuàng)建媒體選擇隊(duì)列(從ipod庫中讀出音樂文件) MPMediaQuery *everything = [[MPMediaQuery alloc] init]; // 2.創(chuàng)建讀取條件(類似于對數(shù)據(jù)做一個(gè)篩選) Value:作用等同于MPMediaType枚舉值 MPMediaPropertyPredicate *albumNamePredicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic ] forProperty: MPMediaItemPropertyMediaType]; //3.給隊(duì)列添加讀取條件 [everything addFilterPredicate:albumNamePredicate]; //4.從隊(duì)列中獲取符合條件的數(shù)組集合 NSArray *itemsFromGenericQuery = [everything items]; //5.便利解析數(shù)據(jù) for (MPMediaItem *song in itemsFromGenericQuery) { [self resolverMediaItem:song]; }
完整代碼
#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 -使用系統(tǒng)自帶的控制器 - (void)presentiTunesMediaPickerController { //1.創(chuàng)建媒體選擇器 /**MPMediaType(大概有13種,這里只列出比較常用的幾種) MPMediaTypeMusic:音樂歌曲 MPMediaTypePodcast:博客(有聲雜志) MPMediaTypeAudioITunesU:iTuneU中的有聲讀物 MPMediaTypeMovie:電影 */ MPMediaPickerController *controller = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic]; //2.是否支持多選,默認(rèn)為no controller.allowsPickingMultipleItems = YES; //在導(dǎo)航欄的上方添加一個(gè)提示文本 controller.prompt = @"傳智播客-黑馬程序員"; //3.設(shè)置代理(代理比較簡單,只有兩個(gè)方法,完成選取和取消選?。? controller.delegate = self; //4.彈出媒體選擇器 [self presentViewController:controller animated:YES completion:nil]; } #pragma mark -只獲取iTunes中的數(shù)據(jù),界面自定義 - (void)getiTuneLibrary { // 1.創(chuàng)建媒體選擇隊(duì)列(從ipod庫中讀出音樂文件) MPMediaQuery *everything = [[MPMediaQuery alloc] init]; // 2.創(chuàng)建讀取條件(類似于對數(shù)據(jù)做一個(gè)篩選) Value:作用等同于MPMediaType枚舉值 MPMediaPropertyPredicate *albumNamePredicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic ] forProperty: MPMediaItemPropertyMediaType]; //3.給隊(duì)列添加讀取條件 [everything addFilterPredicate:albumNamePredicate]; //4.從隊(duì)列中獲取符合條件的數(shù)組集合 NSArray *itemsFromGenericQuery = [everything items]; //5.便利解析數(shù)據(jù) for (MPMediaItem *song in itemsFromGenericQuery) { [self resolverMediaItem:song]; } } #pragma mark -解析iTune音樂數(shù)據(jù) - (void)resolverMediaItem:(MPMediaItem *)song { //歌名 NSString *name = [song valueForProperty: MPMediaItemPropertyTitle]; //歌曲路徑 NSString *url = [song valueForProperty: MPMediaItemPropertyAssetURL]; //歌手名字 NSString *songer = [song valueForProperty: MPMediaItemPropertyArtist]; //歌曲時(shí)長(單位:秒) 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]; //從插圖中獲取圖像,參數(shù)size是圖像的大小 UIImage *image = [artwork imageWithSize:CGSizeMake(50, 50)]; self.label.text = [NSString stringWithFormat:@"歌名:%@ \n 歌曲路徑:%@ \n 歌手名字:%@ \n 歌曲時(shí)長%@ \n 歌曲插圖:%@",name,url,songer,time,image]; } #pragma mark -MPMediaPickerControllerDelegate //完成選取 - (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection { for (MPMediaItem *song in mediaItemCollection.items) { //解析數(shù)據(jù) [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
關(guān)于怎么在iOS中獲取系統(tǒng)iTunes音樂就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。