1
1
#import " FlutterSoundPlugin.h"
2
2
#import < AVFoundation/AVFoundation.h>
3
3
4
- @implementation FlutterSoundPlugin
4
+ @implementation FlutterSoundPlugin {
5
5
NSURL *audioFileURL;
6
6
AVAudioRecorder *audioRecorder;
7
7
AVAudioPlayer *audioPlayer;
8
8
NSTimer *timer;
9
+ }
10
+
11
+ - (void )audioPlayerDidFinishPlaying : (AVAudioPlayer *)player successfully : (BOOL )flag {
12
+ NSLog (@" audioPlayerDidFinishPlaying" );
13
+ NSNumber *duration = [NSNumber numberWithDouble: audioPlayer.duration * 1000 ];
14
+
15
+ // Send last event then finish it.
16
+ // [self sendEventWithName:@"rn-playback" body:@{
17
+ // @"duration" : [duration stringValue],
18
+ // @"current_position" : [duration stringValue],
19
+ // @"justFinished" : @"1",
20
+ // }
21
+ // ];
22
+ if (timer != nil ) {
23
+ [timer invalidate ];
24
+ timer = nil ;
25
+ }
26
+ }
27
+
28
+ - (void )updateProgress : (NSTimer *) timer
29
+ {
30
+ NSLog (@" updateProgress" );
31
+ NSNumber *duration = [NSNumber numberWithDouble: audioPlayer.duration * 1000 ];
32
+ NSNumber *currentTime = [NSNumber numberWithDouble: audioPlayer.currentTime * 1000 ];
33
+
34
+ NSDictionary *status = @{
35
+ @" duration" : [duration stringValue ],
36
+ @" current_position" : [currentTime stringValue ],
37
+ };
38
+
39
+ // [self sendEventWithName:@"rn-playback" body:status];
40
+ }
41
+
42
+ - (void )startTimer
43
+ {
44
+ // dispatch_async(dispatch_get_main_queue(), ^{
45
+ // self->timer = [NSTimer scheduledTimerWithTimeInterval:1.0
46
+ // target:self
47
+ // selector:@selector(updateProgress:)
48
+ // userInfo:nil
49
+ // repeats:YES];
50
+ // });
51
+ }
52
+
9
53
10
54
+ (void )registerWithRegistrar : (NSObject <FlutterPluginRegistrar>*)registrar {
11
55
FlutterMethodChannel* channel = [FlutterMethodChannel
@@ -24,15 +68,17 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
24
68
} else if ([@" stopRecorder" isEqualToString: call.method]) {
25
69
[self stopRecorder: result];
26
70
} else if ([@" startPlayer" isEqualToString: call.method]) {
27
- [self startPlayer: result];
71
+ NSString * path = (NSString *)call.arguments [@" path" ];
72
+ [self startPlayer: path result: result];
28
73
} else if ([@" stopPlayer" isEqualToString: call.method]) {
29
74
[self stopPlayer: result];
30
75
} else if ([@" pausePlayer" isEqualToString: call.method]) {
31
76
[self pausePlayer: result];
32
77
} else if ([@" resumePlayer" isEqualToString: call.method]) {
33
78
[self resumePlayer: result];
34
79
} else if ([@" seekToPlayer" isEqualToString: call.method]) {
35
- [self seekToPlayer: result];
80
+ NSNumber * sec = (NSNumber *)call.arguments [@" sec" ];
81
+ [self seekToPlayer: sec result: result];
36
82
} else {
37
83
result (FlutterMethodNotImplemented);
38
84
}
@@ -115,7 +161,6 @@ - (void)startPlayer:(NSString*)path result: (FlutterResult)result {
115
161
}
116
162
117
163
if (!audioPlayer) {
118
- RCTLogInfo (@" audio player alloc" );
119
164
audioPlayer = [[AVAudioPlayer alloc ] initWithContentsOfURL: audioFileURL error: nil ];
120
165
audioPlayer.delegate = self;
121
166
}
@@ -151,7 +196,6 @@ - (void)stopPlayer:(FlutterResult)result {
151
196
}
152
197
153
198
- (void )pausePlayer : (FlutterResult)result {
154
- RCTLogInfo (@" pause" );
155
199
if (audioPlayer && [audioPlayer isPlaying ]) {
156
200
[audioPlayer pause ];
157
201
if (timer != nil ) {
@@ -196,7 +240,7 @@ - (void)resumePlayer:(FlutterResult)result {
196
240
- (void )seekToPlayer : (nonnull NSNumber *) time result : (FlutterResult)result {
197
241
if (audioPlayer) {
198
242
audioPlayer.currentTime = [time doubleValue ];
199
- result (audioPlayer. currentTime );
243
+ result (time );
200
244
} else {
201
245
result ([FlutterError
202
246
errorWithCode: @" Audio Player"
0 commit comments