将音频播放器添加到SceneKit

时间:2015-06-12 08:00:03

标签: swift scenekit audio-player

将音频播放器添加到节点时遇到一些问题。出于某种原因,我得到一个例外,我的申请停止了。这是我的代码的样子

    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) 

有人可以帮我找出问题所在吗?

编辑:例外:

2015-06-12 10:44:29.202 asd [10480:2157476] 10:44:29.202错误:AVAudioNodeImpl.h:39:AVAE_CheckNodeHasEngine:必需条件为false:_engine!= nil 2015-06-12 10:44:29.208 asd [10480:2157476] *由于未被捕获的异常终止应用程序' com.apple.coreaudio.avfaudio',原因:'必需条件是false:_engine!= nil' * 第一次抛出调用堆栈: (     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视图] + 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:以NSException类型的未捕获异常终止 (lldb)

1 个答案:

答案 0 :(得分:0)

尝试使用SCNAction。或确保音频文件名正确无误。

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