New issue
Advanced search Search tips

Issue 784086 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Dec 2017
Cc:
Components:
EstimatedDays: ----
NextAction: 2017-12-20
OS: Android
Pri: 2
Type: Bug



Sign in to add a comment

MediaSession next/previous controls do not work properly when PWA is not in foreground

Reported by chem...@gmail.com, Nov 11 2017

Issue description

Steps to reproduce the problem:
1. Open icerrr2.rejh.nl (or comparable PWA that implements MediaSession notifications.
2. Optional: add it to the home screen
3. Log in, add two radio stations*, select one of them and start playing
4. Put the PWA in the background (go to the home screen) and pull down the notification shade
5. Tap the "next" (or previous) control

* I recommend searching for "NPO Radio 2" and "NPO Radio 3FM", because it offers streams over https (a discussion for another day: how would I support icecast streaming - which rarely support https - in a service worker powered PWA without the content getting blocked :))

What is the expected behavior?
The next track should be loaded (audio.src='...' and played audio.play()*) but it doesn't happen until the PWA is in the foreground again.

* Note: it's possible that, due to handling of media events, pause() is called somewhere. I did try following this guide/article: https://developers.google.com/web/updates/2017/02/media-session

I needed a little more complexity so it is possible that the issue is caused by how the events are handled?

What went wrong?
The stream is paused but the next track is never played. The media notification isn't called either.

Did this work before? N/A 

Does this work in other browsers? N/A

Chrome version: 62.0.3202.84  Channel: stable
OS Version: 8.0
Flash Version: 

Tested on Moto X 2013 Android 6.0 (LineageOS) and Pixel 2016 Android 8.0 (stock)
 

Comment 1 by chem...@gmail.com, Nov 12 2017

I wanted to add some additional info.

The callback for the 'next/previous' controls is the exact same one I use on the main screen (the buttons left and right of the big orange play button). If you tap those, they do load the next station and plays it.

When I tap the notification's next/previous buttons while the app is in the foreground (behind the notification shade) it works, too.

But if I tap either of them while the app is in the background it doesn't work. It loads the station, the metadata/mediasession notification is updated, but the stream doesn't play. I checked the events and the audio element *does* report it's playing (onPlaying event is fired). 

Only when I return to the app, the music starts playing (read: actually making sound)
Labels: Needs-triage-Mobile
Labels: TE-NeedTriageFromMTV
Need to test the issue NPO Rado stations. Could some one from MTV Team please look into the issue and provide an update.
Thank You.
Components: Internals>Media
Labels: -TE-NeedTriageFromMTV
Routing to Media team fro triaging.
Cc: beccahughes@chromium.org mlamouri@chromium.org
OP, do you use iframes to play the radios?

Comment 7 by chem...@gmail.com, Nov 18 2017

@comment6: No it's an audio element embedded in the page. It's created by javascript.

See the app.audio class. I control most of it through methods in app.sections.home, those call app.mediaplayer and that controls app.audio (or app.chromecast, depending on selected output).

I attached a zip with the unbundled, un-uglified sources (note: I use gulp to preprocess everything. It's also set up to become a cordova project but this isn't required or used at this time).
cordova-icerrr2.zip
1.1 MB Download
Cc: -mlamouri@chromium.org
Components: -Internals>Media Internals>Media>Session
Owner: mlamouri@chromium.org
Status: Assigned (was: Unconfirmed)
Cc: fbeaufort@chromium.org
Status: Untriaged (was: Assigned)
I look at it in different angles and couldn't find an obvious issue. It seems that the playback should be starting. One thing I was wondering if you might be creating a new <audio> but it seems like you are not. If you are, try to just recycle the same <audio>. There are a ton of stuff happening when pressing next and I wonder if there is something happening there. FWIW, https://googlechrome.github.io/samples/media-session/audio.html is working so the feature itself isn't broken: it's either an edge case or a problem with the website.

+fbeaufort@ who may have an idea :)

Comment 10 by chem...@gmail.com, Dec 8 2017

Thanks I'll try to contact fbeaufort, I think I follow him on G+ :)

I've been thinking about it and it I came up with two possible causes:

1) I think I pause the audio before I switch stream. It seemed the sensible thing to do. Maybe audio.play() is not allowed/working in the background?

2) When the stream pauses, I disable preload (I think it's preload="none") because if I don't then the audio will just keep buffering the stream - since it's a radio stream it never stops buffering and ends up using a lot of data. Maybe this causes the stream not to start?

PS: I know that at least javascript is executing because it does refresh the "now playing" info and album art (both ajax calls that take a little while) when I press play..
1. there are limitations for playback in the background but if you use an <audio> that previously played in the background, it *should* be fine. I have not checked. I think that for sure if you call play() in the event handle of the media session event handler, it should work.

2. I don't think preload should have any impact.
With Chrome Canary (65.0.3288.3), I can reproduce this issue.

When the app is in foreground, there's no problem. I can hit next multiple times in the media session notification.

When the app is in background, I get a MEDIA_ERR_DECODE which causes the media session notification to disappear. For info, here's the error from about:media-internals: 

    Failed to send audio packet for decoding: timestamp=8208000 duration=24000 size=698 side_data_size=0 is_key_frame=1 encrypted=0 discard_padding (ms)=(0, 0)
Status: Assigned (was: Untriaged)
Cc: mlamouri@chromium.org
NextAction: 2017-12-20
Owner: ----
Status: Available (was: Assigned)
> 1) I think I pause the audio before I switch stream. It seemed the sensible 
> thing to do. Maybe audio.play() is not allowed/working in the background?

If you re-use the same <audio>, you should be able to play it in the background. However, if you create another one, it will not work. It may work if you do this inside the 'nexttrack' event handler though but I guess it may not happen given the amount of JS running (probably async) when the action happens.

> 2) When the stream pauses, I disable preload (I think it's preload="none") 
> because if I don't then the audio will just keep buffering the stream - since 
> it's a radio stream it never stops buffering and ends up using a lot of data. 
> Maybe this causes the stream not to start?

I don't think this should be a problem if you call play() explicitly.

I'm afraid there isn't much else we can do here. I am going to un-assign myself and will close this bug later unless we get more actionable information such as a reduced test case.

Comment 15 by chem...@gmail.com, Dec 19 2017

Ok I found the issue. It's the way I implemented the fadeIn() function (using requestAnimationFrame). That doesn't get called in the background I guess.

So the stream actually works but I set the volume to 0 and then requested an animation frame. Bah. So I guess I have to find some other way to do it.

At least, I just commented out the fadeIn() call and the playback works so it's still an assumption but I'm pretty sure :)

Oh well. Thanks for all the effort and investigating! I guess this issue can be closed.
Status: WontFix (was: Available)
I'm glad you figured it out.
The NextAction date has arrived: 2017-12-20

Sign in to add a comment