Adding audio player to a node in SceneKit

1.4k views Asked by At

I have some problems with adding an audio player to a node. For some reason, I get an exception and my application stops. Here is how my code looks like

    let node = scene.rootNode.childNodeWithName("box", recursively: true)!

    let source = SCNAudioSource(fileNamed: "test.mp3")
    source.loops = true
    source.volume = 2
    source.positional = true
    source.shouldStream = true
    source.load()
    let player = SCNAudioPlayer(source: source)

    node.addAudioPlayer(player) 

Can anybody help me find out what is the problem?

Edit: Exception:

2015-06-12 10:44:29.202 asd[10480:2157476] 10:44:29.202 ERROR: AVAudioNodeImpl.h:39: AVAE_CheckNodeHasEngine: required condition is false: _engine != nil 2015-06-12 10:44:29.208 asd[10480:2157476] * Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: _engine != nil' * First throw call stack: ( 0 CoreFoundation 0x000000010ca18885 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010e9afdf1 objc_exception_throw + 48 2 CoreFoundation 0x000000010ca186ea +[NSException raise:format:arguments:] + 106 3 libAVFAudio.dylib 0x0000000115549efe libAVFAudio.dylib + 98046 4 libAVFAudio.dylib 0x000000011558b4fd libAVFAudio.dylib + 365821 5 libAVFAudio.dylib 0x000000011558aaa5 libAVFAudio.dylib + 363173 6 SceneKit 0x000000010d3695eb C3DTransactionFlush + 1967 7 SceneKit 0x000000010d3699e9 C3DTransactionCommit + 218 8 SceneKit 0x000000010d2f7f6e -[SCNRenderer setPointOfView:] + 4677 9 SceneKit 0x000000010d2f8c05 -[SCNRenderer setScene:completionHandler:] + 213 10 SceneKit 0x000000010d354920 -[SCNView setScene:] + 319 11 asd 0x000000010c8338b2 _TFC3asd18GameViewController11viewDidLoadfS0_FT_T_ + 2866 12 asd 0x000000010c833ba2 _TToFC3asd18GameViewController11viewDidLoadfS0_FT_T_ + 34 13 UIKit 0x000000010d7bbd65 -[UIViewController loadViewIfRequired] + 860 14 UIKit 0x000000010d7bc0b4 -[UIViewController view] + 27 15 UIKit 0x000000010d69f3d4 -[UIWindow addRootViewControllerViewIfPossible] + 61 16 UIKit 0x000000010d69fad1 -[UIWindow _setHidden:forced:] + 302 17 UIKit 0x000000011c867717 UIKit + 247575 18 UIKit 0x000000010d6b0ff8 -[UIWindow makeKeyAndVisible] + 43 19 UIKit 0x000000010d63302b -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3545 20 UIKit 0x000000010d638ef0 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1755 21 UIKit 0x000000010d63673f -[UIApplication workspaceDidEndTransaction:] + 188 22 FrontBoardServices 0x0000000116052d7b FrontBoardServices + 163195 23 FrontBoardServices 0x0000000116053118 FrontBoardServices + 164120 24 CoreFoundation 0x000000010c9430f1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 25 CoreFoundation 0x000000010c938eac __CFRunLoopDoSources0 + 556 26 CoreFoundation 0x000000010c938363 __CFRunLoopRun + 867 27 CoreFoundation 0x000000010c937d78 CFRunLoopRunSpecific + 488 28 UIKit 0x000000010d636091 -[UIApplication _run] + 402 29 UIKit 0x000000010d63a79b UIApplicationMain + 171 30 asd 0x000000010c83624d main + 109 31 libdyld.dylib 0x000000010f3a0a05 libdyld.dylib + 10757 32 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

1

There are 1 answers

0
Jeff On

Try using SCNAction. Or making sure the audio file name is correct.

    let source = SCNAudioSource(fileNamed: "SoundFile.mp3")
    let action = SCNAction.playAudio(source!, waitForCompletion: true)
    node.runAction(action)