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

feat: added support to remote files #141

Merged
merged 2 commits into from
Apr 21, 2017
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 @@ -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
25 changes: 24 additions & 1 deletion android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.media.AudioManager;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
Expand All @@ -15,6 +16,8 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import android.util.Log;

public class RNSoundModule extends ReactContextBaseJavaModule {
Map<Integer, MediaPlayer> playerPool = new HashMap<>();
Expand Down Expand Up @@ -43,7 +46,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 All @@ -56,6 +66,19 @@ protected MediaPlayer createMediaPlayer(final String fileName) {
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - streaming is a good idea over http for sure.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you have a specific reason not to invoke a callback error when the setDataSource throws an exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it usually throws an exception when internet is missing, or when the file does not exist. Do you suggest to handle differently those two? (returning null here, will invoke the error callback anyway later on with a more generic "Resouce not found" error)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheGhoul21 ah - thanks, I didn't notice that it'd hit the error callback anyway. Thanks for the clarification. This seems a sensible solution

}
return mediaPlayer;
}

File file = new File(fileName);
if (file.exists()) {
Uri uri = Uri.fromFile(file);
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