Simple music player with MediaPlayer framework-Chen Peng

Source: Internet
Author: User

The project needs to use the MediaPlayer framework, so the program needs to add the Mediapalyer framework to the project and use it in the implementation section of the controller class above #import<mediaplayer/mediaplayer.h> Import header file for this frame

List of programs:

@interface Fkviewcontroller () <mpmediapickercontrollerdelegate>{mpmediapickercontroller* MPC;    mpmusicplayercontroller* Musicplayer; Mpmediaitemcollection * itemList;}    @end @implementation fkviewcontroller-(void) viewdidload {[Super viewdidload];    Nsarray * paths = Nssearchpathfordirectoriesindomains (Nsdocumentationdirectory, Nsuserdomainmask, YES);    NSLog (@ "path=%@", Paths[0]); Uibarbuttonitem * bn = [[Uibarbuttonitem alloc]initwithtitle:@ "Select Music" Style:uibarbuttonitemstyleplain target:self    Action: @selector (choose:)];    Self.navigationItem.rightBarButtonItem = bn; Create a music playerMusicplayer = [Mpmusicplayercontroller systemmusicplayer];Create a Mpmediapickercontroller objectMPC = [[Mpmediapickercontroller Alloc]initwithmediatypes:mpmediatypeanyaudio];Create a Mpmediapickercontroller Setup delegatempc.delegate = self;Set hint text for music selectionmpc.prompt = @ "Please choose your favorite music";Set whether to allow multiple selectionsmpc.allowspickingmultipleitems = YES;Set whether to allow selection of cloud music mpc.showsclouditems = YES; }-(void) Choose: (ID) sender{//must display Mpmusicplayercontroller view controller in modal mode [self PRESENTVIEWCONTROLLER:MPC animated:yes Completion:nil];} This method is fired when the user chooses to specify music, Mediaitemcolllection on behalf of the user's selected music-(void) Mediapicker: (Mpmediapickercontroller *) Mediapicker didpickmediaitems: (mpmediaitemcollection *) mediaitemcollection{Save user selected music list itemList = mediaitemcollection;//Set User selected music list to Musicplayer playlist [Musicplayer Setqueuewithitemcollectio    N:mediaitemcollection];    [Self.tableview Reloaddata];    [MPC Dismissviewcontrolleranimated:yes Completion:nil];    }-(void) Mediapickerdidcancel: (Mpmediapickercontroller *) mediapicker{NSLog (@ "User canceled selection");    [MPC Dismissviewcontrolleranimated:yes Completion:nil];    }-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} #pragma mark Uitableviewdatasource and Uitableviewdatagate proxy Method-(Nsinteger) TableView: (UITableView *) TableView    Numberofrowsinsection: (Nsinteger) section{return itemlist.count; }-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;} -(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{Nsuinteger    RowNo = Indexpath.row;    Static NSString * Cellid = @ "Cellid"; UITableViewCell * cell = [TableView dequeuEreusablecellwithidentifier:cellid];    if (!cell) {cell = [[UITableViewCell alloc]initwithstyle:0 reuseidentifier:cellid];    } Cell.textLabel.text = [Itemlist.items[rowno] valueforkeypath:mpmediaitempropertytitle]; return cell;} -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//Get row number of table row Nsuinteger ro    WNO = Indexpath.row;    Set the music that the player is about to play Musicplayer.nowplayingitem = [Itemlist.items Objectatindex:rowno]; Start playing [Musicplayer play];} -(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return 44;}

In the above program, the 1th line of bold code creates a Mpmusicplayercontroller object that will copy the music, and then the program creates the Mpmediapixkercontroller object and sets the corresponding properties for the object. The object will be responsible for selecting music.

The above view controller class implements the Mpmediapickercontrollerdelegate protocol, and implements two methods in the protocol, especially the Mediapicker:idpickmediaitems: method, This method is fired when the user selects more than one music. This method saves the user's choice of multiple music, and use the music selected by the user as a playlist of Mpmusicplayercontroller. The program also implements Uitableviewdatasource and Uitableviewdatagate methods so that the user-selected music can be displayed in a table, and controls the music that the user selects to play the response when a table row is specified.

Simple music player with MediaPlayer framework-Chen Peng

Related Article

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.