How to implement AudioServicesSystemSoundCompletionProc in Swift?

I try and cannot create an instance of AudioServicesSystemSoundCompletionProc for an argument in AudioServicesAddSystemSoundCompletion using Swift in Xcode.

That's what i still have

func completionCallback(ssID:SystemSoundID,clientData:UnsafeMutablePointer<Void>) -> Void {

}

var foo:(ssID:SystemSoundID,clientData:UnsafeMutablePointer<Void>) -> Void = completionCallback;

AudioServicesAddSystemSoundCompletion(soundID, nil, nil, foo, nil);

I wrote this with some tutorials explaining how to write equivalent C-function pointers in Swift, but this causes this error:

'(ssID: SystemSoundID, clientData: UnsafeMutablePointer<Void>) -> Void' is not convertible to 'AudioServicesSystemSoundCompletionProc'

The documentation shows an Objective-C declaration:

typedef void (*AudioServicesSystemSoundCompletionProc) ( SystemSoundID ssID, void *clientData );

This ad is shown when using Xcode:

typealias AudioServicesSystemSoundCompletionProc = CFunctionPointer<((SystemSoundID, UnsafeMutablePointer<Void>) -> Void)>

I'm not sure how to properly implement AudioServicesSystemSoundCompletionProc in Swift.

+4
source share
5 answers

. , , AudioServicesSystemSoundCompletionProc objective-c, swift. , ... : https://gist.github.com/jparishy/7b76edf8d0fcca1d63b0 ( )

, FunctionPointer.h FunctionPointer.c, , .

:

  • , , Objective-C Bridging Header, "YourProject/Bridging-Header.h" ( YourProject - ).
  • Objective-C Bridging-Header.h
  • #import "FunctionPointer.h"

FunctionPointer ... sweet

compeletion, , AudioServicesAddSystemSoundCompletion, :

    // at the top of the class
    var functionPointer: AudioServicesCompletionFunctionPointer?

    // in the code
    var me = self
    let userData = withUnsafePointer(&me) { ptr in
        return unsafeBitCast(ptr, UnsafeMutablePointer<Void>.self)
    }
    self.functionPointer = AudioServicesCompletionFunctionPointer(systemSoundID: soundId, block: {(systemSoundID: SystemSoundID, userData: UnsafeMutablePointer<Void>) -> () in
        // sound has ended, do your stuff here
        }, userData: userData)
    AudioServicesAddSystemSoundCompletion(soundId, CFRunLoopGetMain(), kCFRunLoopDefaultMode, AudioServicesCompletionFunctionPointer.completionHandler(), userData)

NSObject TheClassYou'reUsingThisIn soundId soundId

+5

, Swift 2.0.

AudioServicesAddSystemSoundCompletion(soundID, nil, nil, { (soundID, clientData) -> Void in

    // Your completion callback...                            
    AudioServicesDisposeSystemSoundID(soundID)

}, nil

Apple ( ):

C Swift

+5
let myData = unsafeBitCast(self, UnsafeMutablePointer<Void>.self)
AudioServicesAddSystemSoundCompletion(YOUR_SOUND_ID, CFRunLoopGetMain(), kCFRunLoopDefaultMode,{ (mSound, mVoid) in
        let me = unsafeBitCast(mVoid, YOURCURRENTCLASS.self)
        //me it is your current object so if yo have a variable like
        // var someVar you can do
        print(me.someVar)
    }, myData)
+2
source

This works in Swift 3:

        let weakSelf = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
        let error = AudioServicesAddSystemSoundCompletion(soundId,
                                                          nil,
                                                          nil,
                                                          {soundId, weakSelfPointer in

                                                            guard let weakSelfPointer = weakSelfPointer else {
                                                                return
                                                            }

                                                            let weakSelfValue = Unmanaged<NAME_OF_SELF_CLASS>.fromOpaque(weakSelfPointer).takeUnretainedValue()

                                                            // Then you can use `weakSelfValue` as you would do with `self`.
                                                            weakSelfValue.A_METHOD_OF_SELF
        }, weakSelf)
0
source

All Articles