简单本地音频播放

#import

#import@implementation CCADPlatformManager {

AVAudioPlayer *audioPlayerBegin;

AVAudioPlayer *audioPlayerEnd;

}

#pragma mark - 播放 广告音乐

- (void)playShareMusic {

if (audioPlayerBegin == nil) {

NSString *path = [[NSBundle mainBundle] pathForResource:@"sharktosigninbegan" ofType:@"mp3"];

NSURL *url = [NSURL URLWithString:path];

NSError *error;

audioPlayerBegin = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

audioPlayerBegin.numberOfLoops = 2;

[audioPlayerBegin setVolume:1.0f];

}

if (audioPlayerEnd == nil) {

NSString *path = [[NSBundle mainBundle] pathForResource:@"sharktosigninended" ofType:@"mp3"];

NSURL *url = [NSURL URLWithString:path];

NSError *error;

audioPlayerEnd = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

[audioPlayerEnd setVolume:1.0f];

}

if (![audioPlayerBegin isPlaying] && ![audioPlayerEnd isPlaying]) {

[audioPlayerBegin prepareToPlay];

[audioPlayerBegin play];

[audioPlayerEnd prepareToPlay];

[audioPlayerEnd performSelector:@selector(play) withObject:nil afterDelay:1.7f];

}

}

#pragma mark - 停止 广告音乐

- (void)stopShareMusic {

if (audioPlayerBegin && [audioPlayerBegin isPlaying]) {

[audioPlayerBegin stop];

}

if (audioPlayerEnd && [audioPlayerEnd isPlaying]) {

[audioPlayerEnd stop];

}

}

你可能感兴趣的:(简单本地音频播放)