react-native-audioplayer icon indicating copy to clipboard operation
react-native-audioplayer copied to clipboard

URI as a source

Open idajimenez opened this issue 8 years ago • 1 comments

Is there a support using an URI? thanks

idajimenez avatar Jun 13 '16 03:06 idajimenez

There is none. You can make it work pretty easily for iOS though:

Install react-native-audioplayer as prescribed on the docs (https://github.com/andreaskeller/react-native-audioplayer#installation).

Add this before @end in RNAudioPlayer.h:

@property (strong, nonatomic) AVPlayer *audioPlayerURL;

This before @end in RNAudioPlayer.m:

RCT_EXPORT_METHOD(playFromURL:(NSString *)url)
{
    NSURL *soundURL=[NSURL URLWithString:url];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL: soundURL];
    self.audioPlayerURL = [AVPlayer playerWithPlayerItem:playerItem];
    [self.audioPlayerURL play];
    self.audioPlayerURL.actionAtItemEnd = AVPlayerActionAtItemEndNone;
}

And add this in AudioPlayer object in AudioPlayer.js in node_modules/react-native-audioplayer:

playFromURL(fileName: string) {
    RNAudioPlayer.playFromURL(fileName);
  }

You should now be able to pass a URL to AudioPlayer instance in your React Native code: AudioPlayer.playFromURL("http://example.com/song.mp3")

N.B. You will have to recompile from Xcode for your objective-c code to work.

tbinetruy avatar Aug 28 '16 13:08 tbinetruy