8.5 C
Canberra
Thursday, October 23, 2025

ios – SwiftUI right option to play sounds


My IOS Utility has mp3 information within the challenge. I exploit AVAudioPlayer to play the sounds. For many customers and for all of our take a look at telephones it really works wonderful. However just a few customers complain they do not hear any sounds. I’ve requested them to verify their quantity is up and the machine shouldn’t be muted and so they have confirmed that. I additionally launched a debug model to them through test-flight that prints out a message when the sound ought to play to inform me if one thing went unsuitable. It prints the sound was performed.

I am undecided why it could work on most IOS units however not others. I’ve examined it on the identical IOS 18 model and iPhone model one of many customers is seeing it on however I cannot reproduce it.

Right here is my play sound methodology. Once they run the debug model they see “Sound was Performed”. So I do know it is discovering the file and no exceptions have been generated and the sound file was discovered. Undecided what to take a look at subsequent.

The sound information are mp3 and saved within the root challenge listing. However I am unable to think about the situation is the issue or I might have seen “Sound File couldn’t be discovered” printed out.

import Basis
import AVFoundation
import UIKit

public var muteSounds: Bool = false
var audioPlayer:AVAudioPlayer?

struct SoundService {

func playSound(soundfile: String) -> String{
    
    if (!muteSounds) {
        
        if let path = Bundle.fundamental.path(forResource: soundfile, ofType: nil){
            do{
                audioPlayer = strive AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
                audioPlayer?.prepareToPlay()
                audioPlayer?.play()
                return "Sound was performed"
            }catch {
                print("Error attempting to play sound")
                return("Error attempting to play sound")
            }
        } else {
            return "Sound File couldn't be discovered"
        }
        
    } else {
        return "Sounds are muted"
    }
 }

}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles