Kishore Kumar
Kishore Kumar

Reputation: 4375

How to implement AVAudioPlayer Inside Singleton Method?

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.

Upvotes: 3

Views: 2359

Answers (2)

Matic Oblak
Matic Oblak

Reputation: 16774

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")

Upvotes: 2

Matic Oblak
Matic Oblak

Reputation: 16774

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

Upvotes: 2

Related Questions