3

I'm building an app for iPhone/iPad, and I'm trying to solve a sound problem:

The app comes with a four minute countdown timer. When the coundown reaches 00:00, it triggers a short sound effect. This works fine on the different simulators, but my phone remains silent. The app is built for 7.1. I'm wondering if it's because of the code or if my phone isn't working (it does have sound issues). Does my code look okay? I hope someone can help with this.

Here it is:

import Foundation
import UIKit
import AVFoundation


class VC11 : UIViewController {


    @IBOutlet weak var timerLabel: UILabel!



    var timer = NSTimer()
    var count = 240
    var timerRunning = false
    var audioPlayer = AVAudioPlayer()


    override func viewDidLoad() {
        super.viewDidLoad()


            func nextPage(sender:UISwipeGestureRecognizer) {
                switch sender.direction {

                case UISwipeGestureRecognizerDirection.Left:
                    print("SWIPED LEFT")
                    self.performSegueWithIdentifier("seg11", sender: nil)
                default:
                    break

                }


                var leftSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))
                var rightSwipe = UISwipeGestureRecognizer (target: self, action: Selector("nextPage"))

                leftSwipe.direction = .Left
                rightSwipe.direction = .Right

                view.addGestureRecognizer(leftSwipe)
                view.addGestureRecognizer(rightSwipe)
            }


        }



    func updateTime() {
        count--

            let seconds = count % 60
            let minutes = (count / 60) % 60
            let hours = count / 3600
            let strHours = hours > 9 ? String(hours) : "0" + String(hours)
            let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
            let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
            if hours > 0 {
                timerLabel.text = "\(strHours):\(strMinutes):\(strSeconds)"
            }
            else {
                timerLabel.text = "\(strMinutes):\(strSeconds)"

        }
        stopTimer()


           }
  @IBAction func startTimer(sender: AnyObject) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

    sender.setTitle("Running...", forState: .Normal)

       }


    func stopTimer()

    {

        if count == 0 {
            timer.invalidate()
            timerRunning = false
            timerLabel.text = "04:00"
            playSound()
            count = 240

        }


    }




    func playSound() {

    var soundPath = NSBundle.mainBundle().pathForResource("Metal_Gong", ofType: "wav")
    var soundURL = NSURL.fileURLWithPath(soundPath!)


        self.audioPlayer = AVAudioPlayer(contentsOfURL: soundURL, error: nil)
        self.audioPlayer.play()
    }


}
mojomo
  • 109
  • 11

2 Answers2

6

Make sure that your device is not triggered into silent mode.

Also, for short system sounds you can use System Sounds from AudioToolbox (https://developer.apple.com/library/ios/documentation/AudioToolbox/Reference/SystemSoundServicesReference/):

if let soundURL = NSBundle.mainBundle().URLForResource("Metal_Gong", withExtension: "wav") {
    var mySound: SystemSoundID = 0
    AudioServicesCreateSystemSoundID(soundURL, &mySound)
    AudioServicesPlaySystemSound(mySound);
}
shpasta
  • 1,913
  • 15
  • 21
  • Thanks for the link and code. Where do I add the code? For now I added it to override func viewDidLoad() { super.viewDidLoad() but it's not working either. – mojomo Jan 13 '16 at 17:45
  • Tried adding it to several functions, like in playSound(), alas no luck. – mojomo Jan 13 '16 at 18:00
  • Try to debug it in `updateTime()` function. Print `count` variable by NSLog after `count--` line. Maybe something wrong with the timer, not with the sound. – shpasta Jan 13 '16 at 18:20
  • I wrote println("count") after count-- and it does print count four times – mojomo Jan 13 '16 at 18:32
  • Only four? It has to display 240 logs, one for each second (from 239 to 0) – shpasta Jan 13 '16 at 18:36
  • sorry, i checked again - there are 240 – mojomo Jan 13 '16 at 18:39
  • I found this: http://www.globalnerdy.com/2015/07/06/how-to-fix-the-common-no-sound-from-avplayer-avaudioplayer-problem-in-ios-swift-programming/ - could that be the problem? – mojomo Jan 13 '16 at 18:50
  • Check this one, it's about sound file name http://stackoverflow.com/questions/16903309/system-sound-plays-in-iphone-simulator-but-on-iphone-not-playing – shpasta Jan 13 '16 at 19:11
  • nope. everything spelled correctly. i now changed my file to metalGong.wav to test if _ causes a problem, but it's all the same. – mojomo Jan 13 '16 at 19:44
  • #facepalm Didn't even think to check silent switch. Nothing else seems to respect it so I forgot it was on. – AnthonyW Oct 05 '16 at 14:07
0

I just found out that my code does work - it's only because of my broken phone that no sound is played. Thank you for your help!

mojomo
  • 109
  • 11