Play ipod library Songs

Source: Internet
Author: User

First, brief

Playing device local music is a more practical feature for AV entertainment applications, and most music players on the market support this feature. Apple offers two ways to access the local music library, mpmediaquery and Mpmediapickercontroller, respectively. Mpmediapickercontroller is a custom UI-enabled control, and in order to meet custom requirements, we usually use more powerful mpmediaquery to get local music.

This article provides a demo for a simple demonstration of how to use Mpmediaquery get local music information and use Avaudioplayer playback. such as:

  

Second, code example

#import "ViewController.h"#import<MediaPlayer/MediaPlayer.h>#import<AVFoundation/AVFoundation.h>@interfaceViewcontroller () <UITableViewDataSource,UITableViewDelegate,AVAudioPlayerDelegate>@property (nonatomic, strong) Avaudioplayer*Audioplayer, @property (nonatomic, strong) Mpmediaquery*Mediaquery, @property (nonatomic, strong) UITableView*Mediatableview; @property (nonatomic, assign) Nsinteger Playingindex;@end@implementationViewcontroller-(UITableView *) mediatableview{if(!_mediatableview) {_mediatableview=[[UITableView alloc] InitWithFrame:self.view.bounds style:uitableviewstylegrouped]; _mediatableview.Delegate=Self ; _mediatableview.datasource=Self ; }    return_mediatableview;}- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.Self.playingindex =Nsintegermax; Self.mediaquery=[Mpmediaquery Songsquery];        [Self.view AddSubview:self.mediaTableView]; UILabel*header = [[UILabel alloc] Initwithframe:cgrectmake (0,0, Cgrectgetwidth (Self.view.bounds),64.0)]; Header.text=@"IPod Library"; Header.textcolor=[Uicolor Browncolor]; Header.textalignment=Nstextalignmentcenter; Header.font= [Uifont systemfontofsize:18.0];        [Self.mediatableview Settableheaderview:header]; UIButton*footer =[UIButton Buttonwithtype:uibuttontypecustom]; Footer.frame= CGRectMake (0,0, Cgrectgetwidth (Self.view.bounds),32.0); [Footer Settitle:@"Stop"Forstate:uicontrolstatenormal]; Footer.titleLabel.font= [Uifont systemfontofsize:18.0];    [Footer Settitlecolor:[uicolor Browncolor] forstate:uicontrolstatenormal];    [Footer addtarget:self Action: @selector (stop) forcontrolevents:uicontroleventtouchupinside]; [Self.mediatableview settablefooterview:footer];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark-action/** * Play audio * * @param itme < #itme description#>*/- (void) Playmediawithitem: (Mpmediaitem *) itme{Self.audioplayer=[[Avaudioplayer alloc] InitWithContentsOfURL:itme.assetURL Error:nil]; Self.audioplayer.Delegate=Self ;    [Self.audioplayer Preparetoplay]; [Self.audioplayer play];}/** * Stop*/- (void) stop{if(self.audioPlayer.playing) {[Self.audioplayer stop]; Self.playingindex=Nsintegermax;    [Self.mediatableview Reloaddata]; }}#pragmamark-uitableviewdatasource-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnSelf.mediaQuery.items.count;}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return 66.0;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellidentifier =@"Ipodmusiccell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(!cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; } Mpmediaitem*item =Self.mediaquery.items[indexpath.row]; Cell.textLabel.text=Item.title; Cell.textLabel.textColor=[Uicolor Browncolor]; Cell.detailTextLabel.text=item.artist; Cell.detailTextLabel.textColor=[Uicolor Browncolor]; Cell.accessorytype=Uitableviewcellaccessorydisclosureindicator; Cell.tintcolor=[Uicolor Browncolor]; if(Indexpath.row = =self.playingindex) {Cell.textLabel.textColor=[Uicolor Redcolor]; Cell.detailTextLabel.textColor=[Uicolor Redcolor]; Cell.accessorytype=Uitableviewcellaccessorycheckmark; Cell.tintcolor=[Uicolor Redcolor]; }    returncell;}- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{Mpmediaitem*item =Self.mediaquery.items[indexpath.row];        [Self playmediawithitem:item]; Self.playingindex=Indexpath.row; [Self.mediatableview reloaddata];}#pragmamark-avaudioplayerdelegate-(void) Audioplayerdidfinishplaying: (Avaudioplayer *) player successfully: (BOOL) flag{if(Player = =self.audioplayer) {Self.playingindex++;//Automatic Next song        if(Self.playingindex <self.mediaQuery.items.count) {[self playmediawithitem:self.mediaquery.items[_playingindex]]; } Else{Self.playingindex=0;        [Self playmediawithitem:[self.mediaquery.items firstobject]];    } [Self.mediatableview Reloaddata]; }}@end

Play ipod library Songs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.