Simple implementation of the iOS-based music player, and with lyrics, with the play automatically scrolling, the effect is as follows:
First, it is necessary to establish a class ZMPLRC to parse the lyrics, which is mainly to separate the time and the corresponding lyrics, and then store them in the array.
ZMPlrc.h
#import <Foundation/Foundation.h> @interface zmplrc:nsobject/** time */@property (Nonatomic,strong) Nsmutablearray *timearray;/** Lyrics */@property (Nonatomic,strong) nsmutablearray *wordarray;/** parsing lyrics */-(void) parselrc;@ End
Zmplrc.m
#import "ZMPlrc.h" @implementation zmplrc-(instancetype) init{self = [super init]; if (self) { _timearray = [Nsmutablearray array]; _wordarray = [Nsmutablearray array]; } return self;} /** Lyrics Path */-(NSString *) getlrcpath{ return [[NSBundle mainbundle] pathforresource:@ "Fish leong-Occasional showers" oftype:@ "LRC"];} /** parsing lyrics */-(void) parselrc{ nsstring *content = [NSString stringwithcontentsoffile:[self getlrcpath] Encoding: Nsutf8stringencoding Error:nil]; Nsarray *separray = [Content componentsseparatedbystring:@ "["]; for (int i = 5; i < separray.count; i + +) { //There are two elements, one is time, one is lyrics nsarray *arr = [Separray[i] Componentsseparat edbystring:@ "]"; NSLog (@ "%@", Separray[i]); [_timearray Addobject:arr[0]]; [_wordarray addobject:arr[1]; } NSLog (@ "%@", content);} @end
Next, add the necessary controls in the storyboard.
Finally, the controller can be implemented in the Zmpviewcontroller, in the implementation process, the main is to listen to the playback of the audio player player time, with the previous resolution of each lyrics corresponding time to deal with.
ZMPViewController.h
#import <UIKit/UIKit.h> @interface zmpviewcontroller:uiviewcontroller@property (weak, nonatomic) Iboutlet UITableView *lrctableview; @property (weak, nonatomic) Iboutlet uislider *timeslider; @property (weak, nonatomic) Iboutlet UILabel *currenttimelabel; @property (weak, nonatomic) Iboutlet UILabel *totaltimelabel; @property (Weak, nonatomic) Iboutlet UIButton *lastmusicbtnclick;-(ibaction) Playbtnclick: (UIButton *) sender;-(ibaction) Premusicbtnclick: (ID) sender;-(ibaction) ValueChange: (UISlider *) sender;-(ibaction) Nextmusicbtnclick: (ID) sender;@ End
Zmpviewcontroller.m
#import "ZMPViewController.h" #import <AVFoundation/AVFoundation.h> #import "ZMPlrc.h" @interface Zmpviewcontroller () <uitableviewdatasource,uitableviewdelegate>{//music player Avaudioplayer *player; ZMPLRC *LRC; Nsinteger CurrentRow;} @end @implementation zmpviewcontroller-(void) viewdidload{[Super viewdidload];//do no additional setup after loading The view, typically from a nib. [Self initplayer]; Listen current time [Nstimer scheduledtimerwithtimeinterval:0.5 target:self selector: @selector (updatetime) Userinfo:nil repeats : YES]; LRC = [[ZMPLRC alloc] init]; [LRC PARSELRC]; [Self.lrctableview Registerclass:[uitableviewcell class] forcellreuseidentifier:@ "CELL"]; [Self.lrctableview Reloaddata]; }-(void) updatetime{cgfloat currenttime = player.currenttime; Self.currentTimeLabel.text = [NSString stringwithformat:@ "%02d:%02d", (int) currenttime/60, (int) currenttime% 60]; Self.timeSlider.value = currenttime/ Player.duration; for (int i = 0; i < lrc.timeArray.count; i + +) {Nsarray *arr = [Lrc.timearray[i] Componentsseparatedbys tring:@ ":"]; CGFloat comptime = [arr[0] integervalue]*60 + [arr[1] floatvalue]; if (Player.currenttime > Comptime) {currentrow = i; } else {break; }} [Self.lrctableview Reloaddata]; [Self.lrctableview scrolltorowatindexpath:[nsindexpath Indexpathforrow:currentrow inSection:0] AtScrollPosition: Uitableviewscrollpositionmiddle Animated:yes];} /** Initialize music player */-(void) initplayer{//Background playback audio settings avaudiosession *session = [Avaudiosession sharedinstance]; [Session Setactive:yes Error:nil]; [Session Setcategory:avaudiosessioncategoryplayback Error:nil]; Let app support accept remote control events [[UIApplication sharedapplication] beginreceivingremotecontrolevents]; NSString *path = [[NSBundle mainbundle] pathforresource:@ "Fish leong-Occasional shower" oftype:@ "mP3 "]; Nsurl *url = [Nsurl Fileurlwithpath:path]; Player = [[Avaudioplayer alloc] Initwithcontentsofurl:url Error:nil]; Channel-1------left channel, 1-----right channel Player.pan = 0; Volume: 0~1 player.volume = 1; Single loop (negative number means single loop) Player.numberofloops = 1; Rate (default is 1)//player.enablerate = YES; Player.rate = 1.0; Total time CGFloat totalseconds = player.duration; Self.totalTimeLabel.text = [NSString stringwithformat:@ "%02d:%02d", (int) totalseconds/60, (int) totalseconds% 60]; Current time player.currenttime; #if 0//play [player play]; Stop [player stop]; Pause [player pause], #endif}-(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(Ibaction) Playbtnclick: (UIButton *) Sender {sender.selected =!sender.selected; if (sender.selected) {[Player preparetoplay]; [Player play]; }else{[player pause]; }}-(IBAction) Premusicbtnclick: (ID) Sender {[self initplayer];} -(Ibaction) ValueChange: (UISlider *) Sender {player.currenttime = player.duration * sender.value;} -(Ibaction) Nextmusicbtnclick: (ID) Sender {[self initplayer];} #pragma mark-tableviewdelegate-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{return lrc.wordArray.count;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Cell"]; if (Indexpath.row = = CurrentRow) {cell.textLabel.textColor = [uicolor Redcolor]; } else {cell.textLabel.textColor = [uicolor blackcolor]; } cell.textLabel.textAlignment = Nstextalignmentcenter; Cell.textLabel.font = [Uifont systemfontofsize:15]; Cell.textLabel.text = Lrc.wordarray[indexpath.row]; return cell;} @end
Need music after the program into the background can also play, then in the plist file, add required background modes set the value of item 0 for app plays audio or streams audio/video using AirPlay
And in the controller, add settings for background playback audio
Background playback audio settings avaudiosession *session = [avaudiosession sharedinstance]; [Session Setactive:yes Error:nil]; [Session Setcategory:avaudiosessioncategoryplayback Error:nil]; Let app support accept remote control Events [[UIApplication sharedapplication] beginreceivingremotecontrolevents];
In addition, you need a demo can leave a message.
iOS music player (lyrics auto scroll)