diff --git a/README.md b/README.md index 362e410..9489afb 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ import { ReactNativeAudioStreaming } from 'react-native-audio-streaming'; const url = "http://lacavewebradio.chickenkiller.com:8000/stream.mp3"; ReactNativeAudioStreaming.pause(); ReactNativeAudioStreaming.resume(); -ReactNativeAudioStreaming.play(url); +ReactNativeAudioStreaming.play(url, {showIniOSMediaCenter: true, showInAndroidNotifications: true}); ReactNativeAudioStreaming.stop(); ``` diff --git a/android/libs/aacdecoder-android-0.8.jar b/android/libs/aacdecoder-android-0.8.jar index dde2d0d..cd9a60c 100644 Binary files a/android/libs/aacdecoder-android-0.8.jar and b/android/libs/aacdecoder-android-0.8.jar differ diff --git a/android/src/main/java/com/audioStreaming/ReactNativeAudioStreamingModule.java b/android/src/main/java/com/audioStreaming/ReactNativeAudioStreamingModule.java index bd15925..00921ef 100644 --- a/android/src/main/java/com/audioStreaming/ReactNativeAudioStreamingModule.java +++ b/android/src/main/java/com/audioStreaming/ReactNativeAudioStreamingModule.java @@ -1,4 +1,3 @@ - package com.audioStreaming; import android.content.ComponentName; @@ -7,7 +6,6 @@ import android.content.ServiceConnection; import android.os.IBinder; import android.util.Log; - import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; @@ -17,17 +15,19 @@ import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; - import javax.annotation.Nullable; -public class ReactNativeAudioStreamingModule extends ReactContextBaseJavaModule implements ServiceConnection { +public class ReactNativeAudioStreamingModule extends ReactContextBaseJavaModule + implements ServiceConnection { + public static final String SHOULD_SHOW_NOTIFICATION = "showInAndroidNotifications"; private ReactApplicationContext context; private Class clsActivity; private static Signal signal; private Intent bindIntent; private String streamingURL; + private boolean shouldShowNotification; public ReactNativeAudioStreamingModule(ReactApplicationContext reactContext) { super(reactContext); @@ -54,18 +54,15 @@ public Signal getSignal() { } public void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) { - this.context - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(eventName, params); + this.context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(eventName, params); } - @Override - public String getName() { + @Override public String getName() { return "ReactNativeAudioStreaming"; } - @Override - public void initialize() { + @Override public void initialize() { super.initialize(); try { @@ -76,51 +73,51 @@ public void initialize() { } } - @Override - public void onServiceConnected(ComponentName className, IBinder service) { + @Override public void onServiceConnected(ComponentName className, IBinder service) { signal = ((Signal.RadioBinder) service).getService(); signal.setData(this.context, this); WritableMap params = Arguments.createMap(); sendEvent(this.getReactApplicationContextModule(), "streamingOpen", params); } - @Override - public void onServiceDisconnected(ComponentName className) { + @Override public void onServiceDisconnected(ComponentName className) { signal = null; } - @ReactMethod - public void play(String streamingURL) { + @ReactMethod public void play(String streamingURL, ReadableMap options) { this.streamingURL = streamingURL; + this.shouldShowNotification = + options.hasKey(SHOULD_SHOW_NOTIFICATION) && options.getBoolean(SHOULD_SHOW_NOTIFICATION); signal.setURLStreaming(streamingURL); // URL of MP3 or AAC stream + playInternal(); + } + + private void playInternal() { signal.play(); - signal.showNotification(); + if (shouldShowNotification) { + signal.showNotification(); + } } - @ReactMethod - public void stop() { + @ReactMethod public void stop() { signal.stop(); } - @ReactMethod - public void pause() { + @ReactMethod public void pause() { // Not implemented on aac this.stop(); } - @ReactMethod - public void resume() { + @ReactMethod public void resume() { // Not implemented on aac - this.play(this.streamingURL); + playInternal(); } - @ReactMethod - public void destroyNotification() { + @ReactMethod public void destroyNotification() { signal.exitNotification(); } - @ReactMethod - public void getStatus(Callback callback) { + @ReactMethod public void getStatus(Callback callback) { WritableMap state = Arguments.createMap(); state.putString("status", signal != null && signal.isPlaying ? Mode.PLAYING : Mode.STOPPED); callback.invoke(null, state); diff --git a/android/src/main/jniLibs/armeabi-v7a/libaacdecoder.so b/android/src/main/jniLibs/armeabi-v7a/libaacdecoder.so index f2a3a70..610df1b 100644 Binary files a/android/src/main/jniLibs/armeabi-v7a/libaacdecoder.so and b/android/src/main/jniLibs/armeabi-v7a/libaacdecoder.so differ diff --git a/android/src/main/jniLibs/armeabi/libaacdecoder.so b/android/src/main/jniLibs/armeabi/libaacdecoder.so index fefd827..7f03217 100644 Binary files a/android/src/main/jniLibs/armeabi/libaacdecoder.so and b/android/src/main/jniLibs/armeabi/libaacdecoder.so differ diff --git a/android/src/main/jniLibs/mips/libaacdecoder.so b/android/src/main/jniLibs/mips/libaacdecoder.so index a7facfa..36c3b46 100644 Binary files a/android/src/main/jniLibs/mips/libaacdecoder.so and b/android/src/main/jniLibs/mips/libaacdecoder.so differ diff --git a/android/src/main/jniLibs/x86/libaacdecoder.so b/android/src/main/jniLibs/x86/libaacdecoder.so index 7a0e292..ecfbe95 100644 Binary files a/android/src/main/jniLibs/x86/libaacdecoder.so and b/android/src/main/jniLibs/x86/libaacdecoder.so differ diff --git a/index.js b/index.js index 770dcca..c075869 100644 --- a/index.js +++ b/index.js @@ -62,7 +62,7 @@ class Player extends Component { break; case STOPPED: case ERROR: - ReactNativeAudioStreaming.play(this.props.url); + ReactNativeAudioStreaming.play(this.props.url, {showIniOSMediaCenter: true, showInAndroidNotifications: true}); break; case BUFFERING: ReactNativeAudioStreaming.stop(); diff --git a/ios/ReactNativeAudioStreaming.h b/ios/ReactNativeAudioStreaming.h index 60807fc..11bda88 100644 --- a/ios/ReactNativeAudioStreaming.h +++ b/ios/ReactNativeAudioStreaming.h @@ -8,10 +8,11 @@ @property (nonatomic, strong) STKAudioPlayer *audioPlayer; @property (nonatomic, readwrite) BOOL isPlayingWithOthers; +@property (nonatomic, readwrite) BOOL showNowPlayingInfo; @property (nonatomic, readwrite) NSString *lastUrlString; @property (nonatomic, retain) NSString *currentSong; -- (void)play; +- (void)play:(NSString *) streamUrl options:(NSDictionary *)options; - (void)pause; @end diff --git a/ios/ReactNativeAudioStreaming.m b/ios/ReactNativeAudioStreaming.m index f1cf624..446ebe4 100644 --- a/ios/ReactNativeAudioStreaming.m +++ b/ios/ReactNativeAudioStreaming.m @@ -25,11 +25,7 @@ - (ReactNativeAudioStreaming *)init [self setSharedAudioSessionCategory]; self.audioPlayer = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES }]; [self.audioPlayer setDelegate:self]; - [self registerAudioInterruptionNotifications]; - [self registerRemoteControlEvents]; - [self setNowPlayingInfo:true]; self.lastUrlString = @""; - [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(tick:) userInfo:nil repeats:YES]; NSLog(@"AudioPlayer initialized"); @@ -70,7 +66,7 @@ - (void)dealloc #pragma mark - Pubic API -RCT_EXPORT_METHOD(play:(NSString *) streamUrl) +RCT_EXPORT_METHOD(play:(NSString *) streamUrl options:(NSDictionary *)options) { if (!self.audioPlayer) { return; @@ -85,6 +81,18 @@ - (void)dealloc } self.lastUrlString = streamUrl; + self.showNowPlayingInfo = false; + if ([options objectForKey:@"showIniOSMediaCenter"]) { + self.showNowPlayingInfo = [[options objectForKey:@"showIniOSMediaCenter"] boolValue]; + } + if(self.showNowPlayingInfo) { + //unregister any existing registrations + [self unregisterAudioInterruptionNotifications]; + [self unregisterRemoteControlEvents]; + //register + [self registerAudioInterruptionNotifications]; + [self registerRemoteControlEvents]; + } [self setNowPlayingInfo:true]; } @@ -428,16 +436,20 @@ - (void)unregisterRemoteControlEvents - (void)setNowPlayingInfo:(bool)isPlaying { - // TODO Get artwork from stream - // MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc]initWithImage:[UIImage imageNamed:@"webradio1"]]; + if (self.showNowPlayingInfo) { + // TODO Get artwork from stream + // MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc]initWithImage:[UIImage imageNamed:@"webradio1"]]; - NSString* appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]; - NSDictionary *nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys: - self.currentSong, MPMediaItemPropertyAlbumTitle, - @"", MPMediaItemPropertyAlbumArtist, - appName ? appName : @"", MPMediaItemPropertyTitle, - [NSNumber numberWithFloat:isPlaying ? 1.0f : 0.0], MPNowPlayingInfoPropertyPlaybackRate, nil]; - [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfo; + NSString* appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]; + NSDictionary *nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys: + self.currentSong, MPMediaItemPropertyAlbumTitle, + @"", MPMediaItemPropertyAlbumArtist, + appName ? appName : @"", MPMediaItemPropertyTitle, + [NSNumber numberWithFloat:isPlaying ? 1.0f : 0.0], MPNowPlayingInfoPropertyPlaybackRate, nil]; + [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfo; + } else { + [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nil; + } } @end