Skip to content
Open
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
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 26
buildToolsVersion "26.0.3"

defaultConfig {
minSdkVersion 16
Expand Down
58 changes: 51 additions & 7 deletions android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.media.AudioManager;
import android.app.Activity;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
Expand All @@ -23,7 +24,14 @@
import java.util.Map;
import java.io.IOException;

// AH
import android.util.Log;
import com.facebook.react.modules.network.ForwardingCookieHandler;
import java.util.List;
import java.util.Iterator;
import java.net.URISyntaxException;
import java.net.URI;
//

public class RNSoundModule extends ReactContextBaseJavaModule implements AudioManager.OnAudioFocusChangeListener {
Map<Double, MediaPlayer> playerPool = new HashMap<>();
Expand All @@ -33,11 +41,13 @@ public class RNSoundModule extends ReactContextBaseJavaModule implements AudioMa
Boolean mixWithOthers = true;
Double focusedPlayerKey;
Boolean wasPlayingBeforeFocusChange = false;
private ForwardingCookieHandler mCookieHandler;

public RNSoundModule(ReactApplicationContext context) {
super(context);
this.context = context;
this.category = null;
this.mCookieHandler = new ForwardingCookieHandler(context);
}

private void setOnPlay(boolean isPlaying, final Double playerKey) {
Expand All @@ -54,6 +64,8 @@ public String getName() {

@ReactMethod
public void prepare(final String fileName, final Double key, final ReadableMap options, final Callback callback) {


MediaPlayer player = createMediaPlayer(fileName);
if (player == null) {
WritableMap e = Arguments.createMap();
Expand Down Expand Up @@ -147,7 +159,10 @@ public synchronized boolean onError(MediaPlayer mp, int what, int extra) {

protected MediaPlayer createMediaPlayer(final String fileName) {
int res = this.context.getResources().getIdentifier(fileName, "raw", this.context.getPackageName());

MediaPlayer mediaPlayer = new MediaPlayer();

//MediaPlayer mediaPlayer = MediaPlayer.create(this.getCurrentActivity(), fileName);
if (res != 0) {
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(res);
Expand All @@ -164,8 +179,13 @@ protected MediaPlayer createMediaPlayer(final String fileName) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.i("RNSoundModule", fileName);
try {
mediaPlayer.setDataSource(fileName);
} catch(IOException e) {

Map<String, String> cookieHeader = getCookieHeader(fileName);

Uri uri = Uri.parse(fileName);
mediaPlayer.setDataSource(this.context, uri, cookieHeader);

} catch(Exception e) {
Log.e("RNSoundModule", "Exception", e);
return null;
}
Expand Down Expand Up @@ -200,6 +220,30 @@ protected MediaPlayer createMediaPlayer(final String fileName) {
return null;
}

private Map getCookieHeader(String fileName) {
Map<String, String> cookieHeader = new HashMap<>();
try {
URI cookieURI = new URI(fileName);
Map<String, List<String>> cookieMap = mCookieHandler.get(cookieURI, new HashMap<String, List<String>>());
Log.i("RNSoundModule", "cookieMap: " + cookieMap);

Object c[] = cookieMap.get("Cookie").toArray();
String path = cookieURI.getPath();
String host = cookieURI.getHost();
List list = cookieMap.get("Cookie");

Log.i("RNSoundPlayer", "Cookie list: " + list);
for (Iterator<String> it = list.iterator(); it.hasNext();) {
String cookeValue = it.next();
cookieHeader.put("Cookie", "DOMAIN="+ host + "; " + "PATH=" + path + "; " + cookeValue + "\r\n");
}
}
catch (Exception e) {
Log.e("RNSoundModule", "Exception", e);
}
return cookieHeader;
}

@ReactMethod
public void play(final Double key, final Callback callback) {
MediaPlayer player = this.playerPool.get(key);
Expand Down Expand Up @@ -352,10 +396,10 @@ public void setLooping(final Double key, final Boolean looping) {

@ReactMethod
public void setSpeed(final Double key, final Float speed) {
if (android.os.Build.VERSION.SDK_INT < 23) {
Log.w("RNSoundModule", "setSpeed ignored due to sdk limit");
return;
}
if (android.os.Build.VERSION.SDK_INT < 23) {
Log.w("RNSoundModule", "setSpeed ignored due to sdk limit");
return;
}

MediaPlayer player = this.playerPool.get(key);
if (player != null) {
Expand Down Expand Up @@ -436,4 +480,4 @@ public Map<String, Object> getConstants() {
constants.put("IsAndroid", true);
return constants;
}
}
}