如何在单例方法中实现 AVAudioPlayer?

How to implement AVAudioPlayer Inside Singleton Method?

这是我的代码:

class SomeAudioManager: NSObject
{

    class var sharedInstance: SomeAudioManager{
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: SomeAudioManager? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = SomeAudioManager()
        }
        return Static.instance!
    }
    func audioView(songname: NSString,format: NSString)
    {
        let audioPlayer:ava
        audioPlayer=try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(songname, ofType:format)!), fileTypeHint: AVFileTypeMPEGLayer3)
        audioPlayer!.delegate=self;
        self.audioPlayer!.play()
    }
}

那个AVAudioPlayer在NSObject下,我实现不了

在输入 let audioPlayer:AVAudio 时 -> 它没有显示任何内容。

并不是说它有多大意义,但这对我来说是编译的:

import AVFoundation
class SomeAudioManager: NSObject, AVAudioPlayerDelegate
{
    class var sharedInstance: SomeAudioManager {
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: SomeAudioManager? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = SomeAudioManager()
        }
        return Static.instance!
    }
    func audioView(songname: String,format: String) {
        let audioPlayer: AVAudioPlayer

        do {
            audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(songname, ofType:format)!), fileTypeHint: AVFileTypeMPEGLayer3)
            audioPlayer.delegate = self;
            audioPlayer.play()
        } catch {
            // error
        }
    }
}

所以需要引入框架,swift中的try-catch是do-try-catch。其他一些语法错误也已修复。

单例在Swift BTW中没有这样使用。

用法:

class someOtherClass {
    func doSomething() {
        SomeAudioManager().audioView("name_here", format: "format_here")
        SomeAudioManager.sharedInstance.audioView("name_here", format: "format_here")
    }
}

至于你案例中的单例部分(来自评论)你应该使用这样的东西:

class MyAudioPlayer: NSObject, AVAudioPlayerDelegate {
    private static let sharedPlayer: MyAudioPlayer = {
        return MyAudioPlayer()
    }()

    private var container = [String : AVAudioPlayer]()

    static func playFile(name: String, type: String) {
        var player: AVAudioPlayer?
        let key = name+type
        for (file, thePlayer) in sharedPlayer.container {
            if file == key {
                player = thePlayer
                break
            }
        }
        if player == nil, let resource = NSBundle.mainBundle().pathForResource(name, ofType:type) {
            do {
                player = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: resource), fileTypeHint: AVFileTypeMPEGLayer3)
            } catch {
                // error
            }
        }
        if let thePlayer = player {
            if thePlayer.playing {
                // already playing
            } else {
                thePlayer.delegate = sharedPlayer
                sharedPlayer.container[key] = thePlayer
                thePlayer.play()
            }
        }
    }
}

用法是:

MyAudioPlayer.playFile("Breach", type: "mp3")