Skip to content

Instantly share code, notes, and snippets.

@stan229
Created October 15, 2015 05:00
Show Gist options
  • Save stan229/669acdee795b76b88397 to your computer and use it in GitHub Desktop.
Save stan229/669acdee795b76b88397 to your computer and use it in GitHub Desktop.
Full Implementation
#import "MediaController.h"
#import "AppDelegate.h"
@implementation MediaController
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
-(void)showMediaPicker {
if(self.mediaPicker == nil) {
self.mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
[self.mediaPicker setDelegate:self];
[self.mediaPicker setAllowsPickingMultipleItems:NO];
[self.mediaPicker setShowsCloudItems:NO];
self.mediaPicker.prompt = @"Select song";
}
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.rootViewController presentViewController:self.mediaPicker animated:YES completion:nil];
}
-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
MPMediaItem *mediaItem = mediaItemCollection.items[0];
NSURL *assetURL = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
[self.bridge.eventDispatcher sendAppEventWithName:@"SongPlaying" body:[mediaItem valueForProperty:MPMediaItemPropertyTitle]];
NSError *error;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:assetURL error:&error];
[self.player setDelegate:self];
if (error) {
NSLog(@"%@", [error localizedDescription]);
} else {
[self.player play];
}
hideMediaPicker();
}
-(void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
hideMediaPicker();
}
#pragma mark RCT_EXPORT
RCT_EXPORT_METHOD(showSongs) {
[self showMediaPicker];
}
#pragma mark private-methods
void hideMediaPicker() {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment