您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在iOS中獲取系統iTunes音樂,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
一、創建
MPMediaPickerController *mpc = [[MPMediaPickerControlleralloc]initWithMediaTypes:MPMediaTypeMusic]; mpc.delegate = self;//委托 mpc.prompt =@"Please select a music";//提示文字 mpc.allowsPickingMultipleItems=NO;//是否允許一次選擇多個
上述代碼創建了一個 MPMediaPickerController 并設置了相關屬性。其中初始化的時候有一個參數是媒體類型,媒體類型可以是下面這些值:
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;
二、委托函數
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{ /*insert your code*/ for ( MPMediaItem* itemin [mediaItemCollection items]) { } [selfdismissModalViewControllerAnimated:YES]; [mediaPicker release]; }
在上面這個函數里你可以處理所選取的內容。下面這個函數負責處理選中后取消的動作:
-(void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker{ /*insert your code*/ [selfdismissModalViewControllerAnimated:YES]; [mediaPicker release]; }
三、顯示
你可以在任何需要顯示的時候調用下面代碼:
[selfpresentModalViewController:mpc animated:YES];
使用系統自帶的控制器選擇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]; }
1、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]; }
2、解析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]; }
只獲取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]; }
完整代碼
#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
關于怎么在iOS中獲取系統iTunes音樂就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。