iOS Development Outreach-Audio processing (music player 5)

Source: Internet
Author: User

iOS Development Outreach-Audio processing (music player 5)

Implementation results:

One, the translucent slider settings

 1/** 2 * Drag the slider 3 */4-(Ibaction) Panslider: (Uipangesturerecognizer *) Sender {5 6//1. Get moved distance 7 cgpoint t= [Sender TranslationInView:sender.view]; 8//move to clear 9 [sender Settranslation:cgpointzero inview:sender.view];10 11//2. Control slider and progress bar Frame12 CGFloat SLIDERMAXX=SELF.VIEW.WIDTH-SELF.SLIDER.WIDTH;13 self.slider.x+=t.x;14//Control slider frame, do not let it out of bounds if (self.slider.x&lt ; 0) self.slider.x=0;18}else if (Self.slider.x>slidermaxx), {Self.slider.x=slider MAXX;21}22//Set the width of the progress bar at self.progressview.width=self.slider.center.x;24 25//3. Set time value-Double Pro     GRESS=SELF.SLIDER.X/SLIDERMAXX;27//Current time value = Duration of the music * Current progress value Nstimeinterval time=self.player.duration*progress;29 [Self.slider settitle:[self Strwithtime:time] forstate:uicontrolstatenormal];30 31//Set the value of the drag progress x Self.curren ttimeview.x=self.slider.x;33 [Self.currenttimeview setTitle:self.slider.currentTitle Forstate:uicontroLSTATENORMAL];34 35//4. If you start dragging, then stop the timer if (Sender.state==uigesturerecognizerstatebegan) {37//Stop Timer [Self removecurrenttime];39 40//Set Drag Progress 41//Display Self.currenttimeview.hidden=no ; self.currenttimeview.y=self.currenttimeview.superview.height-5-self.currenttimeview.height;44 45}         else if (sender.state==uigesturerecognizerstateended) 46 {47//Hide self.currenttimeview.hidden=yes;49 Set the player playback time self.player.currenttime=time;51 #warning If you are playing, you need to add a timer, Self.player.isPlayi NG) {53//open Timer [self addcurrenttimetimer];55//}56}57}

Trimming the detail of rounded corners:

  

Second, play or pause, the previous song, the next song implementation

 1//Previous 2-(Ibaction) Previous {3//1. Disable all App Click events before starting playback 4 UIWindow *window=[[uiapplication sharedapplication] . Windows Lastobject]; 5 Window.userinteractionenabled=no; 6 7//2. Reset the current song 8 [self resetplayingmusic]; 9 10//3. Get previous song each [Yymusictool setplayingmusic:[yymusictool previousmusic]];12 13//4. Play previous Song 14 [ Self starplayingmusic];15 16//5. Reply Window click to available window.userinteractionenabled=yes;18}19//Next First-(ibaction     Next {21//1. Disable all App Click events before starting playback UIWindow *window=[[uiapplication sharedapplication].windows lastobject];23     Window.userinteractionenabled=no;24 25//2. Resets the current song to [self resetplayingmusic];27 28//3. Get the next song 29     [Yymusictool setplayingmusic:[yymusictool nextmusic]];30 31//4. Play next song [Self starplayingmusic];33 34 5. Reply to Window's Click to Available window.userinteractionenabled=yes;36}37 38//Resume or pause play-(ibaction) playorpause {(SE Lf.playOrPauseButton.isSelecTed) {//Pause self.playorpausebutton.selected=no;42//Pause play [Yyaudiotool pauseMusic:self.playingMu SIC.FILENAME];44//Stop timer [self removecurrenttime];46}else47 {Self.playorpausebutton. selected=yes;49//Continue playing [Yyaudiotool playmusic:self.playingmusic.filename];51//Turn on Timer 52 [ Self addcurrenttimetimer];53}54}
Description: The picture settings for the play and pause buttons are not the same in both states, setting the status of the play button third, the existence of the bug to improveDrag-and-drop problems (timer problems) a better way to do finer control where timers are added:
1/** 2  *  Add a Timer 3 *  /4-(void) Addcurrenttimetimer 5 {6     ///If it is not currently playing, then return directly 7 if     ( Self.player.isplaying==no) return; 8      9     //before adding a timer, remove the previous timer [self removecurrenttime];11//     First call a progress update earlier, In order to ensure that the timer works in a timely manner     [self updatecurrenttime];14     //Create a timer, every second to invoke the self     . Currenttimetimer=[nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (updatecurrenttime) Userinfo:nil repeats:yes];17     //The timer is added to the runtime     [[Nsrunloop mainrunloop]addtimer:self. Currenttimetimer formode:nsrunloopcommonmodes];19}

Iv. Supplementary

The complete code is as follows:

  1//2//YYPLAYINGVIEWCONTROLLER.M 3//20-Audio processing (music Player 1) 4//5//Created by Apple on 14-8-13. 6//Copyright (c) 2014 Yangyong.  All rights reserved. 7//8 9 #import "YYPlayingViewController.h" #import "YYMusicTool.h" #import "YYMusicModel.h" #import "Yyaudio Tool.h "@interface Yyplayingviewcontroller () 15//Display drag-and-drop progress @property (weak, nonatomic) Iboutlet UIButton *current Timeview; 17//progress bar @property (weak, nonatomic) Iboutlet UIView *progressview; 19//Slide block @property (weak, nonatomic) Iboutlet UIButton *slider; @property (Weak, nonatomic) Iboutlet Uiimageview *iconview; @property (Weak, nonatomic) Iboutlet UILabel *songlabel; @property (Weak, nonatomic) Iboutlet UILabel *singerlabel; 24//Current playing music duration @property (weak, nonatomic) Iboutlet UILabel *durationlabel; 26//Music being played @property (Nonatomic,strong) Yymusicmodel *playingmusic; 28//Music Player Object @property (Nonatomic,strong) Avaudioplayer *player; 30//Timer @property (Nonatomic,strong) nstiMer *currenttimetimer; -(ibaction) exit; -(Ibaction) TAPPROGRESSBG: (UITapGestureRecognizer *) sender; -(Ibaction) Panslider: (Uipangesturerecognizer *) sender; -(ibaction) previous; -(ibaction) Playorpause; PNS-(ibaction) next; @property (Weak, nonatomic) Iboutlet UIButton *playorpausebutton;      @end @implementation Yyplayingviewcontroller (void) viewdidload {[Super viewdidload]; 47 48//cropped fillet self.currenttimeview.layer.cornerradius=8; The #pragma mark-public method-(void) Show 54 {55//1. Disable click events for the entire app. UIWindow *window=[uiapplication Share Dapplication].keywindow; Window.userinteractionenabled=no; 58 59//2. Add Playback interface 60//Set the view size to cover the entire window self.view.frame=window.bounds; 62//Set view display self.view.hidden=no; 64//Add view to the window. [Window AddSubview:self.view]; 66 67//3. Detect if the song is changed if (Self.playingmusic!=[yymusictool Playingmusic]) {[Self RESETPLAYINGMUsic]; 70} 71 72//4. Use animations to show the self.view.y=self.view.height of the view;          [UIView animatewithduration:0.25 animations:^{self.view.y=0; completion:^ (BOOL finished) {77 78//Set music data [self starplayingmusic]; Window.userinteractionenabled=yes; 81}]; "The #pragma mark-private method 86//Reset the music being played-(void) Resetplayingmusic 88 {89//1. Reset Interface Data self.iconview.i Mage=[uiimage imagenamed:@ "PLAY_COVER_PIC_BG"]; Self.songlabel.text=nil; Self.singerlabel.text=nil; 93 94//2. Stop play [Yyaudiotool stopMusic:self.playingMusic.filename]; 96//The player is emptied by the Self.player=nil; 98 99//3. Stop timer [self removecurrenttime];101 102//4. Set the status of the Music playback button 103 Self.playOrPauseButton.sel ected=no;104}105//start playing Music data 106-(void) starPlayingMusic107 {108//1. Set interface Data 109 110//If the music currently playing is the incoming music, then return directly 11 1 if (Self.playingmusic==[yymusictool Playingmusic]) 112 {113//Add the timer to the [self addcurrenttimetimer];115 return;116}117//Access Music 118 Self.playingmu Sic=[yymusictool playingmusic];119 self.iconview.image=[uiimage imagenamed:self.playingmusic.icon];120 self.songLa bel.text=self.playingmusic.name;121 self.singerlabel.text=self.playingmusic.singer;122 123//2. Start playing 124 sel  F.player = [Yyaudiotool playmusic:self.playingmusic.filename];125 126//3. Set length 127//self.player.duration;     The length of time that the player is playing the music file Self.durationlabel.text=[self strwithtime:self.player.duration];129 130//4. Adding Timers 131  [Self addcurrenttimetimer];132 133//5. Set the status of the Music playback button 134 self.playorpausebutton.selected=yes;135}136 137/**138 * Put the length of time--time string 139 */140-(NSString *) Strwithtime: (nstimeinterval) time141 {142 int minute=time/60;143 int SE cond= (int) time% 60;144 return [nsstring stringwithformat:@ "%d:%d", minute,second];145}146 147 #pragma mark-Timer Control 148/ **149 * Add a timer 150 */151 -(void) addCurrentTimeTimer152 {153//if not currently playing, then return directly 154 if (Self.player.isplaying==no) return;155 156/ /before adding a timer, remove the previous timer 157 [self removecurrenttime];158 159//First call a progress update to ensure that the timer works in a timely manner [self updatecurr ENTTIME];161 162//Create a timer that calls 163 self every second. Currenttimetimer=[nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (updatecurrenttime) Userinfo:nil repeats:yes];164//Add the timer to the runtime 165 [[Nsrunloop mainrunloop]addtimer:self. Currenttimetimer formode:nsrunloopcommonmodes];166}167/**168 * Remove a Timer 169 */170-(void) removeCurrentTime171 {172 [s Elf. Currenttimetimer invalidate];173 174//Empty the timer 175 self.  currenttimetimer=nil;176}177 178/**179 * Update playback Progress */181-(void) updateCurrentTime182 {183//1. Calculate Progress Value 184 double progress=self.player.currenttime/self.player.duration;185 186//2. Calculate the x value of the slider 187//The maximum x value of the slider 188 cgfloat slide rmaxx=self.view.width-self.slider.width;189 Self.slider.x=slidermaxx*progress;190//Set the current playback time on the slider 191 [self.slider settitle:[self StrWithTime:self.player.currentTime] ForSt ate:uicontrolstatenormal];192 193//3. Set the width of the progress bar 194 self.progressview.width=self.slider.center.x;195 196}19      7 198 #pragma mark-Internal button monitoring method 199//Return button-(ibaction) exit {201 202//0. Remove Timer 203 [self removecurrenttime];204 1. Disable click events for the entire app 205 UIWindow *window=[uiapplication sharedapplication].keywindow;206 Window.userinteractionen abled=no;207 208//2. Animation hidden View209 [UIView animatewithduration:0.25 animations:^{210 Self.view.y=window. height;211} completion:^ (BOOL finished) {212 window.userinteractionenabled=yes;213//Set view hide to save some performance 214 self.view.hidden=yes;215}];216}217 218/**219 * Click on progress bar */221-(ibaction) TAPPROGRESSBG: (uitapgesture     Recognizer *) Sender {222//Get the currently clicked Point 223 cgpoint point=[sender locationinview:sender.view];224//Toggle song's current play Time 225 Self.player.currentTIme= (point.x/sender.view.width) *self.player.duration;226//Update playback progress 227 [self updatecurrenttime];228}229/**230 * Drag Slider 231 */232-(ibaction) Panslider: (Uipangesturerecognizer *) Sender {233 234//1. Get moving distance 235 cgpoint T=[sender     translationinview:sender.view];236//Nudge 237 [Sender Settranslation:cgpointzero inview:sender.view];238 239 2. Control slider and progress bar frame240 cgfloat slidermaxx=self.view.width-self.slider.width;241 self.slider.x+=t.x;242//control slip Block frame, do not let it cross 243 if (self.slider.x<0) 244 {245 self.slider.x=0;246}else if (self.slider.x>sliderm AxX) 247 {248 self.slider.x=slidermaxx;249}250//Set the width of the progress bar 251 SELF.PROGRESSVIEW.WIDTH=SELF.SLIDER.C enter.x;252 253//3. Set the time value 254 double progress=self.slider.x/slidermaxx;255//Current time value = Duration of the music * Current progress value Imeinterval time=self.player.duration*progress;257 [Self.slider settitle:[self strwithtime:time] ForState:   uicontrolstatenormal];258 259  Sets the value of the X for the drag progress 260 self.currenttimeview.x=self.slider.x;261 [Self.currenttimeview setTitle:self.slider.currentTitle forstate:uicontrolstatenormal];262 263//4. If you start dragging, stop the timer (Sender.state==uigesturerecognizerstatebegan {265//stop timer 266 [self removecurrenttime];267 268//Set Drag Progress 269//Show SEL f.currenttimeview.hidden=no;271 self.currenttimeview.y= self.currenttimeview.superview.height-5-self.currenttimeview.height;272 273}else if (sender.state==UIGestureRe         cognizerstateended) 274 {275//Hide 276 self.currenttimeview.hidden=yes;277//Set player playback time 278 self.player.currenttime=time;279 #warning If you are playing, you need to add a timer 280//if (self.player.isPlaying) {281//Turn on Timer 28 2 [Self addcurrenttimetimer];283//}284}285}286 287//Previous 288-(Ibaction) Previous {289//1. Starting playback Before, disable everything in the app click event 290 UIWindow *window=[[uiapplication sharedapplication].windowslastobject];291 window.userinteractionenabled=no;292 293//2. Resetting the current song 294 [self resetplayingmusic];295 29 6//3. Get previous song 297 [Yymusictool setplayingmusic:[yymusictool previousmusic]];298 299//4. Play previous song [Sel F starplayingmusic];301 302//5. Click on Reply window for available 303 window.userinteractionenabled=yes;304}305//Next 306-(ibact Ion) Next {307//1. Disable all App Click events before starting playback 308 UIWindow *window=[[uiapplication sharedapplication].windows Lastobject]; 309 window.userinteractionenabled=no;310 311//2. Reset the current song 312 [Self resetplayingmusic];313 314//3. Get Next song 315 [Yymusictool setplayingmusic:[yymusictool nextmusic]];316 317//4. Play next song 318 [self Starplayingmus ic];319 320//5. Reply window click for available 321 window.userinteractionenabled=yes;322}323//Resume or pause playback 324-(ibaction) playorpa Use {325 if (self.playOrPauseButton.isSelected) {//Pause 326 self.playorpausebutton.selected=no;327//Pause Playback 328 [Yyaudiotool pausemusic:self.playingmusic.filename];329//Stop timer [self removecurrenttime];331}else332 {333] self.playorpausebutton.selected=yes;334//Continue playing 335 [Yyaudiotool PlayMusic:self.playingMusic.filenam e];336//Turn on timer 337 [self addcurrenttimetimer];338}339}340 341 342 @end

iOS Development Outreach-Audio processing (music player 5)

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.