简单本地音频播放

#import<AVFoundation/AVFoundation.h>

#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];

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容