You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Apply MediaElement.Speed when playback starts on iOS and fix inverted AreFloatingPointNumbersEqual - #3327
Fixes two related defects in MediaElement described in #3326.
1. iOS / Mac Catalyst: playback starts at 1x regardless of Speed
AVPlayer.Play() always starts playback at a rate of 1. MediaManager.macios.cs called it from PlatformPlay, from the auto-play path in PlatformUpdateSource and from PlayedToEnd when looping, so each of those reset playback to 1x while MediaElement.Speed kept the requested value.
These three call sites now use a new PlayAtSpeed() helper, which starts playback by setting Player.Rate to MediaElement.Speed (and falls back to Player.Play() when Speed is 0).
2. MediaManager.AreFloatingPointNumbersEqual was inverted
It returned Math.Abs(number1 - number2) > tolerance, i.e. true when the numbers differ. It now returns true when they are within the tolerance. Call sites:
Call site
Before
After
iOS RateChanged
Never copied the player's rate into Speed
Copies it when it differs. A rate of 0 is ignored, because AVPlayer reports 0 while paused and that is not a change of the requested speed
Windows OnPlaybackSessionPlaybackRateChanged
Correct, because it relied on the inverted result
Negated, so behavior is unchanged
Android OnPlaybackParametersChanged
Never updated Speed
Updates Speed when the player's speed differs
Android PlatformUpdateShouldMute
Refreshed volumeBeforeMute only when it already equalled Player.Volume, so a Volume set before the handler connected was overwritten with 1
Refreshes it when it differs, so the initial Volume is kept
Has tests (if omitted, state reason in description) — unit tests added for AreFloatingPointNumbersEqual. The iOS and Android playback changes have no automated test; they were verified on a simulator and an emulator, see below
Has samples (if omitted, state reason in description) — n/a, bug fix with no new API
Documentation created or updated — n/a, no API change
Additional information
Verification
The same scripted scenarios were run against main (c8740a8) and this branch, using a small test app that references the MediaElement project and logs state, position and Speed.
ShouldAutoPlay = false, Speed = 2, then Play(); position after 3s
2.81s
5.84s
ShouldAutoPlay = false, Speed = 1.5, then Play() (remote MP4); position after 3s
2.80s
4.10s
Playing at Speed = 2, Pause(), Play(); media played in the next 2s
1.91s
3.75s
Playing at Speed = 2, Stop(), Play(); position after 2s
1.83s
3.75s
ShouldAutoPlay = true with initial Speed = 2; position after 2s
1.76s
3.54s
ShouldLoopPlayback = true at Speed = 2 (3.89s MP3); time per loop
about 4s
about 2s
Speed reported 2 (or 1.5) throughout on both builds. Pause() leaves Speed unchanged with this PR. Fourteen further scenarios (auto-play on and off, source changes, invalid URL, HLS, audio, Source = null, play to end) produce the same event and state sequence on both builds, apart from the 2x auto-play case now reaching MediaEnded sooner.
Android (Android 17 emulator), one run per build:
Speed change, pause / resume, Speed = 0 then Speed = 1.5, and mute / unmute with volume changes behave identically on both builds.
A MediaElement created with Volume = 0.3 reports Volume 1.00 on main and 0.30 with this PR.
Builds and tests (.NET SDK 10.0.401, Xcode 26.6): CommunityToolkit.Maui.MediaElement builds in Release for net10.0, Android, iOS and Mac Catalyst with no warnings from the changed files; CommunityToolkit.Maui.UnitTests passes (11,679 tests, including the 15 added here).
Not verified
Windows: the one-line change is a negation that preserves existing behavior, but it was neither compiled nor run (no Windows machine).
Mac Catalyst: compiles, not run.
Physical devices.
The iOS rate-to-Speed sync driven by the native player controls' speed menu, which this PR makes functional, was not exercised by hand.
Notes for reviewers
AreFloatingPointNumbersEqual is public static, so the corrected return value is visible to anyone calling it directly.
Not changed here: on iOS, setting Speed while paused starts playback, because PlatformUpdateSpeed assigns Player.Rate directly. That is existing behavior on main.
#3281 also edits MediaManager.macios.cs. The two PRs touch different things except for one spot: the auto-play start. On main that call lives in PlatformUpdateSource; this PR changes it in place to PlayAtSpeed(), and #3281 moves it into a new PlayerItemStatusChanged method that runs after ReadyToPlay. Git will report a conflict there for whichever PR merges second.
Resolution, in either order: keep #3281's structure and start playback with this PR's helper, so the block in PlayerItemStatusChanged reads:
The replacement is required, not optional. Resolving the conflict by keeping the Player.Play(); Player.Rate = (float)MediaElement.Speed; pair was also tested: with auto-play and Speed = 2, Speed is overwritten with 1 and playback runs at 1x (3.03s after 4s). #3327 makes RateChanged copy the player's rate into MediaElement.Speed, and Player.Play() sets the rate to 1 before the pair reapplies it.
The other changes here (PlatformPlay, the loop restart, RateChanged, the helper and its Windows call site) do not overlap with #3281.
… AreFloatingPointNumbersEqual
AVPlayer.Play() always starts playback at a rate of 1, so Play(), auto-play
and loop restarts ignored MediaElement.Speed on iOS and Mac Catalyst. They now
start playback by setting the rate to the requested Speed.
AreFloatingPointNumbersEqual returned true when the numbers differed. It now
returns true when they are within the tolerance. The Windows call site is
negated to keep its behavior; the iOS RateChanged handler now ignores the
rate of 0 that AVPlayer reports while paused.
FixesCommunityToolkit#3326
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rting playback
RateChanged now guards only the Speed assignment with the equality check, so
a nonzero rate change always updates NowPlayingInfo.PlaybackRate, as it did
before the helper was corrected.
PlayAtSpeed falls back to Player.Play() only when Speed is exactly 0, so a
negative Speed is applied instead of restarting forward at 1x.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Lock-screen and Control Center Play still bypass this helper: Metadata.PlayCommand calls player.Play() (Primitives/Metadata.macios.cs:151), which starts at rate 1. The corrected comparison in RateChanged now copies that rate into MediaElement.Speed, so resuming with Speed = 2 loses the requested speed; subsequent application Play() calls also use 1 unless the app resets Speed. Pass a speed-aware playback callback into Metadata and use it for remote Play, while retaining synchronization for genuine native speed changes.
Lock screen and Control Center Play called AVPlayer.Play(), which always
starts at a rate of 1, and RateChanged then copied that rate back into
MediaElement.Speed. Route the remote Play and toggle commands through
PlayAtSpeed instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Re: Copilot review, "Remote Play ignores the configured playback speed".
Valid, fixed in 29938a0. Metadata now takes a speed-aware play callback and MediaManager passes PlayAtSpeed to it, so the remote Play command (and the play branch of the toggle command) no longer calls player.Play() and starts at the requested Speed. RateChanged is unchanged, so genuine native speed changes still sync back to MediaElement.Speed.
Verified on a physical iPad Air 13-inch (M4) with Speed = 2, sending the system remote Play command while paused and measuring how fast the position advanced:
MediaManager.macios.cs: keep main's early return in PlatformUpdateSource;
the PR's MediaOpened/GetVideoDimensions/PlayAtSpeed/SetPoster block there
is superseded by CommunityToolkit#3281's PlayerItemStatusChanged.
Autoplay in PlayerItemStatusChanged now calls PlayAtSpeed() instead of
Play() then Rate = speed, whose two rate changes made RateChanged flip
Speed between 1 and the requested value and stall playback.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
RateChanged copies the player's rate into MediaElement.Speed, and
PlatformUpdateSpeed sent that value back to the player as a new rate
request. When two rates are set in one main-loop turn, AVPlayer reports
the earlier rate again afterwards, so the two requests kept replacing each
other: Speed flipped about every 40 ms and playback stalled. This happened
when Speed was set in a MediaOpened handler of an autoplaying element
(autoplay starts in the same turn), or when Speed changed twice, or Play()
and a Speed change ran, in one main-loop turn. PlatformUpdateSpeed now
skips the update when the player already runs at the requested Speed.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Change
Fixes two related defects in
MediaElementdescribed in #3326.1. iOS / Mac Catalyst: playback starts at 1x regardless of
SpeedAVPlayer.Play()always starts playback at a rate of 1.MediaManager.macios.cscalled it fromPlatformPlay, from the auto-play path inPlatformUpdateSourceand fromPlayedToEndwhen looping, so each of those reset playback to 1x whileMediaElement.Speedkept the requested value.These three call sites now use a new
PlayAtSpeed()helper, which starts playback by settingPlayer.RatetoMediaElement.Speed(and falls back toPlayer.Play()whenSpeedis 0).2.
MediaManager.AreFloatingPointNumbersEqualwas invertedIt returned
Math.Abs(number1 - number2) > tolerance, i.e.truewhen the numbers differ. It now returnstruewhen they are within the tolerance. Call sites:RateChangedSpeedAVPlayerreports 0 while paused and that is not a change of the requested speedOnPlaybackSessionPlaybackRateChangedOnPlaybackParametersChangedSpeedSpeedwhen the player's speed differsPlatformUpdateShouldMutevolumeBeforeMuteonly when it already equalledPlayer.Volume, so aVolumeset before the handler connected was overwritten with 1Volumeis keptLinked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal) — [BUG] iOS: MediaElement plays at 1x after Play(), auto-play or loop while Speed reports the requested value #3326 is linked and not yet approvedAreFloatingPointNumbersEqual. The iOS and Android playback changes have no automated test; they were verified on a simulator and an emulator, see belowmainat time of PRAdditional information
Verification
The same scripted scenarios were run against
main(c8740a8) and this branch, using a small test app that references the MediaElement project and logs state, position andSpeed.iOS (iPhone 17 simulator, iOS 26.5), 24.87s local MP4 unless noted:
mainShouldAutoPlay = false,Speed = 2, thenPlay(); position after 3sShouldAutoPlay = false,Speed = 1.5, thenPlay()(remote MP4); position after 3sSpeed = 2,Pause(),Play(); media played in the next 2sSpeed = 2,Stop(),Play(); position after 2sShouldAutoPlay = truewith initialSpeed = 2; position after 2sShouldLoopPlayback = trueatSpeed = 2(3.89s MP3); time per loopSpeedreported 2 (or 1.5) throughout on both builds.Pause()leavesSpeedunchanged with this PR. Fourteen further scenarios (auto-play on and off, source changes, invalid URL, HLS, audio,Source = null, play to end) produce the same event and state sequence on both builds, apart from the 2x auto-play case now reachingMediaEndedsooner.Android (Android 17 emulator), one run per build:
Speed = 0thenSpeed = 1.5, and mute / unmute with volume changes behave identically on both builds.MediaElementcreated withVolume = 0.3reportsVolume1.00 onmainand 0.30 with this PR.Builds and tests (.NET SDK 10.0.401, Xcode 26.6):
CommunityToolkit.Maui.MediaElementbuilds in Release fornet10.0, Android, iOS and Mac Catalyst with no warnings from the changed files;CommunityToolkit.Maui.UnitTestspasses (11,679 tests, including the 15 added here).Not verified
Speedsync driven by the native player controls' speed menu, which this PR makes functional, was not exercised by hand.Notes for reviewers
AreFloatingPointNumbersEqualispublic static, so the corrected return value is visible to anyone calling it directly.Speedwhile paused starts playback, becausePlatformUpdateSpeedassignsPlayer.Ratedirectly. That is existing behavior onmain.Overlap with #3281
#3281 also edits
MediaManager.macios.cs. The two PRs touch different things except for one spot: the auto-play start. Onmainthat call lives inPlatformUpdateSource; this PR changes it in place toPlayAtSpeed(), and #3281 moves it into a newPlayerItemStatusChangedmethod that runs afterReadyToPlay. Git will report a conflict there for whichever PR merges second.Resolution, in either order: keep #3281's structure and start playback with this PR's helper, so the block in
PlayerItemStatusChangedreads:PlatformUpdateSourcehunk, and replaces Fix ios media element start bug #3281'sPlayer.Play(); Player.Rate = (float)MediaElement.Speed;pair withPlayAtSpeed().Verified locally (iPhone 17 simulator, iOS 26.5). Both branches were merged onto
main(c8740a8) with the resolution above:CommunityToolkit.Maui.UnitTestspasses (11,679 tests).Speed = 2,Play()without auto-play: 5.70s of media after 3s; auto-play atSpeed = 2: 3.47s after 2s).The replacement is required, not optional. Resolving the conflict by keeping the
Player.Play(); Player.Rate = (float)MediaElement.Speed;pair was also tested: with auto-play andSpeed = 2,Speedis overwritten with 1 and playback runs at 1x (3.03s after 4s). #3327 makesRateChangedcopy the player's rate intoMediaElement.Speed, andPlayer.Play()sets the rate to 1 before the pair reapplies it.The other changes here (
PlatformPlay, the loop restart,RateChanged, the helper and its Windows call site) do not overlap with #3281.🤖 Generated with Claude Code