iOS player-Avaudioplayer

Source: Internet
Author: User

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

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.