forked from garrettg123/triggerio-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_API.m
More file actions
107 lines (83 loc) · 2.83 KB
/
audio_API.m
File metadata and controls
107 lines (83 loc) · 2.83 KB
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
//
// alert_API.m
// ForgeInspector
//
// Created by Connor Dunn on 27/07/2012.
// Copyright (c) 2012 Trigger Corp. All rights reserved.
//
#import "audio_API.h"
static AVAudioPlayer* player = nil;
@implementation audio_API
+ (void)play:(ForgeTask*)task filename:(NSString *)filename {
// grab file url
NSString * bundlePath = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"bundle"];
NSBundle * myBundle = [NSBundle bundleWithPath:bundlePath];
NSURL * url = [myBundle URLForResource:filename withExtension:nil];
// test url
if(![url isFileURL]) {
NSLog(@"NOT a file url: %@", [url path]);
[task error:[NSString stringWithFormat:@"%@%@%@%@", @"NOT a file url: ", [url path], @" from ", filename]];
} else
NSLog(@"IS a file url: %@", [url path]);
// create the player
NSError* error = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if(!player)
{
NSLog(@"Error creating player: %@", error);
[task error:[NSString stringWithFormat:@"%@%@", @"Error creating player: ", [error localizedDescription]]];
};
player.delegate = (id<AVAudioPlayerDelegate>)[audio_API class];
// activate the session (to use ducking)
AudioSessionSetActive(true);
// play the file
[player play];
// return success
[task success:nil];
}
+ (void)pause:(ForgeTask*)task {
[ForgeLog d:@"Pausing player playing..."];
[ForgeLog d:[player url]];
[player pause];
[task success:nil];
}
+ (void)stop:(ForgeTask*)task {
[ForgeLog d:@"Stopping player playing..."];
[ForgeLog d:[player url]];
[player stop];
[task success:nil];
}
+ (void)mute:(ForgeTask*)task {
[ForgeLog d:@"Muting player playing..."];
[ForgeLog d:[player url]];
player.volume = 0.0;
[task success:nil];
}
+ (void)unmute:(ForgeTask*)task {
[ForgeLog d:@"Unmuting player playing..."];
[ForgeLog d:[player url]];
player.volume = 1.0;
[task success:nil];
}
+ (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag) {
[ForgeLog d:@"Audio finished playing successfully."];
} else {
[ForgeLog d:@"Audio did not finish playing successfully."];
}
// deactivate the session (to use ducking)
AudioSessionSetActive(false);
// call the audio.finishedPlaying event
[[ForgeApp sharedApp] event:@"audio.finishedPlaying" withParam:nil];
}
+ (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
[ForgeLog d:@"Audio had error decoding."];
[ForgeLog e:error];
// deactivate the session (to use ducking)
AudioSessionSetActive(false);
// call the audio.decodeErrorOccurred event
[[ForgeApp sharedApp] event:@"audio.decodeErrorOccurred" withParam:nil];
}
@end