IOS Avaudioplayer Simple MP3 Playback

Source: Internet
Author: User

- (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

Related Article

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.