Skip to content

Commit

Permalink
fix regression on notification before first play
Browse files Browse the repository at this point in the history
  • Loading branch information
jgitlin-nypr committed Sep 10, 2024
1 parent 51a8947 commit 1a2177c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public void load() {
dataSourceFactory = new DefaultDataSource.Factory(context);
mediaSession = new MediaSessionCompat(context, "wnyc");

startMediaService();

AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
Expand All @@ -112,7 +110,18 @@ public void play(PluginCall call) {
call.reject("URL is required");
return;
}


if (service == null) {
startMediaService();
while (service == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

handler.post(() -> {
releasePlayer();
player = new ExoPlayer.Builder(getContext()).build();
Expand Down Expand Up @@ -162,8 +171,7 @@ public void onPlaybackStateChanged(int state) {
break;
case Player.STATE_ENDED:
stopUpdatingTime();
service.setPlaybackState(PlaybackStateCompat.STATE_NONE);
service.update();
stop();
notifyListeners("stop", new JSObject());
break;
}
Expand Down Expand Up @@ -249,11 +257,17 @@ private void seekTo(Long position) {

@PluginMethod
public void stop(PluginCall call) {
stop();
call.resolve();
}

public void stop() {
releasePlayer();
service.setPlaybackState(PlaybackStateCompat.STATE_NONE);
service.update();
if (service != null) {
// stop may be called before the service is started
service.destroy();
}
notifyListeners("stop", new JSObject());
call.resolve();
}

@PluginMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.support.v4.media.session.PlaybackStateCompat;

import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.media.session.MediaButtonReceiver;
import androidx.media.app.NotificationCompat.MediaStyle;

Expand Down Expand Up @@ -146,6 +147,8 @@ public void connectAndInitialize(RemoteStreamerPlugin plugin, Intent intent) {

public void destroy() {
stopForeground(true);
//mediaSession.setActive(false);
notificationManager.cancel(NOTIFICATION_ID);
stopSelf();
}

Expand Down Expand Up @@ -218,11 +221,6 @@ public void setPlaybackSpeed(float playbackSpeed) {
@SuppressLint("RestrictedApi")
public void update() {
if (possibleActionsUpdate) {
if (playbackState == PlaybackStateCompat.STATE_NONE) {
mediaSession.setActive(false);
notificationManager.cancel(NOTIFICATION_ID);
return;
}
if (notificationBuilder != null) {
notificationBuilder.mActions.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nypublicradio/capacitor-remote-streamer",
"repository": "git://github.com/nypublicradio/capacitor-remote-streamer.git",
"version": "0.2.5",
"version": "0.2.6",
"description": "Stream remote HLS and MP3 streams on iOS and Android. Capacitor 6.",
"main": "dist/plugin.cjs.js",
"repository": {
Expand Down

0 comments on commit 1a2177c

Please sign in to comment.