播放歌曲|播放歌曲 AudioTool 笔记
.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
#import#define kBirthdaySong @"birthday"
@interface AudioTool ()
@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;
【播放歌曲|播放歌曲 AudioTool 笔记】//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; \
}
推荐阅读
- 四首关于旅行记忆的外文歌曲
- 定军山|定军山 改编歌曲
- 2020年10月新番播放时间及版权信息集合
- Qt|Qt 使用QMediaPlayer播放MP3
- iOS10|iOS10 WKWebView里的视频播放转屏适配
- FFmpeg|FFmpeg 开发(07)(FFmpeg + OpenGLES 实现 3D 全景播放器)
- 使用ffmpeg制作一个ios播放器|使用ffmpeg制作一个ios播放器 一
- Android播放视频--VideoView
- python|python 媒体文件播放 脚本
- 苹果HomePod姗姗来迟|苹果HomePod姗姗来迟 可智能分析歌曲提供合适音效