Skip to content

Commit

Permalink
feat: added support to remote files (zmxv#141)
Browse files Browse the repository at this point in the history
* feat: added support to remote files

- Sound.js now checks if is RelativePath using also http and https
- RNSoundModule now uses setAudioStreamType(AudioManager.STREAM_MUSIC)
to stream music
- Exception now are handled differently

* fix: amended README specifying support for android network streaming
  • Loading branch information
TheGhoul21 authored and aidan-doherty committed Oct 26, 2020
1 parent a246d97 commit 62b00ab
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Feature | iOS | Android | Windows
---|---|---|---
Load sound from the app bundle | ✓ | ✓ | ✓
Load sound from other directories | ✓ | ✓ | ✓
Load sound from the network | ✓ | |
Load sound from the network | ✓ | |
Play sound | ✓ | ✓ | ✓
Playback completion callback | ✓ | ✓ | ✓
Pause | ✓ | ✓ | ✓
Expand Down
44 changes: 43 additions & 1 deletion android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.net.Uri;
import android.content.res.AssetFileDescriptor;
import android.util.Log;
import android.media.AudioManager;

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

public class RNSoundModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
Map<Integer, MediaPlayer> playerPool = new HashMap<>();
Expand Down Expand Up @@ -53,7 +55,14 @@ public void prepare(final String fileName, final Integer key, final Callback cal
}
try {
player.prepare();
} catch (Exception e) {
} catch (Exception exception) {
Log.e("RNSoundModule", "Exception", exception);

WritableMap e = Arguments.createMap();
e.putInt("code", -1);
e.putString("message", exception.getMessage());
callback.invoke(e);
return;
}
this.playerPool.put(key, player);
WritableMap props = Arguments.createMap();
Expand Down Expand Up @@ -111,6 +120,39 @@ protected MediaPlayer createMediaPlayer(final String fileName) {
Uri uri = Uri.fromFile(file);
return MediaPlayer.create(this.context, uri);
}
int res = this.context.getResources().getIdentifier(fileName, "raw", this.context.getPackageName());
if (res != 0) {
return MediaPlayer.create(this.context, res);
}
if(fileName.startsWith("http://") || fileName.startsWith("https://")) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.i("RNSoundModule", fileName);
try {
mediaPlayer.setDataSource(fileName);
} catch(IOException e) {
Log.e("RNSoundModule", "Exception", e);
return null;
}
return mediaPlayer;
}

if (fileName.startsWith("asset:/")){
try {
AssetFileDescriptor descriptor = this.context.getAssets().openFd(fileName.replace("asset:/", ""));
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
return mediaPlayer;
} catch(IOException e) {
Log.e("RNSoundModule", "Exception", e);
return null;
}
}

File file = new File(fileName);
if (file.exists()) {
Uri uri = Uri.fromFile(file);
return MediaPlayer.create(this.context, uri);
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSourc
var nextKey = 0;

function isRelativePath(path) {
return !/^\//.test(path);
return !/^(\/|http(s?))/.test(path);
}

function Sound(filename, basePath, onError) {
Expand Down

0 comments on commit 62b00ab

Please sign in to comment.