Skip to content
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

volume control hassles #43

Closed
artangco opened this issue Dec 22, 2018 · 2 comments
Closed

volume control hassles #43

artangco opened this issue Dec 22, 2018 · 2 comments

Comments

@artangco
Copy link

Trying to do volume control when playing but the only way to do that is through creating a new instance that I then have to keep track of since if it goes out of scope it doesn't play. Perhaps I'm doing it wrong. Ideally I would like to do something like Sound.play(url: url, withVolume:0.3 ....

Is this possible?

@adamcichy
Copy link
Owner

adamcichy commented Dec 22, 2018

Creating an instance is currently the only option to control the volume. However, you could overcome this with a simple wrapper for your sounds, like the following:

struct Sounds {

    static var globalVolume: Float = 0.5 {
        didSet {
            mySound1?.volume = globalVolume
            //add other sounds here
        }
    }

    static var mySound1: Sound? = {
        if let url = Bundle.main.url(forResource: "myFile", withExtension: "wav") {
            let sound = Sound(url: url)
            sound?.volume = Sounds.globalVolume
            return sound
        }
        return nil
    }()

    //add more sounds here
}

Then the usage is as simple as this:

    Sounds.mySound1?.play()

And to change the volume:

   Sounds.globalVolume = 0.6

@artangco
Copy link
Author

Thank you. I made a wrapper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants