forked from jsierles/react-native-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioPlayerManager.m
109 lines (85 loc) · 2.67 KB
/
AudioPlayerManager.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
//
// AudioPlayerManager.m
// AudioPlayerManager
//
// Created by Joshua Sierles on 15/04/15.
// Copyright (c) 2015 Joshua Sierles. All rights reserved.
//
#import "AudioPlayerManager.h"
#import "RCTConvert.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import <AVFoundation/AVFoundation.h>
NSString *const AudioPlayerEventProgress = @"playerProgress";
NSString *const AudioPlayerEventFinished = @"playerFinished";
@implementation AudioPlayerManager {
AVAudioPlayer *_audioPlayer;
NSTimeInterval _currentTime;
id _progressUpdateTimer;
int _progressUpdateInterval;
NSDate *_prevProgressUpdateTime;
NSURL *_audioFileURL;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
- (void)sendProgressUpdate {
if (_audioPlayer && _audioPlayer.playing) {
_currentTime = _audioPlayer.currentTime;
}
NSString *time = [NSString stringWithFormat:@"%f", _currentTime];
if (_prevProgressUpdateTime == nil ||
(([_prevProgressUpdateTime timeIntervalSinceNow] * -1000.0) >= _progressUpdateInterval)) {
[_bridge.eventDispatcher sendDeviceEventWithName:AudioPlayerEventProgress body:@{
@"currentTime": [NSNumber numberWithFloat:_currentTime]
}];
_prevProgressUpdateTime = [NSDate date];
}
}
- (void)stopProgressTimer {
[_progressUpdateTimer invalidate];
}
- (void)startProgressTimer {
_progressUpdateInterval = 250;
_prevProgressUpdateTime = nil;
[self stopProgressTimer];
_progressUpdateTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(sendProgressUpdate)];
[_progressUpdateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)AudioPlayerDidFinishPlaying:(AVAudioPlayer *)recorder successfully:(BOOL)flag {
NSLog(flag ? @"FINISHED OK" : @"FINISH ERROR");
[_bridge.eventDispatcher sendDeviceEventWithName:AudioPlayerEventFinished body:@{
@"finished": @TRUE
}];
}
RCT_EXPORT_METHOD(play:(NSString *)path)
{
NSError *error;
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *audioFilePath = [resourcePath stringByAppendingPathComponent:path];
_audioFileURL = [NSURL fileURLWithPath:audioFilePath];
NSLog([_audioFileURL absoluteString]);
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:_audioFileURL
error:&error];
if (error) {
[self stopProgressTimer];
NSLog(@"audio playback loading error: %@", [error localizedDescription]);
// TODO: dispatch error over the bridge
} else {
[self startProgressTimer];
[_audioPlayer play];
}
}
RCT_EXPORT_METHOD(pause)
{
if (_audioPlayer.playing) {
[_audioPlayer pause];
}
}
RCT_EXPORT_METHOD(stop)
{
if (_audioPlayer.playing) {
[_audioPlayer stop];
}
}
@end