Last active
June 15, 2016 07:21
-
-
Save homanp/b09b7e83cdd1aafa19b39527044bef28 to your computer and use it in GitHub Desktop.
Implementation file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "RCTSound.h" | |
#import <AVFoundation/AVPlayer.h> | |
#import <AVFoundation/AVPlayerItem.h> | |
#import <AVFoundation/AVAsset.h> | |
#import "RCTEventDispatcher.h" | |
@implementation RCTSound { | |
NSString *audioUrl; | |
AVPlayerItem *player; | |
AVPlayer *stream; | |
} | |
@synthesize bridge = _bridge; | |
RCT_EXPORT_MODULE(); | |
RCT_EXPORT_METHOD(init:(NSString *)url callback:(RCTResponseSenderBlock)callback) | |
{ | |
audioUrl = [NSString stringWithFormat:@"%@", url]; | |
player = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:audioUrl]]; | |
stream = [[AVPlayer alloc] initWithPlayerItem:player]; | |
if (stream) { | |
CMTime duration = stream.currentItem.asset.duration; | |
float seconds = CMTimeGetSeconds(duration); | |
callback(@[@(seconds)]); | |
} | |
} | |
RCT_EXPORT_METHOD(initLocal:(NSString *)url callback:(RCTResponseSenderBlock)callback) | |
{ | |
NSURL * localURL = [NSURL URLWithString:[@"file://" stringByAppendingString:url]]; | |
audioUrl = [NSString stringWithFormat:@"%@", localURL]; | |
player = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:audioUrl]]; | |
stream = [[AVPlayer alloc] initWithPlayerItem:player]; | |
if (stream) { | |
CMTime duration = stream.currentItem.asset.duration; | |
float seconds = CMTimeGetSeconds(duration); | |
callback(@[@(seconds)]); | |
} | |
} | |
RCT_EXPORT_METHOD(getDuration) | |
{ | |
if(stream) { | |
CMTimeGetSeconds(stream.currentItem.asset.duration); | |
} | |
} | |
RCT_EXPORT_METHOD(play:(RCTResponseSenderBlock)callback) | |
{ | |
if(stream) { | |
__weak typeof(self) weakSelf = self; | |
[stream addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0 / 1.0, NSEC_PER_SEC) | |
queue:NULL | |
usingBlock:^(CMTime time){ | |
[weakSelf getCurrentTime]; | |
}]; | |
[stream play]; | |
callback(@[@YES]); | |
} | |
} | |
RCT_EXPORT_METHOD(pause:(RCTResponseSenderBlock)callback) | |
{ | |
if(stream) { | |
[stream pause]; | |
callback(@[@YES]); | |
} | |
} | |
RCT_EXPORT_METHOD(stop:(RCTResponseSenderBlock)callback) | |
{ | |
if(stream) { | |
[stream seekToTime:CMTimeMake(0, 1)]; | |
[stream pause]; | |
callback(@[@YES]); | |
} | |
} | |
-(void) getCurrentTime | |
{ | |
if (stream) { | |
CMTime currentTime = [stream currentTime]; | |
CMTime duration = stream.currentItem.asset.duration; | |
float currentSeconds = CMTimeGetSeconds(currentTime); | |
float currentDuration = CMTimeGetSeconds(duration); | |
if (currentSeconds == currentDuration) { | |
[player seekToTime:kCMTimeZero]; | |
[self.bridge.eventDispatcher sendAppEventWithName:@"onPlaybackEnd" | |
body:nil]; | |
} else { | |
[self.bridge.eventDispatcher sendAppEventWithName:@"onPlaybackChange" | |
body:@(currentSeconds)]; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment