Derek N
Derek N

Reputation: 49

Play sound based on UIImageVIew named image not working

I developed a flashcard app and i have a UIImageView along with gesture clicks to change the picture and a button to play a sound based on the image.

Here is what's happening, i cycle thru all of the images using a number:

        // if the tapped view is a UIImageView then set it to imageview
        if (gesture.view as? UIImageView) != nil {
            if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 0 {
                imageView.image = UIImage(named: "card2")
                imageView.image = UIImage(named: "card\(number)")
                number = number % 26 + 1
            }
            else if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 1 {
                imageView.image = UIImage(named: "upper2")
                imageView.image = UIImage(named: "upper\(number)")
                number = number % 26 + 1
            }
            else if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 2 {
                imageView.image = UIImage(named: "num2")
                imageView.image = UIImage(named: "num\(number)")
                number = number % 10 + 1
            }
}

I then have a button that should play a sound based on the image being displayed in the imageview:

        if imageView.image ==  UIImage(named: "card1") {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: aSound))
                audioPlayer.play()
            } catch {
                print("Couldn't load sound file")
            }
        }
        else if imageView.image !=  UIImage(named: "card2") {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: bSound))
                audioPlayer.play()
            } catch {
                print("Couldn't load sound file")
            }
        }
        else if imageView.image !=  UIImage(named: "card3") {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: cSound))
                audioPlayer.play()
            } catch {
                print("Couldn't load sound file")
            }
        }
}

I have set all the variables for the sound and added them as references to my xcode project:

var audioPlayer = AVAudioPlayer() 
let aSound = Bundle.main.path(forResource: "aSound.m4a", ofType:nil)!
let bSound = Bundle.main.path(forResource: "bSound.m4a", ofType:nil)!
let cSound = Bundle.main.path(forResource: "cSound.m4a", ofType:nil)!

The problem is when i press the sound button, it plays the same sound every time, it's like it never reads thru my if statement.

Upvotes: 0

Views: 73

Answers (1)

Andrew21111
Andrew21111

Reputation: 908

The problem is that when you try to compare images in this way

if imageView.image ==  UIImage(named: "card1") {
            ...
        }

You are actually trying to compare two different Objects which have different references.

In order to achieve your desired behaviour, you can adopt two different solution.

The first one is using the tag property of the view:

if segControl.selectedSegmentIndex == 0 && segControl2.selectedSegmentIndex == 0 {
                imageView.image = UIImage(named: "card2")
                imageView.image = UIImage(named: "card\(number)")
// HERE YOU CAN ADD A TAG
                imageView.tag = 1 //for example, it has to be Int
                number = number % 26 + 1
            }

So you compare tag in this way:

if imageView.tag ==  1 {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: aSound))
                audioPlayer.play()
            } catch {
                print("Couldn't load sound file")
            }
        }

and so on.

The second one is to use isEqual, which should compare hash value of the Objects:

if imageView.image.isEqual(UIImage(named: "")){
            ...
        }

Upvotes: 1

Related Questions