Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
709 views
in Technique[技术] by (71.8m points)

ios - Issue with "try?" and AVAudioPlayer

I am having the following issue with AVAudioPlayer:

import Foundation   //Needed for dispatch_once_t
import AVFoundation //Needed to play sounds

class PlayStartSong {

    var song: AVAudioPlayer = AVAudioPlayer()
    var songStarted: Bool = false

    class var sharedInstance: PlayStartSong {
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: PlayStartSong? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = PlayStartSong()
        }
        return Static.instance!
    }

    func prepareAudios() {

        var path = NSBundle.mainBundle().pathForResource("start-2.0", ofType: "mp3")
        song = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))    //Error Here
        song.prepareToPlay()

        song.numberOfLoops = -1 //Makes the song play repeatedly
    }
}

On the line that assigns the value of the variable "song" in the "prepareAudios" function, I am getting the following error after converting to Swift 2.0:

Value of optional type 'AVAudioPLayer?' not unwrapped; did you mean to use '!' or '?'?

However, upon using the suggested fix, it is telling me to remove the exclamation point I just added. What is the exact issue here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To use try? as you intended your song variable has to be an Optional:

class PlayStartSong {

    var song: AVAudioPlayer?
    var songStarted: Bool = false

    class var sharedInstance: PlayStartSong {
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: PlayStartSong? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = PlayStartSong()
        }
        return Static.instance!
    }

    func prepareAudios() {
        let path = NSBundle.mainBundle().pathForResource("start-2.0", ofType: "mp3")
        song = try? AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))
        song?.prepareToPlay()
        song?.numberOfLoops = -1 //Makes the song play repeatedly
    }
}

To not make it an Optional, you would use try inside do catch:

func prepareAudios() {
    do {
        let path = NSBundle.mainBundle().pathForResource("start-2.0", ofType: "mp3")
        song = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))
        song.prepareToPlay()
        song.numberOfLoops = -1 //Makes the song play repeatedly
    } catch {
        print(error)
    }
}

Also, if you are absolutely sure that creating the AVAudioPlayer instance will always succeed, you can ignore "do catch" by using try!:

func prepareAudios() {
    let path = NSBundle.mainBundle().pathForResource("start-2.0", ofType: "mp3")
    song = try! AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: path!))
    song.prepareToPlay()
    song.numberOfLoops = -1 //Makes the song play repeatedly
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...