iOS Development Outreach-audio processing (music player 6)

Source: Internet
Author: User
Tags set time
<span id="Label3"></p><p><p><strong>iOS Development Outreach-audio processing (music player 6)</strong></p></p><p><p><strong>first, image Processing</strong></p></p><p><p><strong></strong></p></p><p><p></p></p><p><p>Description</p></p>The aspect indicates that the scale is scaled according to the original aspect Ratio. Aspectfit said that according to the original aspect ratio scaling, required to see all the pictures, the result is not completely cover the window, will leave Blank. Aspectfill is scaled by the original aspect ratio, but only some of the pictures are Visible. Raised issue: there may be a portion of the screen that goes Out. so, If you select the Aspectfill mode, you need to cut out the picture, in the storyboard can also be set. The following two types of settings are Equivalent. (1) setting in storyboard (2) using code clipping<strong><strong>second, Playback Processing</strong></strong>1. After the current song has finished playing, continue playing back the song Solution: become the agent of the Player.  Listen for playback of the Player. 2. Playback Interrupt Processing<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 #pragma mark-music player Agent 2//player will call this method 3-(void) audioplayerdidfinishplaying: (avaudioplayer *) player successfully :(BOOL) Flag 4 {5 [self next]; 6} 7//when The player encounters an interrupt (such as a call), call the method 8-(void) audioplayerbegininterruption: (avaudioplayer *) Player 9 {ten if (self.player.isPlaying) {one ////if currently playing, then pause [self playorpause];13 }14}15// Call the following method after the end of the interrupt event (void) audioplayerendinterruption: (avaudioplayer *) player withoptions: (nsuinteger) flags17 {18 //can do nothing, Let the user decide whether to continue playback or pause 19}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>3. Turn on background task description: new Issue-when the playback task goes into the background, the music playback will stop. Solution:<p><p>The code in the YYAPPDELEGATE.M file is handled as Follows:</p></p>Get more chances: Tell it is a music play Task 4. It is best to also process it in the Audio playback tool class.<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 #import "YYAudioTool.h" 2 3 @implementation yyaudiotool 4 + (void) Initialize 5 {6 //audio session 7 Avaudiosession *s Ession =[avaudiosession sharedinstance]; 8 //set Painting type (play type, play mode, will automatically stop other music Playback) 9 [session setcategory:avaudiosessioncategorysoloambient error:nil];10 //activation session One by one [session Setactive:yes error:nil];12}13//....</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p><strong>Iii. Creating a Lyrics control</strong></p></p>Create lyrics Control lyrics format Description: play the Effect:<br>The realization of the effect of the wool glass (1) let the artist provide a translucent picture (2) using a bunch of graphic algorithms to generate the UIImage object of the frosted glass style here a third-party framework is used to produce frosted glass: English Blur third party framework: Drnrealtimeblur Concrete Implementation: Create a new class and let it inherit from it, you can achieve the effect of frosted glass.<p><p>  </p></p><p><p>Add a lyrics control to Xib.</p></p><p><p>Note the level relationship of the lyrics control, exit and the word figure two buttons should be on top of the lyrics control, so that you can click to Toggle.</p></p><p><p></p></p><p><p>Associates the control with the newly created class.</p></p><p><p>  </p></p><p><p>Add a constraint and clear its background color. Not displayed by default (set hidden)</p></p><p><p>  </p></p><p><p>The effect of frosted glass is as follows:</p></p><p><p>  </p></p><p><p></p></p><p><p>The simple code is handled as Follows:</p></p><p><p>YYLRCVIEW.M file</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1//2//YYLRCVIEW.M 3//24-audio processing (music player 5) 4//5//Created by Apple on 14-8-15. 6//Copyright (c) 2014 Yangyong. All Rights Reserved. 7//8 9 #import "YYLrcView.h" @interface yylrcview () <uitableviewdatasource,uitableviewdelegate>12 @property (nonatomic,strong) UITableView *tableview;13 @end14 @implementation YYLrcView15-(id) initwithframe: (cgrect) frame17 {self = [super] initwithframe:frame];19 if (self) {[self setup];21}22 return self;23}24-(id) initwithcoder: (NS Coder *) aDecoder26 {self=[super initwithcoder:adecoder];28 if (self) {"self setup];30}31 re Turn self;32}33-(void) setup35 {36//add Table control PNS UITableView *tableview=[[uitableview alloc]init];38 Tablevie w.delegate=self;39 tableview.datasource=self;40 [self addsubview:tableview];41 self.tableview=tableview;42}43 #pragma mark-public method (void) setlrcname: (nsstring *) lrcname46 {_lrcname=[lrcname copy];48}49 #pRagma Mark-data Source Method #warning TODO52 @end </pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Code description:</p></p><p><p>Note: do not assume that only the controller can act as a TableView data source and Proxy. This is why the proxy and data source properties are of the type id, and their proxies and data sources are respected by their protocols.</p></p><p><p>-(id) Initwithcoder:. Called when reading an object from a file, it is recommended to invoke the initialized code in two methods for the rigor of the Program.</p></p><p><p>Call this method to indicate that the object was parsed from the File.</p></p><p><p>If the object is created through code alloc\init, then Call-(id) initwithframe: method.</p></p><p><p>Description: the essence of the Xib file is an XML file.</p></p><p><p><strong>Iv. code Additions to the master controller</strong></p></p><p><p>YYPLAYINGVIEWCONTROLLER.M file</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1//2//YYPLAYINGVIEWCONTROLLER.M 3//4 5 #import "YYPlayingViewController.h" 6 #import "YYMusicTool.h" 7 #im Port "YYMusicModel.h" 8 #import "YYAudioTool.h" 9 #import "YYLrcView.h" @interface Yyplayingviewcontroller () < Avaudioplayerdelegate>-(ibaction) lyricorpic: (UIButton *) sender; @property (weak, nonatomic) iboutlet yylrcview *lrcview; 14//display Drag Progress @property (weak, Nonatomic) Iboutlet UIButton *currenttimeview; 16//progress Bar @property (weak, nonatomic) iboutlet UIView *progressview; 18//sliding 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; 23//music currently playing time of the @property (weak, nonatomic) iboutlet UILabel *durationlabel; 25//playing Music @property (nonatomic,strong) Yymusicmodel *playingmusic; 27//music Player Object @property (nonatomic,strong) Avaudioplayer *player; 29//fixedTime Device @property (nonatomic,strong) Nstimer *currenttimetimer; -(ibaction) exit; -(ibaction) tapprogressbg: (uitapgesturerecognizer *) sender; -(ibaction) panslider: (uipangesturerecognizer *) sender; previous-(ibaction); -(ibaction) playorpause; -(ibaction) next; Panax Notoginseng @property (weak, nonatomic) iboutlet UIButton *playorpausebutton; @end @implementation yyplayingviewcontroller-(void) viewdidload [[super viewdidload]; 46 47//clipping Fillet self.currenttimeview.layer.cornerradius=8; The #pragma mark-public method-(void) Show 53 {54//1. Disable click events for the entire app UIWindow *window=[uiapplication share dapplication].keywindow; window.userinteractionenabled=no; 57 58//2. Add the Playback interface 59//set the view size to cover the entire window of self.view.frame=window.bounds; 61//set View Display self.view.hidden=no; 63//add view to the WINDOW. [window addSubview:self.view]; 65 66//3. Detect if the song is changed if (self.playingmusic!=[yymusictool PlayingmusIc]) {[self resetplayingmusic]; 69} 70 71//4. use animations to show the view self.view.y=self.view.height; [UIView animatewithduration:0.25 animations:^{self.view.y=0;} completion:^ (BOOL finished) {76 77//set Music data for [self starplayingmusic]; window.userinteractionenabled=yes; 80}]; bayi} the #pragma mark-private method 85//reset music being played (void) Resetplayingmusic 87 {88//1. Reset Interface Data self.iconview.i Mage=[uiimage imagenamed:@ "play_cover_pic_bg"]; self.songlabel.text=nil; self.singerlabel.text=nil; 92 93//2. Stop playing 94 [yyaudiotool stopMusic:self.playingMusic.filename]; 95//the player is emptied by self.player=nil; 97 98//3. Stop Timer [self removecurrenttime];100 101//4. Set the status of the Music playback button 102 Self.playOrPauseButton.sel Ected=no;103}104//start playing music data-(void) starPlayingMusic106 {107//1. set interface Data 108 109//if The music currently playing is the incoming music, then return directly 11 0 if (self.playingmusic==[Yymusictool playingmusic]) 111 {112//add timer to 113 [self addcurrenttimetimer];114 return;115 }116//access Music 117 Self.playingmusic=[yymusictool playingmusic];118 Self.iconview.image=[uiimage IMAGENAMED:SELF.P layingmusic.icon];119 self.songlabel.text=self.playingmusic.name;120 self.singerlabel.text= self.playingmusic.singer;121 122//2. start playing 123 Self.player = [yyaudiotool playMusic:self.playingMusic.filename]; 124 self.player.delegate=self;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//6. Set Lyrics 137 self.lrcview.lrcname=self.playingmusic.lrcname;138}139 140/**141 * Take the Time--time string 142 */143-(nsstring *) s Trwithtime: (nstimeinterval) time144 {145 int minute=time/60;146 int Second= (int) time% 60;147 return [nsstring stringwithformat:@ "%d:%d", minute,second];148}149 #pragma mark-timer control 151 /**152 * Add a Timer 153 */154-(void) addCurrentTimeTimer155 {156//if not currently playing, then return directly to 157 if (self.player.isplaying== NO) return;158 159//before Adding a timer, Remove the previous timer [self removecurrenttime];161 162//advance A progress update first to ensure the timer is working When timely 163 [self updatecurrenttime];164 165//create a timer, call 166 self every Second. Currenttimetimer=[nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (updatecurrenttime) Userinfo:nil Repeats:yes];167//add the timer to the runtime 168 [[nsrunloop mainrunloop]addtimer:self. Currenttimetimer formode:nsrunloopcommonmodes];169}170/**171 * Remove a timer 172 */173-(void) removeCurrentTime174 {175 [s Elf. Currenttimetimer invalidate];176 177//empty the timer 178 self. currenttimetimer=nil;179}180 181/**182 * Update playback Progress 183 */184-(void) updateCurrentTime185 {186//1. Calculate progress Value 187 double Progress=self.player.currenttime/self.player.duration;188 189//2. Calculate the x value of the slider 190//the maximum x value of the slider 191 cgfloat slidermaxx=self.view.width-self.slide r.width;192 Self.slider.x=slidermaxx*progress;193//set The current playback time on the slider 194 [self.slider settitle:[self StrWithTime:se lf.player.currentTime] forstate:uicontrolstatenormal];195 196//3. Set the width of the progress bar 197 Self.progressview.width=self.sli der.center.x;198 199}200 201 #pragma mark-internal button monitoring method 202//return button 203-(ibaction) exit {204 205//0. Remove Timer 206 [ Self removecurrenttime];207//1. Disable click events for the entire app 208 UIWindow *window=[uiapplication sharedapplication].keywindow;209 window.userinteractionenabled=no;210 211//2. animation hidden View212 [UIView animatewithduration:0.25 animations:^{213 self.view.y=window.height;214} completion:^ (BOOL finished) {215 window.userinteractionenabled=yes;21 6//set View Hide to save some performance 217 self.view.hidden=yes;218}];219 220}221 222/**223 * Click on progress bar 224 */225-( Ibaction) Tapprogressbg: (UITapGestureRecognizer *) Sender {226//get the currently clicked Point 227 cgpoint Point=[sender Locationinview:sender.view];228//toggle Song Current playback time 229 self.player.currenttime= (point.x/sender.view.width) *self.player.duration;230//update Playback Progress 231 [self upd atecurrenttime];232}233/**234 * Drag the slider 235 */236-(ibaction) panslider: (uipangesturerecognizer *) sender {237 238/ /1. get moved distance 239 cgpoint t=[sender translationinview:sender.view];240//shift 241 [sender Settranslation:cgpointze Ro inview:sender.view];242 243//2. control slider and progress bar frame244 CGFloat slidermaxx=self.view.width-self.slider.width;245 Self.slider.x+=t.x;246//control Slider frame, do not let it cross 247 if (self.slider.x<0) 248 {249 self.slider.x=0;250 }else if (self.slider.x>slidermaxx) 251 {252 self.slider.x=slidermaxx;253}254//set The width of the progress bar 255 s elf.progressview.width=self.slider.center.x;256 257//3. Set time value 258 double progress=self.slider.x/slidermaxx;259 Current time value = Duration of the music * Current progressValue 260 nstimeinterval time=self.player.duration*progress;261 [self.slider settitle:[self strWithTime:time] forState: uicontrolstatenormal];262 263//set The value of X for drag progress self.currenttimeview.x=self.slider.x;265 [self.currenttimevi EW SetTitle:self.slider.currentTitle forstate:uicontrolstatenormal];266 267//4. If you start dragging, Stop the timer 268 if (sender. State==uigesturerecognizerstatebegan) {269//stop Timer [self removecurrenttime];271 272//set Drag and drop Progress 273//display 274 self.currenttimeview.hidden=no;275 self.currenttimeview.y=self.currenttimeview.su perview.height-5-self.currenttimeview.height;276 277}else if (sender.state==uigesturerecognizerstateended) 278 {279//hide 280 Self.currenttimeview.hidden=yes;281//set player playback time 282 self.player.currentTi me=time;283 #warning If you are playing, you need to add a timer 284//if (self.player.isPlaying) {285//turn on timer 286 [self Addcurr enttimetimer];287// }288}289}290 291//previous 292-(ibaction) Previous {293//1. Disable all app Click events before starting playback 294 UIWindow *window=[[uiapp Lication sharedapplication].windows lastobject];295 window.userinteractionenabled=no;296 297//2. resetting the current song 298 [self resetplayingmusic];299 300//3. get previous song 301 [yymusictool Setplayingmusic:[yymusictool previousmusic]];302 303//4. Play the previous song 304 [self starplayingmusic];305 306//5. reply to Window's click as available 307 Window.userinteractionen Abled=yes;308}309//next 310-(ibaction) Next {311//1. Disable all app Click events before starting playback 312 UIWindow *window=[[uiapplication sha Redapplication].windows lastobject];313 window.userinteractionenabled=no;314 315//2. reset Current song [self reset] playingmusic];317 318//3. Get Next song 319 [yymusictool setplayingmusic:[yymusictool nextmusic]];320 321//4 . Play next Song 322 [self starplayingmusic];323 324//5. reply to Window's click as available 325 window.userinteractionenabled=yes;326}3 27 328//resume or pause playback 329-(IBaction) playorpause {self.playOrPauseButton.isSelected) {//pause 331 self.playorpausebutton.selected=no;33 2//pause Playback 333 [yyaudiotool pausemusic:self.playingmusic.filename];334//stop Timer 335 [self Remov ecurrenttime];336}else337 {338 self.playorpausebutton.selected=yes;339//continue Playback 340 [yyaudio Tool Playmusic:self.playingmusic.filename];341//open Timer 342 [self addcurrenttimetimer];343}344}345 346 #pragma mark-music player Agent 347//player will call this method after the player has finished playing 348-(void) audioplayerdidfinishplaying: (avaudioplayer *) player Successfully: (BOOL) flag349 {[self next];351}352///when The player encounters an interrupt (such as a call), call the method 353-(void) Audioplayerbegininterruption :(avaudioplayer *) player354 {355 if (self.player.isPlaying) {356//if currently playing, then pause 357 [self Playorpause ];358}359}360//interrupt event After the end of the call method 361-(void) audioplayerendinterruption: (avaudioplayer *) player Withoptions: ( Nsuinteger) flags362 {363//can do nothing to let the user decide whether to continue playing or pausing364}365-(ibaction) lyricorpic: (UIButton *) sender {366 if (self.lrcView.hidden) {367//display lyrics 368 SELF.L rcview.hidden=no;369 sender.selected=yes;370}else371 {372//hide lyrics, show singer Pictures 373 Self.lrcview.h idden=yes;374 sender.selected=no;375}376}377 @end</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p><span class="cnblogs_code_copy"></span></p></p><p><p>iOS Development Outreach-audio processing (music player 6)</p></p></span>

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.