Created
March 16, 2017 09:58
-
-
Save kyo504/26e75edf89bd787b38b1bb149a44fcb8 to your computer and use it in GitHub Desktop.
iOS native module
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 <React/RCTBridgeModule.h> | |
@import AVFoundation; | |
@interface RNAudioPlayer : NSObject <RCTBridgeModule> | |
@property (strong, nonatomic) AVPlayerItem *playerItem; | |
@property (strong, nonatomic) AVPlayer *player; | |
@end |
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 "RNAudioPlayer.h" | |
#import <React/RCTBridge.h> | |
#import <React/RCTEventDispatcher.h> | |
#import <React/RCTEventEmitter.h> | |
#import <AVFoundation/AVFoundation.h> | |
#import <Foundation/Foundation.h> | |
#import <MediaPlayer/MediaPlayer.h> | |
@implementation RNAudioPlayer | |
@synthesize bridge = _bridge; | |
RCT_EXPORT_MODULE(); | |
- (RNAudioPlayer *)init { | |
return [super init]; | |
} | |
#pragma mark - Pubic API | |
RCT_EXPORT_METHOD(play:(NSString *) url) | |
{ | |
NSURL *soundUrl = [[NSURL alloc] initWithString:url]; | |
self.playerItem = [AVPlayerItem playerItemWithURL:soundUrl]; | |
self.player = [AVPlayer playerWithPlayerItem:self.playerItem]; | |
[self.player play]; | |
[self.bridge.eventDispatcher sendDeviceEventWithName: @"onPlaybackStateChanged" body: @{@"state": @"PLAYING" }]; | |
} | |
RCT_EXPORT_METHOD(pause) | |
{ | |
[self.player pause]; | |
[self.bridge.eventDispatcher sendDeviceEventWithName: @"onPlaybackStateChanged" body: @{@"state": @"PAUSED" }]; | |
} | |
RCT_EXPORT_METHOD(isPlaying:(RCTResponseSenderBlock) callback) | |
{ | |
NSMutableDictionary *events = [[NSMutableDictionary alloc] init]; | |
if (self.player != nil && self.player.rate != 0) { | |
events[@"playing"] = @YES; | |
} else { | |
events[@"playing"] = @NO; | |
} | |
callback(events); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment