Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
feat: Android interuption (#240)
Browse files Browse the repository at this point in the history
* Add AudioFocusListener, refactor VolumeListener

* Update js and readme

* Cleanup

* Set volume levels correct
  • Loading branch information
roffelund authored and tanguyantoine committed Apr 25, 2019
1 parent 67cd156 commit 7c5fe92
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 161 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ componentDidMount() {
MusicControl.enableBackgroundMode(true);

// on iOS, pause playback during audio interruptions (incoming calls) and resume afterwards.
// As of {{ INSERT NEXT VERSION HERE}} works for android aswell.
MusicControl.handleAudioInterruptions(true);

MusicControl.on('play', ()=> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.tanguyantoine.react;

import android.support.v4.media.RatingCompat;
import android.support.v4.media.session.MediaSessionCompat;

public class MediaSessionCallback extends MediaSessionCompat.Callback {
private final MusicControlEventEmitter emitter;

MediaSessionCallback(MusicControlEventEmitter emitter) {
this.emitter = emitter;
}

@Override
public void onPlay() {
emitter.onPlay();
}

@Override
public void onPause() {
emitter.onPause();
}

@Override
public void onStop() {
emitter.onStop();
}

@Override
public void onSkipToNext() {
emitter.onSkipToNext();
}

@Override
public void onSkipToPrevious() {
emitter.onSkipToPrevious();
}

@Override
public void onSeekTo(long pos) {
emitter.onSeekTo(pos);
}

@Override
public void onFastForward() {
emitter.onFastForward();
}

@Override
public void onRewind() {
emitter.onRewind();
}

@Override
public void onSetRating(RatingCompat rating) {
if(MusicControlModule.INSTANCE == null) return;
int type = MusicControlModule.INSTANCE.ratingType;

if(type == RatingCompat.RATING_PERCENTAGE) {
emitter.onSetRating(rating.getPercentRating());
} else if(type == RatingCompat.RATING_HEART) {
emitter.onSetRating(rating.hasHeart());
} else if(type == RatingCompat.RATING_THUMB_UP_DOWN) {
emitter.onSetRating(rating.isThumbUp());
} else {
emitter.onSetRating(rating.getStarRating());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.tanguyantoine.react;

import android.content.Context;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.os.Build;

import com.facebook.react.bridge.ReactApplicationContext;

public class MusicControlAudioFocusListener implements AudioManager.OnAudioFocusChangeListener {
private final MusicControlEventEmitter emitter;
private final MusicControlVolumeListener volume;

private AudioManager mAudioManager;
private AudioFocusRequest mFocusRequest;

MusicControlAudioFocusListener(ReactApplicationContext context, MusicControlEventEmitter emitter,
MusicControlVolumeListener volume) {
this.emitter = emitter;
this.volume = volume;

this.mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}

@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
emitter.onStop();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
emitter.onPause();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
volume.setCurrentVolume(40);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
if (volume.getCurrentVolume() != 100) {
volume.setCurrentVolume(100);
}
emitter.onPlay();
}
}

public void requestAudioFocus() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(this).build();

mAudioManager.requestAudioFocus(mFocusRequest);
} else {
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
}

public void abandonAudioFocus() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mAudioManager.abandonAudioFocusRequest(mFocusRequest);
} else {
mAudioManager.abandonAudioFocus(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.tanguyantoine.react;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

public class MusicControlEventEmitter {
private static void sendEvent(ReactApplicationContext context, String type, Object value) {
WritableMap data = Arguments.createMap();
data.putString("name", type);

if(value != null) {
if(value instanceof Double || value instanceof Float) {
data.putDouble("value", (double)value);
} else if(value instanceof Boolean) {
data.putBoolean("value", (boolean)value);
} else if(value instanceof Integer) {
data.putInt("value", (int)value);
}
}

context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("RNMusicControlEvent", data);
}

private final ReactApplicationContext context;

MusicControlEventEmitter(ReactApplicationContext context) {
this.context = context;
}

public void onPlay() {
sendEvent(context, "play", null);
}

public void onPause() {
sendEvent(context, "pause", null);
}

public void onStop() {
sendEvent(context, "stop", null);
}

public void onSkipToNext() {
sendEvent(context, "nextTrack", null);
}

public void onSkipToPrevious() {
sendEvent(context, "previousTrack", null);
}

public void onSeekTo(long pos) {
sendEvent(context, "seek", pos / 1000D);
}

public void onFastForward() {
sendEvent(context, "skipForward", null);
}

public void onRewind() {
sendEvent(context, "skipBackward", null);
}

public void onSetRating(float rating) {
sendEvent(context,"setRating", rating);
}
public void onSetRating(boolean hasHeartOrThumb) {
sendEvent(context,"setRating", hasHeartOrThumb);
}

public void onVolumeChange(int volume) { sendEvent(context, "volume", volume); }
}

This file was deleted.

Loading

0 comments on commit 7c5fe92

Please sign in to comment.