Today, take a look at Avaudioplayer, this player class Apple provides proxy methods that are used primarily to play local audio.
In fact, can also be used to play network audio, but it is the entire network file download down, in the actual development will be more expensive to do not recommend.
Here is the relevant code:
#import "ViewController.h"
Introducing Frames
#import <AVFoundation/AVFoundation.h>
@interface viewcontroller ()
<
Avaudioplayerdelegate
>
Set the global object, or there will be no sound to appear
@property (nonatomic,strong) avaudioplayer *audioplayer;
@property (nonatomic,strong) Nstimer *timer;
@property (nonatomic,strong) Nsarray *mp3array;
@property (nonatomic) nsinteger currentMp3;
@property (nonatomic,strong) Nsarray *lrcarray;
@property (nonatomic,strong) CYPARSERLRC *parser;
@property (nonatomic)nsinteger currentrow;
@end
@implementation Viewcontroller
-(void) Initwithmp3path: (nsstring *) Mp3path Lrcpath: (nsstring *) Lrcpath
{
//initwithcontentsofurl Path (local)
self. Audioplayer = [[avaudioplayer alloc] initwithcontentsofurl: [nsurl Fileurlwithpath: Mp3path] error:nil];
// ready to play
[self. Audioplayer preparetoplay];
// set sound
self. Audioplayer. Volume = 0.5f;
Set up proxy
self. Audioplayer. Delegate = self;
// Set the maximum value of the progress bar
//duration Total time of current music
self. Song. MaximumValue = self. Audioplayer. Duration;
// Parse lyrics
[self. Parser PARSERLRC: Lrcpath];
// next refresh tableView
[self. TableView reloaddata];
self. Timer = [nstimer scheduledtimerwithtimeinterval:. 1f target: Self selector:@selector(timechange) userInfo:nil repeats:YES ];
}
#pragma proxy Methods
/* AudioPlayerDidFinishPlaying:successfully:is Called when a sound has finished playing. This method isn't called if the player is stopped due to an interruption. */
-(void) audioplayerdidfinishplaying: (avaudioplayer *) player successfully: (BOOL) flag;
/* If an error occurs while decoding it is reported to the delegate. */
-(void) Audioplayerdecodeerrordidoccur: (avaudioplayer *) Player error: (nserror * __ Nullable) error;
#if Target_os_iphone
/* Avaudioplayer interruption Notifications is Deprecated-use avaudiosession instead. */
/* Audioplayerbegininterruption:is called when the audio session had been interrupted while the player was playing. The player would have been paused. */
-(void) Audioplayerbegininterruption: (avaudioplayer *) player Ns_deprecated_ios(2_ 2, 8_0);
/* AudioPlayerEndInterruption:withOptions:is called when the audio session interruption have ended and this player had bee N interrupted while playing. */
/* Currently the only flag is avaudiosessioninterruptionflags_shouldresume. */
-(void) Audioplayerendinterruption: (avaudioplayer *) player withoptions: (nsuinteger) Flags Ns_deprecated_ios(6_0, 8_0);
-(void) Audioplayerendinterruption: (avaudioplayer *) player withflags: (nsuinteger) Flags Ns_deprecated_ios(4_0, 6_0);
/* Audioplayerendinterruption:is Called when the preferred method, Audioplayerendinterruption:withflags:, was not impleme nted. */
-(void) Audioplayerendinterruption: (avaudioplayer *) player Ns_deprecated_ios(2_2, 6_0);
iOS player-Avaudioplayer