Skip to content

Instantly share code, notes, and snippets.

@kyo504
Created March 16, 2017 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyo504/26e75edf89bd787b38b1bb149a44fcb8 to your computer and use it in GitHub Desktop.
Save kyo504/26e75edf89bd787b38b1bb149a44fcb8 to your computer and use it in GitHub Desktop.
iOS native module
#import <React/RCTBridgeModule.h>
@import AVFoundation;
@interface RNAudioPlayer : NSObject <RCTBridgeModule>
@property (strong, nonatomic) AVPlayerItem *playerItem;
@property (strong, nonatomic) AVPlayer *player;
@end
#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