The sound is relatively short, the music is longer, the format is more, it needs to use the <AVFoundation/AVFoundation.h> framework, Avaudioplayer only support the local
viewcontroller.m//avfoundation////Created by city--online on 15/5/5.//Copyright (c) 2015 CYW. All rights reserved.//#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface Viewcontroller () <AVAudioPlayerDelegate> @property (nonatomic,strong) Avaudioplayer *audioplayer; @property ( Nonatomic,strong) UIButton *btnstop, @property (nonatomic,strong) UIButton *btnpause; @property (Nonatomic,strong) UIButton *btnplayer; @property (Nonatomic,strong) Uiprogressview *progress; @property (Nonatomic,strong) Uiprogressview *averagepowerprogress, @property (nonatomic,weak) Nstimer *timer; @end @implementation ViewController-( void) Viewdidload {[Super viewdidload]; [Self stepui]; NSString *path=[[nsbundle mainbundle]pathforresource:@ "1" oftype:@ ". mp3"]; Nsurl *url=[[nsurl Alloc]initfileurlwithpath:path]; _audioplayer=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil]; Stereo Balance-1 left channel 0 channel Balance 1 right channel _audioplayer.pan=1.0; Volume (0-1) _audioplayer.volume=0.8; Whether the rate _audioplayer.enablerate=yes is allowed to change; Playback rate (0.5-2) 1.0 normal play _audioplayer.rate=0.8; Whether to enable audio measurement by default no _audioplayer.meteringenabled=true; Number of cycles _audioplayer.numberofloops=3; Set proxy _audioplayer.delegate=self; }-(void) stepui{//progress bar _progress=[[uiprogressview alloc]initwithframe:cgrectmake (100, 100, 200, 10)]; [Self.view addsubview:_progress]; _averagepowerprogress=[[uiprogressview Alloc]initwithframe:cgrectmake (100, 200, 100, 10)]; [Self.view addsubview:_averagepowerprogress]; Stop playing _btnstop=[uibutton Buttonwithtype:uibuttontypesystem]; [_btnstop settitle:@ "Stop" forstate:uicontrolstatenormal]; _btnstop.frame=cgrectmake (40, 150, 40, 40); _btnstop.tag=1001; [_btnstop addtarget:self Action: @selector (Operation:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:_btnstop]; Play _btnplayer=[uibutton Buttonwithtype:uibuttontypesystem]; [_btnplayer settitle:@ "Play" forsTate:uicontrolstatenormal]; _btnplayer.frame=cgrectmake (90, 150, 40, 40); _btnplayer.tag=1002; [_btnplayer addtarget:self Action: @selector (Operation:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:_btnplayer]; _btnpause=[uibutton Buttonwithtype:uibuttontypesystem]; [_btnpause settitle:@ "pause" forstate:uicontrolstatenormal]; _btnpause.frame=cgrectmake (140, 150, 40, 40); _btnpause.tag=1003; [_btnpause addtarget:self Action: @selector (Operation:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:_btnpause]; _timer=[nstimer scheduledtimerwithtimeinterval:0.2 target:self selector: @selector (updateprogress) Userinfo:nil Repeats:yes]; }-(void) operation: (ID) sender{UIButton *btn= (UIButton *) sender; Switch (btn.tag) {case 1001:if ([_audioplayer isplaying]) {[_audioplayer stop]; [_progress setprogress:0]; _timer.firedate=[nsdate Distantfuture]; _audioplayer.currenttime=0; NSLog (@ "%ld", _audioplayer.numberofchannels); } break; Case 1002:if (![ _audioplayer isplaying]) {[_audioplayer play]; Recovery timing _timer.firedate=[nsdate Distantpast]; } break; Case 1003:if ([_audioplayer isplaying]) {[_audioplayer pause]; Pause the timer, note that the Invalidate method cannot be called, this method will be canceled and cannot be resumed after _timer.firedate=[nsdate Distantfuture]; } break; Default:break; }}-(void) updateprogress{float progress=_audioplayer.currenttime/_audioplayer.duration; [_progress setprogress:progress Animated:yes]; Update the audio measurements, note that if you want to update the audio measurements, you must set meteringenabled to Yes, and you can instantly get audio decibel information via audio measurements [_audioplayer updatemeters]; To get the decibel peak of the specified channel, note that if you want to get a decibel spike, you must call the Updatemeters method/float Peakpower=[_audioplayer Peakpowerforchannel:2];//NSLog (@ "%lf", peakpower); Gets the decibel average of the specified channel, note that if you want to obtain a decibel average, you must call the Updatemeters method before this float averagepower= [_audioplayer averagepowerforchannel:1]; Similar up and down fluctuations [_averagepowerprogress setprogress: (-averagepower)/50.0 Animated:yes]; NSLog (@ "%lf", averagepower); }-(void) dealloc{[[Nsnotificationcenter Defaultcenter] removeobserver:self name: Avaudiosessionroutechangenotification Object:nil];} -(void) audioplayerdidfinishplaying: (Avaudioplayer *) player successfully: (BOOL) flag; {NSLog (@ "play complete");} -(void) Audioplayerdecodeerrordidoccur: (Avaudioplayer *) Player ERROR: (Nserror *) error{NSLog (@ "%@", error);} -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Above is the progress bar, below is the decibel average, similar to the music player wave effect
Multimedia music Playback