Skip to content

Commit

Permalink
Merge pull request #273 from jkimbo/android-set-category
Browse files Browse the repository at this point in the history
Android set category
  • Loading branch information
trepidity committed Oct 3, 2017
2 parents cc107e5 + 113cdcc commit a6ca0d2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -63,7 +63,7 @@ First you'll need to add audio files to your project.
// Import the react-native-sound module
var Sound = require('react-native-sound');

// Enable playback in silence mode (iOS only)
// Enable playback in silence mode
Sound.setCategory('Playback');

// Load the sound file 'whoosh.mp3' from the app bundle
Expand Down
44 changes: 40 additions & 4 deletions android/src/main/java/com/zmxv/RNSound/RNSoundModule.java
Expand Up @@ -28,10 +28,12 @@ public class RNSoundModule extends ReactContextBaseJavaModule {
Map<Integer, MediaPlayer> playerPool = new HashMap<>();
ReactApplicationContext context;
final static Object NULL = null;
String category;

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

@Override
Expand All @@ -51,6 +53,27 @@ public void prepare(final String fileName, final Integer key, final ReadableMap

final RNSoundModule module = this;

if (module.category != null) {
Integer category = null;
switch (module.category) {
case "Playback":
category = AudioManager.STREAM_MUSIC;
break;
case "Ambient":
category = AudioManager.STREAM_NOTIFICATION;
break;
case "System":
category = AudioManager.STREAM_SYSTEM;
break;
default:
Log.e("RNSoundModule", String.format("Unrecognised category %s", module.category));
break;
}
if (category != null) {
player.setAudioStreamType(category);
}
}

player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
boolean callbackWasCalled = false;

Expand Down Expand Up @@ -102,11 +125,20 @@ 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();
if (res != 0) {
return MediaPlayer.create(this.context, res);
try {
AssetFileDescriptor afd = context.getResources().openRawResourceFd(res);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
} catch (IOException e) {
Log.e("RNSoundModule", "Exception", e);
return null;
}
return mediaPlayer;
}
if(fileName.startsWith("http://") || fileName.startsWith("https://")) {
MediaPlayer mediaPlayer = new MediaPlayer();

if (fileName.startsWith("http://") || fileName.startsWith("https://")) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.i("RNSoundModule", fileName);
try {
Expand All @@ -121,7 +153,6 @@ protected MediaPlayer createMediaPlayer(final String fileName) {
if (fileName.startsWith("asset:/")){
try {
AssetFileDescriptor descriptor = this.context.getAssets().openFd(fileName.replace("asset:/", ""));
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
return mediaPlayer;
Expand Down Expand Up @@ -292,6 +323,11 @@ public void setSpeakerphoneOn(final Integer key, final Boolean speaker) {
}
}

@ReactMethod
public void setCategory(final String category, final Boolean mixWithOthers) {
this.category = category;
}

@ReactMethod
public void enable(final Boolean enabled) {
// no op
Expand Down
2 changes: 1 addition & 1 deletion sound.js
Expand Up @@ -209,7 +209,7 @@ Sound.setActive = function(value) {
};

Sound.setCategory = function(value, mixWithOthers = false) {
if (!IsAndroid && !IsWindows) {
if (!IsWindows) {
RNSound.setCategory(value, mixWithOthers);
}
};
Expand Down

0 comments on commit a6ca0d2

Please sign in to comment.