How to implement AVAudioPlayer Inside Singleton Method?

爱⌒轻易说出口 提交于 2019-12-22 12:31:52

问题


This is my code :

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()
    }
}

That AVAudioPlayer is under NSObject but I can't implement it.

While typing let audioPlayer:AVAudio -> it didn't display anything.


回答1:


Not that it makes much sense but this compiles for me:

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
        }
    }
}

So you need to import the framework, try-catch in swift are do-try-catch. Some other syntax fails are fixed as well.

The singleton is not used this way in Swift BTW.

Usage:

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



回答2:


As for the singleton part in you case (from comments) you should probable use something like this:

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()
            }
        }
    }
}

And the usage is:

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


来源:https://stackoverflow.com/questions/34435387/how-to-implement-avaudioplayer-inside-singleton-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!