- (void) viewdidload {[Super viewdidload]; UIButton*button =[UIButton Buttonwithtype:uibuttontyperoundedrect]; [Button Setframe:cgrectmake ( -, -, -, +)]; [Button Settitle:@"Play"Forstate:uicontrolstatenormal]; [Button addtarget:self action: @selector (play) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button]; UIButton*button1 =[UIButton Buttonwithtype:uibuttontyperoundedrect]; [Button1 Setframe:cgrectmake ( -, Max, -, +)]; [Button1 Settitle:@"Pause"Forstate:uicontrolstatenormal]; [Button1 addtarget:self Action: @selector (pause) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button1]; UIButton*button2 =[UIButton Buttonwithtype:uibuttontyperoundedrect]; [Button2 Setframe:cgrectmake ( -, $, -, +)]; [Button2 Settitle:@"Stop"Forstate:uicontrolstatenormal]; [Button2 addtarget:self Action: @selector (stop) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button2]; NSString*path = [[NSBundle mainbundle] Pathforresource:@"Mp3sample"OfType:@"mp3"]; Nsurl*url =[Nsurl Urlwithstring:path]; //each avaudioplayer can only play one music file, if it needs to play multiple times, you need to create multipleSelf.avaudiopalayer =[[Avaudioplayer Alloc]initwithcontentsofurl:url Error:nil]; Self.avaudiopalayer.Delegate=Self ; Self.avAudioPalayer.volume=1;//Initialize VolumeSelf.avAudioPalayer.numberOfLoops =-1;//set the number of music plays-1 to keep looping[Self.avaudiopalayer Preparetoplay];//Pre-playSelf.progressview= [[Uiprogressview alloc]initwithframe:cgrectmake ( -, -, $, -)]; [Self.view AddSubview:self.progressView]; Self.timer= [Nstimer scheduledtimerwithtimeinterval:0.1target:self selector: @selector (playprogress) Userinfo:nil Repeats:yes]; Self.slider= [[UISlider alloc]initwithframe:cgrectmake ( -, -, $, -)]; [Self.slider addtarget:self Action: @selector (Volumechange) Forcontrolevents:uicontroleventvaluech Anged]; Self.slider.minimumValue=1.0; Self.slider.maximumValue=10.0; Self.slider.value=5.0; [Self.view AddSubview:self.slider];}- (void) volumechange{Self.avAudioPalayer.volume=Self.slider.value;}- (void) playprogress{self.progressView.progress= self.avaudiopalayer.currenttime/self.avAudioPalayer.duration;}- (void) play{[Self.avaudiopalayer Play];}- (void) pause{[Self.avaudiopalayer pause];}- (void) stop{Self.avAudioPalayer.currentTime=0; [Self.avaudiopalayer stop];}- (void) Audioplayerdidfinishplaying: (Avaudioplayer *) player successfully: (BOOL) flag{[Self.timer invalidate];}
Allow effect
IOS Avaudioplayer Simple MP3 Playback