-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZJAudioTool.m
114 lines (91 loc) · 2.73 KB
/
ZJAudioTool.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// ZJAudioTool.m
// ZJAudioTool
//
// Created by 侯宝伟 on 14/05/24.
// Copyright © 2014年 ZHUNJIEE. All rights reserved.
//
#import "ZJAudioTool.h"
#import <AVFoundation/AVFoundation.h>
@implementation ZJAudioTool
static NSMutableDictionary *_soundIDs;
static NSMutableDictionary *_players;
static AVPlayer *_player;
+ (void)initialize{
_soundIDs = [NSMutableDictionary dictionary];
_players = [NSMutableDictionary dictionary];
}
/**
* 播放本地音效
*/
+ (void)playSoundWithSoundName:(NSString *)soundName{
// 1. 取出对应的SystemSoundID
SystemSoundID soundID = [_soundIDs[soundName] unsignedIntValue];
// 2. 如果之前没有保存过对应的soundID,取出则为0
if (soundID == 0) {
// 根据soundName创建soundID
// 2.1 获取soundName的路径
CFURLRef url = (__bridge CFURLRef)([[NSBundle mainBundle] URLForResource:soundName withExtension:nil]);
// 2.2 给soundID赋值
AudioServicesCreateSystemSoundID(url, &soundID);
// 2.3 将soundID保存到字典中
[_soundIDs setObject:@(soundID) forKey:soundName];
}
// 3. 播放音效
AudioServicesPlayAlertSound(soundID);
}
/**
* 播放在线URL音频
*/
+ (void)playMusicWithUrl:(NSString *)urlString{
_player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlString]];
[_player play];
}
/**
* 停止URL音频
*/
+ (void)stopUrlMusic{
[_player pause];
}
/**
* 播放本地音乐
*/
+ (void)playMusicWithMusicName:(NSString *)musicName{
// 1. 取出对应的播放器
AVAudioPlayer *player = _players[musicName];
// 2. 如果取出为nil,则通过对应url来创建播放器
if (player == nil) {
// 2.1 获取资源的url
NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil];
// 2.2 根据url来创建播放器
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// 2.3 将播放器存储到字典中
[_players setObject:player forKey:musicName];
}
// 3. 播放音乐
[player play];
}
/**
* 暂停本地音乐
*/
+ (void)pauseMusicWithMusicName:(NSString *)musicName{
// 1. 取出对应的播放器
AVAudioPlayer *player = _players[musicName];
// 2. 判断是否为nil,如果不为nil,暂停音乐
if (player) {
[player pause];
}
}
/**
* 停止本地音乐
*/
+ (void)stopMusicWithMusicName:(NSString *)musicName{
// 1. 取出对应的播放器
AVAudioPlayer *player = _players[musicName];
// 2. 判断是否为nil,如果不为nil,停止音乐
if (player) {
[player stop];
[_players removeObjectForKey:musicName];
}
}
@end