Skip to content

Commit

Permalink
foreground service + exoplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
zerbfra committed May 23, 2018
1 parent 202399b commit 386ce32
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 55 deletions.
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 27
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 16
Expand Down
131 changes: 131 additions & 0 deletions android/react-native-audio-streaming.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":react-native-audio-streaming" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../../../android" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":react-native-audio-streaming" />
</configuration>
</facet>
<facet type="android" name="Android">
<configuration>
<option name="SELECTED_BUILD_VARIANT" value="debug" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<afterSyncTasks>
<task>generateDebugSources</task>
</afterSyncTasks>
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
<option name="PROJECT_TYPE" value="1" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/debug" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/debug" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/debug" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/aidl/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/rs/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/apt/androidTest/debug" isTestSource="true" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/rs/androidTest/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/build/generated/res/resValues/androidTest/debug" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/assets" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/debug/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/testDebug/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/assets" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/aidl" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 27 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="com.squareup.okhttp:okhttp-ws:2.5.0@jar" level="project" />
<orderEntry type="library" exported="" name="com.google.android.exoplayer:exoplayer-smoothstreaming-r2.4.2" level="project" />
<orderEntry type="library" exported="" name="com.facebook.fresco:fresco-0.8.1" level="project" />
<orderEntry type="library" exported="" name="com.facebook.fresco:imagepipeline-0.8.1" level="project" />
<orderEntry type="library" exported="" name="com.android.support:appcompat-v7-23.0.1" level="project" />
<orderEntry type="library" exported="" name="com.google.android.exoplayer:exoplayer-r2.4.2" level="project" />
<orderEntry type="library" exported="" name="com.squareup.okhttp:okhttp:2.5.0@jar" level="project" />
<orderEntry type="library" exported="" name="com.android.support:support-core-ui-25.3.1" level="project" />
<orderEntry type="library" exported="" name="com.facebook.fresco:fbcore-0.8.1" level="project" />
<orderEntry type="library" exported="" name="com.android.support:support-compat-25.3.1" level="project" />
<orderEntry type="library" exported="" name="com.facebook.react:react-native-0.20.1" level="project" />
<orderEntry type="library" exported="" name="com.nineoldandroids:library:2.4.0@jar" level="project" />
<orderEntry type="library" exported="" name="com.google.android.exoplayer:exoplayer-ui-r2.4.2" level="project" />
<orderEntry type="library" exported="" name="com.google.code.findbugs:jsr305:3.0.0@jar" level="project" />
<orderEntry type="library" exported="" name="com.fasterxml.jackson.core:jackson-core:2.2.3@jar" level="project" />
<orderEntry type="library" exported="" name="com.android.support:support-core-utils-25.3.1" level="project" />
<orderEntry type="library" exported="" name="com.android.support:support-v4-25.3.1" level="project" />
<orderEntry type="library" exported="" name="com.android.support:recyclerview-v7-23.0.1" level="project" />
<orderEntry type="library" exported="" name="com.android.support:support-fragment-25.3.1" level="project" />
<orderEntry type="library" exported="" name="com.facebook.fresco:drawee-0.8.1" level="project" />
<orderEntry type="library" exported="" name="com.android.support:support-media-compat-25.3.1" level="project" />
<orderEntry type="library" exported="" name="com.facebook.stetho:stetho-okhttp:1.2.0@jar" level="project" />
<orderEntry type="library" exported="" name="com.parse.bolts:bolts-android:1.1.4@jar" level="project" />
<orderEntry type="library" exported="" name="commons-cli:commons-cli:1.2@jar" level="project" />
<orderEntry type="library" exported="" name="com.squareup.okio:okio:1.6.0@jar" level="project" />
<orderEntry type="library" exported="" name="com.facebook.stetho:stetho:1.2.0@jar" level="project" />
<orderEntry type="library" exported="" name="com.google.android.exoplayer:exoplayer-dash-r2.4.2" level="project" />
<orderEntry type="library" exported="" name="com.google.android.exoplayer:exoplayer-core-r2.4.2" level="project" />
<orderEntry type="library" exported="" name="com.google.android.exoplayer:exoplayer-hls-r2.4.2" level="project" />
<orderEntry type="library" exported="" name="com.facebook.fresco:imagepipeline-okhttp-0.8.1" level="project" />
<orderEntry type="library" exported="" name="com.android.support:support-annotations:25.3.1@jar" level="project" />
<orderEntry type="library" exported="" name="org.webkit:android-jsc-r174650" level="project" />
</component>
</module>
7 changes: 3 additions & 4 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.audioStreaming">
package="com.audioStreaming">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true">
<service
android:name="com.audioStreaming.Signal"
android:name="com.audioStreaming.SignalService"
android:enabled="true"
android:theme="@android:style/Theme.NoTitleBar"/>
android:exported="true"/>
</application>
</manifest>

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
Expand All @@ -25,8 +26,10 @@ public class ReactNativeAudioStreamingModule extends ReactContextBaseJavaModule
private ReactApplicationContext context;

private Class<?> clsActivity;
private static Signal signal;
private static SignalService signal;
private Intent bindIntent;
private String streamingURL;
private boolean play = false;
private boolean shouldShowNotification;


Expand All @@ -48,10 +51,10 @@ public Class<?> getClassActivity() {
}

public void stopOncall() {
this.signal.stop();
if (this.signal != null) this.signal.stop();
}

public Signal getSignal() {
public SignalService getSignal() {
return signal;
}

Expand All @@ -64,71 +67,99 @@ public void sendEvent(ReactContext reactContext, String eventName, @Nullable Wri
return "ReactNativeAudioStreaming";
}

@Override public void initialize() {
@Override
public void initialize() {
super.initialize();

try {
bindIntent = new Intent(this.context, Signal.class);
bindIntent = new Intent(this.context, SignalService.class);

this.context.startService(bindIntent);
this.context.bindService(bindIntent, this, Context.BIND_AUTO_CREATE);

} catch (Exception e) {
Log.e("ERROR", e.getMessage());
}
}

@Override public void onServiceConnected(ComponentName className, IBinder service) {
signal = ((Signal.RadioBinder) service).getService();
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
signal = ((SignalService.SignalBinder) service).getService();
signal.setData(this.context, this);
if (play) {
playInternal();
}
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, ReadableMap options) {
@ReactMethod
public void play(String streamingURL, ReadableMap options) {
this.streamingURL = streamingURL;
this.shouldShowNotification = options.hasKey(SHOULD_SHOW_NOTIFICATION) && options.getBoolean(SHOULD_SHOW_NOTIFICATION);
playInternal(streamingURL);
playInternal();
}

private void playInternal(String streamingURL) {
signal.play(streamingURL);

if (shouldShowNotification) {
signal.showNotification();
private void playInternal() {
play = true;
if (signal != null) {

Log.e("IO", "PLAY START");
signal.setURLStreaming(streamingURL); // URL of MP3 or AAC stream
signal.play();

// if (shouldShowNotification) {
// signalService.showNotification();
// }
}
}

@ReactMethod public void stop() {
signal.stop();
@ReactMethod
public void stop() {
play = false;
if (signal != null) 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
signal.resume();
playInternal();
}

@ReactMethod public void destroyNotification() {
@ReactMethod
public void destroyNotification() {
signal.exitNotification();
}

@ReactMethod public void seekToTime(int seconds) {
signal.seekTo(seconds * 1000);
@ReactMethod
public void seekToTime(int seconds) {
// signal.seekTo(seconds * 1000);
}

private boolean isServicePlaying() {
return signal != null; // && signal.isPlaying;
}

@ReactMethod public void getStatus(Callback callback) {
WritableMap state = Arguments.createMap();
state.putDouble("duration", signal.getDuration());
state.putDouble("progress", signal.getCurrentPosition());
state.putString("status", signal != null && signal.isPlaying() ? Mode.PLAYING : Mode.STOPPED);
state.putString("url", signal.getStreamingURL());
callback.invoke(null, state);
}
@ReactMethod
public void getStatus(Callback callback) {
WritableMap state = Arguments.createMap();
state.putString("status", isServicePlaying() ? Mode.PLAYING : Mode.STOPPED);
if (signal != null) {
state.putString("streamUrl", signal.getStreamingURL());
}
callback.invoke(null, state);
}

@ReactMethod public void setCurrentPlaybackRate(float speed) {
signal.setPlaybackRate(speed);
Expand Down
8 changes: 4 additions & 4 deletions android/src/main/java/com/audioStreaming/SignalReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import android.util.Log;

class SignalReceiver extends BroadcastReceiver {
private Signal signal;
private SignalService signal;

public SignalReceiver(Signal signal) {
public SignalReceiver(SignalService signal) {
super();
this.signal = signal;
}
Expand All @@ -17,13 +17,13 @@ public SignalReceiver(Signal signal) {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (action.equals(Signal.BROADCAST_PLAYBACK_PLAY)) {
if (action.equals(SignalService.BROADCAST_PLAYBACK_PLAY)) {
if (!this.signal.isPlaying()) {
this.signal.resume();
} else {
this.signal.pause();
}
} else if (action.equals(Signal.BROADCAST_EXIT)) {
} else if (action.equals(SignalService.BROADCAST_EXIT)) {
this.signal.getNotifyManager().cancelAll();
this.signal.stop();
this.signal.exitNotification();
Expand Down
Loading

0 comments on commit 386ce32

Please sign in to comment.