This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add AudioFocusListener, refactor VolumeListener * Update js and readme * Cleanup * Set volume levels correct
- Loading branch information
1 parent
67cd156
commit 7c5fe92
Showing
8 changed files
with
279 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
android/src/main/java/com/tanguyantoine/react/MediaSessionCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
android/src/main/java/com/tanguyantoine/react/MusicControlAudioFocusListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
android/src/main/java/com/tanguyantoine/react/MusicControlEventEmitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); } | ||
} |
135 changes: 0 additions & 135 deletions
135
android/src/main/java/com/tanguyantoine/react/MusicControlListener.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.