iOS implements multimedia audio music player

Source: Internet
Author: User
Tags allkeys set time

As the smartphone market becomes more active, the app has become colourful and diverse, making your app more appealing to multimedia technology inevitably. Make your app more colorful with controls like audio and video, and work with you today on basic audio usage. This article only provides part of the code, if the question can download source view.

The main function interface is as follows:

    1. First introduce the audio playback frame


 2, through the story version to build the interface, because this step is very simple, here does not explain. :

3. Define variables to hold data and encapsulate the song information in the file into a model into the array

/** Load Song data */@property (nonatomic,strong) nsmutablearray * dataarray;/** save cell Data Control color */@property (Nonatomic,strong) Nsmutablearray * cellarray;/** currently playing music */@property (nonatomic,strong) Jrplayerviewcontroller * playervc;/** last music played */ @property (Nonatomic,strong) nsindexpath * front; #pragma mark-load data-(void) _loaddata{    nsstring * path=[[nsbundle mai Nbundle] pathforresource:@ "musics.plist" oftype:nil];    Nsarray * Array=[nsmutablearray Arraywithcontentsoffile:path];        For (Nsdictionary * dic in array) {        jrmusic *music=[jrmusic getmusicbydic:dic];        [Self.dataarray addobject:music];}    }

As follows:

4, click to jump, load lyrics, Here we need to parse the LRC lyrics and parse the LRC lyrics into a dictionary with the lyrics value in seconds as key and put in the array.

#pragma mark-Format the array for time as a dictionary-(Nsarray *) _makedicwitharray: (Nsarray *) array {Self.allkeys=[nsmutablearray array];  Self.dataarray=[nsmutablearray array];for (NSString * str in array) {if (str.length) {Nsinteger location=[str            rangeofstring:@ "]"].location;                        NSString * Front=[str substringtoindex:location+1];            1 convert the first half into seconds nsmutablestring * fronttemp=[nsmutablestring Stringwithstring:front]; [Fronttemp replaceoccurrencesofstring:@ "[" withstring:@ "" Options:nscaseinsensitivesearch range:NSMakeRange (0,            Fronttemp.length)]; [Fronttemp replaceoccurrencesofstring:@ "]" withstring:@ "" Options:nscaseinsensitivesearch range:NSMakeRange (0,            Fronttemp.length)];            Nsarray * temarray=[fronttemp componentsseparatedbystring:@ ":"];            CGFloat Key=[[temarray Firstobject] Floatvalue]*60+[[temarray lastobject] floatvalue];                        [Self.allkeys addobject:@ (key)]; 2 intercept the second half of NSstring * Behind=[str substringfromindex:location+1];        [Self.dataarray Addobject:behind];    }} for (int i=0;i<10;i++) {[Self.dataarray addobject:@ "];    } return nil; }

5,   scrolls according to the music's playback time control text, When the music is playing, set a timer, according to the time of the timer to get the current lyrics of the key, through key can get the current lyrics, through the table scrolling way, the current lyrics displayed on the panel to

Control lyrics scrolling-(void) show{Nsinteger index= [self getnowrows];        Nsindexpath * Path=[nsindexpath Indexpathforrow:index insection:0];      [Self.tableview Scrolltorowatindexpath:path atscrollposition:uitableviewscrollpositionmiddle Animated:YES];        UITableViewCell * Cell=[self.tableview Cellforrowatindexpath:path];        for (UITableViewCell * C in Self.allcell) {C.textlabel.textcolor=[uicolor Whitecolor];    C.textlabel.font=[uifont Systemfontofsize:16];    } Cell.textlabel.textcolor=[uicolor Greencolor];    Cell.textlabel.font=[uifont Boldsystemfontofsize:18];        [Cell.textlabel Adjustsfontsizetofitwidth]; Set time change if (Count%2==0&&[player isplaying]) {NSString * times=[self GetFomatByTime:player.currentTime]        ;        Self.progress.starttime.text=times;    Progress +1 self.progress.slider.value++;        } count++;        End Loop if (player.duration-player.currenttime<1) {[Timer invalidate];        Timer=nil; Self.Progress.starttime.text=[self getfomatbytime:[player Duration]]; }  }

The effect is as follows:

6, add the button information, through the button to control the previous song, the Next and pause. This work is done through Xib,

7, through the proxy method to achieve song switching

#pragma mark-changesongsdelegate-(Jrmusic *) Chagesongswithflag: (nsinteger) flag{    jrmusic * MUSIC=NIL;// If it is the previous page or the next page must meet not the first and last    if ((flag==2&&self.front.row==self.dataarray.count-1) | | (flag==1&&self.front.row==0)) {        return self.dataarray[self.front.row];    }   Reopen    if (flag==2) {         music=self.dataarray[self.front.row+1];         Self.front=[nsindexpath indexpathforrow:self.front.row+1 insection:0];    } else{         music=self.dataarray[self.front.row-1];         Self.front=[nsindexpath indexpathforrow:self.front.row-1 insection:0];    }        The outside list shows Red    [self clear];    Jrtableviewcell *cell= (Jrtableviewcell *) [Self.tableview CellForRowAtIndexPath:self.front];    Cell.titlelabel.textcolor=[uicolor Redcolor];     return music;}

To learn more about the small partners, you can click to view the source code , run the test yourself.

Inquiries or technical exchanges, please join the official QQ Group: (452379712)

Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

iOS implements multimedia audio music player

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.