首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在gs_instruments的iOS / Swift中播放一个midi音符

在gs_instruments的iOS / Swift中播放一个midi音符
EN

Stack Overflow用户
提问于 2015-11-25 15:19:05
回答 2查看 1.7K关注 0票数 1

我想在一个使用Swift的iOS应用程序上播放一个midi音符。

这是我到目前为止所得到的,但不幸的是,这是一个错误的崩溃。

代码语言:javascript
运行
复制
func initAudio(){

    let engine = AVAudioEngine()
    self.sampler = AVAudioUnitSampler()
    engine.attachNode(self.sampler!)
    engine.connect(self.sampler!, to: engine.outputNode, format: nil)

    guard let soundbank = NSBundle.mainBundle().URLForResource("gs_instruments", withExtension: "dls") else {

        print("Could not initalize soundbank.")
        return
    }

    let melodicBank:UInt8 = UInt8(kAUSampler_DefaultMelodicBankMSB)
    let gmHarpsichord:UInt8 = 6
    do {

        try self.sampler!.loadSoundBankInstrumentAtURL(soundbank, program: gmHarpsichord, bankMSB: melodicBank, bankLSB: 0)

    }catch {
        print("An error occurred \(error)")
        return
    }

    self.sampler!.startNote(60, withVelocity: 64, onChannel: 0)
}

以下是错误消息:

代码语言:javascript
运行
复制
Unable to start playing the low note. Error code: -10867 'ç’ˇˇ'
2015-11-25 15:10:07.419 Demo[774:139850] 15:10:07.418 ERROR:    [0x19eaf4000] AVAudioUnitMIDIInstrument.mm:103: -[AVAudioUnitMIDIInstrument startNote:withVelocity:onChannel:]: error -10867
2015-11-25 15:10:07.419 Demo[774:139850] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'error -10867'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-25 18:57:11

解决方案

AVAudioEngine需要在使用之前启动(error -10867的意思是“未初始化”)。

在Do中添加try engine.start()

代码语言:javascript
运行
复制
do {
    try engine.start()
    try self.sampler!.loadSoundBankInstrumentAtURL(soundbank, program: gmHarpsichord, bankMSB: melodicBank, bankLSB: 0)
}catch {
    print("An error occurred \(error)")
    return
}

关于错误代码

在您的错误消息中有以下一行:

2015年-11-25 15:10:07.419 Demo774 774:139850 15:10:07.418错误: 0x19eaf4000 AVAudioUnitMIDIInstrument.mm:103:-AVAudioUnitMIDIInstrument startNote:2015:onChannel::error -10867

我们看到错误代码的发射器是AVAudioUnitMIDIInstrument,它继承自AVAudioUnit:这些东西来自位于OS的系统文件夹中的"AudioUnit“框架。

AudioUnit的错误代码在此头文件中定义:

/System/Library/Frameworks/AudioUnit.framework/Versions/A/Headers/AUComponent.h

与我们有关的部分:

代码语言:javascript
运行
复制
CF_ENUM(OSStatus) {
    kAudioUnitErr_InvalidProperty           = -10879,
    kAudioUnitErr_InvalidParameter          = -10878,
    kAudioUnitErr_InvalidElement            = -10877,
    kAudioUnitErr_NoConnection              = -10876,
    kAudioUnitErr_FailedInitialization      = -10875,
    kAudioUnitErr_TooManyFramesToProcess    = -10874,
    kAudioUnitErr_InvalidFile               = -10871,
    kAudioUnitErr_UnknownFileType           = -10870,
    kAudioUnitErr_FileNotSpecified          = -10869,
    kAudioUnitErr_FormatNotSupported        = -10868,
    kAudioUnitErr_Uninitialized             = -10867,
    kAudioUnitErr_InvalidScope              = -10866,
    kAudioUnitErr_PropertyNotWritable       = -10865,
    kAudioUnitErr_CannotDoInCurrentContext  = -10863,
    kAudioUnitErr_InvalidPropertyValue      = -10851,
    kAudioUnitErr_PropertyNotInUse          = -10850,
    kAudioUnitErr_Initialized               = -10849,
    kAudioUnitErr_InvalidOfflineRender      = -10848,
    kAudioUnitErr_Unauthorized              = -10847,
    kAudioComponentErr_InstanceInvalidated  = -66749,
};
票数 1
EN

Stack Overflow用户

发布于 2016-04-25 08:07:47

您仍然应该通过将采样器连接到AVAudioEngine图(即调用AVAudioEngine.attachNode )来使其工作。

具体而言,这失败的原因是取样器中包含的引擎引用尚未初始化。调用attachNode可以做到这一点,因此稍后调用startEngine是可以的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33920157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档