.h
//+ (instancetype)sharedAudioTool;
SingletonInterface(AudioTool)
/**
*
* @param fileName 播放的文件名
* @param isLoop 是否循环播放
* @return 是否播放成功
*/
- (BOOL)playMusic:(NSString *)fileName isLoops:(BOOL)isLoop;
/**
*
* @param fileName 停止播放的文件名
*
*/
- (void)stopMusic:(NSString *)fileName;
/** 播放生日歌 */
- (void)playBirthSong;
/** 停止播放生日歌 */
- (void)stopBirthSong;
---------------------------------------------------------
.m
#import <AVFoundation/AVFoundation.h>
#import#define kBirthdaySong @"birthday"
@interface AudioTool () <AVAudioPlayerDelegate>
@end
static NSMutableDictionary *musicPlayers;
@implementation GSAudioTool
- (instancetype)init
{
if (self = [super init]) {
if (!musicPlayers) musicPlayers = [NSMutableDictionary dictionary];
}
return self;
}
//static GSAudioTool *_audioTool = nil;
//+ (instancetype)sharedAudioTool
//{
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// _audioTool = [[GSAudioTool alloc] init];
// });
// return _audioTool;
//}
SingletonImplementation(AudioTool)
#pragma mark - 播放生日歌
- (void)playBirthSong
{
[self playMusic:kBirthdaySong isLoops:YES];
}
#pragma mark - 停止生日歌
- (void)stopBirthSong
{
[self stopMusic:kBirthdaySong];
}
- (BOOL)playMusic:(NSString *)fileName isLoops:(BOOL)isLoop
{
//文件不存在
if (fileName.length == 0) return NO;
AVAudioPlayer *player = musicPlayers[fileName];
if (!player) {
NSURL *url = [[NSBundle mainBundle] URLForResource:fileName withExtension:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.delegate = self;
if (isLoop) player.numberOfLoops = 2;
if (![player prepareToPlay]) return NO; //是否可以播放
[musicPlayers setObject:player forKey:fileName];
}
if (!player.isPlaying) return [player play];
return YES;
}
- (void)stopMusic:(NSString *)fileName
{
//文件不存在
if (fileName.length == 0) return;
AVAudioPlayer *player = musicPlayers[fileName];
if (player) {
[player stop];
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
[musicPlayers removeObjectForKey:fileName];
}
}
#pragma mark - AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
}
------------------------------------------------
紅
#define SingletonInterface(name) + (instancetype)shared##name;
#define SingletonImplementation(name) \
static id _instance; \
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}