-
Notifications
You must be signed in to change notification settings - Fork 121
[Woo POS] [Barcodes] Play error sound when a barcode scan fails #15746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staskus
merged 7 commits into
trunk
from
woomob-579-woo-posbarcodes-play-error-sound-when-a-barcode-scan-fails
Jun 16, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dc2f079
Create PointOfSaleSoundPlayer to play sounds on POS
staskus bdf7ca5
Play barcode scan failure sound
staskus b73d4cd
Add a test for barcode scan failure sound
staskus 60d9bcd
Add credit for sound
joshheald fb6ea6e
Update woo purple in licenses screen
joshheald 12157cb
Merge branch 'trunk' into woomob-579-woo-posbarcodes-play-error-sound…
joshheald 5e43402
Optimize PointOfSaleSoundPlayer
staskus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
WooCommerce/Classes/POS/Utils/Audio/PointOfSaleSoundPlayer.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import Foundation | ||
| import AVFoundation | ||
|
|
||
| struct PointOfSaleSound: Equatable, Hashable { | ||
| let name: String | ||
| let type: String | ||
|
|
||
| static var barcodeScanFailure: PointOfSaleSound { | ||
| PointOfSaleSound(name: "pos_scan_failure", type: "mp3") | ||
| } | ||
| } | ||
|
|
||
| protocol PointOfSaleSoundPlayerProtocol { | ||
| func playSound(_ sound: PointOfSaleSound) async | ||
| } | ||
|
|
||
| final class PointOfSaleSoundPlayer: PointOfSaleSoundPlayerProtocol { | ||
| private var audioPlayer: AVAudioPlayer? | ||
| private var playerCache: [PointOfSaleSound: AVAudioPlayer] = [:] | ||
|
|
||
| @MainActor | ||
| func playSound(_ sound: PointOfSaleSound) async { | ||
| guard let url = Bundle.main.url(forResource: sound.name, withExtension: sound.type) else { | ||
| DDLogError("Sound file not found: \(sound.name).\(sound.type)") | ||
| return | ||
| } | ||
|
|
||
| if let cachedPlayer = playerCache[sound] { | ||
| if !cachedPlayer.isPlaying { | ||
| cachedPlayer.currentTime = 0 | ||
| cachedPlayer.play() | ||
| } | ||
| return | ||
| } | ||
|
|
||
| do { | ||
| audioPlayer = try AVAudioPlayer(contentsOf: url) | ||
| audioPlayer?.prepareToPlay() | ||
| audioPlayer?.play() | ||
| playerCache[sound] = audioPlayer | ||
| } catch { | ||
| DDLogError("Failed to play sound: \(error)") | ||
| } | ||
| } | ||
| } | ||
Binary file not shown.
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
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
8 changes: 7 additions & 1 deletion
8
WooCommerce/WooCommerceTests/POS/Mocks/MockPointOfSaleBarcodeScanService.swift
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
10 changes: 10 additions & 0 deletions
10
WooCommerce/WooCommerceTests/POS/Mocks/MockPointOfSaleSoundPlayer.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import Foundation | ||
| @testable import WooCommerce | ||
|
|
||
| final class MockPointOfSaleSoundPlayer: PointOfSaleSoundPlayerProtocol { | ||
| var onPlaySound: ((PointOfSaleSound) -> Void)? | ||
|
|
||
| func playSound(_ sound: PointOfSaleSound) { | ||
| onPlaySound?(sound) | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider stopping or reusing the existing
audioPlayerbefore creating a new instance to avoid overlapping or resource leakage when sounds are played in rapid succession.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried with this suggestion... but it doesn't really seem to make any difference to how it sounds with lots of rapid failures.
The leak comment is worth thinking about, but I don't really see how it would leak.
It probably doesn't hurt to add this, but I'll leave it up to you @staskus
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a little bit of optimization makes sense here, especially given that if an item is scanned once, it's likely to be scanned multiple times.
Plus, in an extreme case when we scan multiple wrong items in a row fast, it doesn't make sense to stop the previous sound and start another.