Bug 221413 - Safari does not execute JavaScript code to play another audio item when playing backgrounded FairPlay content
Summary: Safari does not execute JavaScript code to play another audio item when playi...
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Audio (show other bugs)
Version: Safari 14
Hardware: iPhone / iPad iOS 14
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-02-04 09:12 PST by Tom Spence
Modified: 2022-12-16 12:01 PST (History)
6 users (show)

See Also:


Attachments
Demonstrates the bug in iOS Safari (1.87 KB, text/html)
2022-05-06 14:17 PDT, Brad Isbell
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Spence 2021-02-04 09:12:13 PST
Hello, we are currently attempting to get Safari to automatically move on to the next audio track when a track ends (by subscribing to the "ended" event of the media element), when the browser is in the background.  We are doing this by executing JavaScript that replaces the src attribute with a new HLS manifest URL. Currently playback terminates at the end of the first track and does not progress to the second.  No error is raised.

The expected behaviour would be that playback continues with the next audio track.

Could you assist?
Comment 1 Alexey Proskuryakov 2021-02-07 10:51:23 PST
Tba kn you for the report! Could you please provide a test case?
Comment 2 Tom Spence 2021-02-09 07:18:32 PST
(In reply to Alexey Proskuryakov from comment #1)
> Thank you for the report! Could you please provide a test case?

Reproduction steps
1. Open Safari on an iOS device
2. Go to URL: https://spoti.fi/harmony-doctor
3. Log in with a Spotify user.
4. Start playback on a playlist, album, or other context with multiple tracks.
5. Wait for playback to start
6. Background Safari
7. Wait for the first song to end

Expected outcome
The second song should play

Actual outcome
Playback stop
Comment 3 Radar WebKit Bug Importer 2021-02-11 09:13:14 PST
<rdar://problem/74236954>
Comment 4 Brad Isbell 2022-05-06 14:17:23 PDT
Created attachment 458973 [details]
Demonstrates the bug in iOS Safari

This bug is still an issue, and is also a problem for regular audio files without DRM.

Attached is a page with some script demonstrating the issue.

Observations:

 - The `ended` event does fire and its handlers do run, even when the screen is off.
 - Upon replacing the `src` of the `<audio>` element, it cannot be played via script when the screen is off.
 - The `paused` property value is `false`, `currentTime` is `0` zero.  Audio playback does not occur.
 - The only way to resume playback is to unlock phone, and use a UI element to call `.play()` on the audio element.

This prevents web apps from playing from a playlist of media.  It also seems to be a regression, as this method used to work.  This method is also documented here:  https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/ControllingMediaWithJavaScript/ControllingMediaWithJavaScript.html#//apple_ref/doc/uid/TP40009523-CH3-SW5
Comment 5 Peter Fernandes 2022-10-01 14:16:57 PDT
(In reply to Brad Isbell from comment #4)
> Created attachment 458973 [details]
> Demonstrates the bug in iOS Safari
> 
> This bug is still an issue, and is also a problem for regular audio files
> without DRM.
> 
> Attached is a page with some script demonstrating the issue.
> 
> Observations:
> 
>  - The `ended` event does fire and its handlers do run, even when the screen
> is off.
>  - Upon replacing the `src` of the `<audio>` element, it cannot be played
> via script when the screen is off.
>  - The `paused` property value is `false`, `currentTime` is `0` zero.  Audio
> playback does not occur.
>  - The only way to resume playback is to unlock phone, and use a UI element
> to call `.play()` on the audio element.
> 
> This prevents web apps from playing from a playlist of media.  It also seems
> to be a regression, as this method used to work.  This method is also
> documented here: 
> https://developer.apple.com/library/archive/documentation/AudioVideo/
> Conceptual/Using_HTML5_Audio_Video/ControllingMediaWithJavaScript/
> ControllingMediaWithJavaScript.html#//apple_ref/doc/uid/TP40009523-CH3-SW5

Brad, are you doing any potentially long-running async operations (fetch, etc.) before the next track starts playing? I have this use case working in my own app. Playback of a user playlist works continuously with the Safari browser tab backgrounded on desktop, or PWA backgrounded on iOS.
Comment 6 Michael 2022-12-16 12:01:25 PST
I have been searching for a solution to this for the past 11 months, as Tom stated, all of my attempts to replace a media (<audio>) source sequentially while in the background (or in locked screen) have failed until recently when I  updated iOS. 

The issue appears to have been fixed. I made no code changes to my app and it just began working after updating to iOS 15.7.2 (The fix may have been prior to this particular version however since I was on 15.5x) I've tried to look for more information in the release notes but haven't been able to spot the exact change that resolved this issue. 

Very happy about this fix.