Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make notifications on android and media center on iOS optional #29

Merged
merged 2 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package com.audioStreaming;

import android.content.ComponentName;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion ios/ReactNativeAudioStreaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 26 additions & 14 deletions ios/ReactNativeAudioStreaming.m
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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];
}

Expand Down Expand Up @@ -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