【问题标题】:MIDIThruConnectionCreate always creates persistent MIDI thru connection?MIDIThruConnectionCreate 总是创建持久的 MIDI 直通连接?
【发布时间】:2016-11-18 00:22:20
【问题描述】:

Xcode 8 beta 2 / Swift 3:

根据Apple's CoreMIDI API documentation,MIDI 直通连接可以建立为持久的(永远保持原位,即使在您的应用退出并且系统重新启动后)或非持久/临时(由您的应用程序拥有并自动销毁它)应用程序退出)。

我遇到的问题是 我似乎无法创建非持久连接,即使我遵循 Apple 的指导方针。

归结为this API

func MIDIThruConnectionCreate(_ inPersistentOwnerID: CFString?, 
                        _ inConnectionParams: CFData, 
                        _ outConnection: UnsafeMutablePointer<MIDIThruConnectionRef>) -> OSStatus

如果你将 null (nil) 传递给 inPersistentOwnerID,这是一个 Swift 可选的,连接应该被创建为临时的。但是,无论我传递 nil 还是 String,连接总是被创建为持久的。 (我可以通过检查 CoreMIDI 的持久直通连接来验证这一点。)

我的代码总结:

public class OTMIDIConnectedThru {
    var connectionRef = MIDIThruConnectionRef()

    init?(sourceEndpoints: [MIDIEndpointRef], destinationEndpoints: [MIDIEndpointRef], persistentOwnerID: String? = nil) {
        var params = MIDIThruConnectionParams()
        MIDIThruConnectionParamsInitialize(&params) // fill with defaults

        // (... snip: code to prepare parameters here ...)

        let paramsData = withUnsafePointer(&params) { p in
            NSData(bytes: p, length: MIDIThruConnectionParamsSize(&params))
        }

        result = MIDIThruConnectionCreate(persistentOwnerID, paramsData, &connectionRef)
        guard result == noErr else { return nil }
    }
}

知道我做错了什么吗?这不可能是 API 中的错误?

【问题讨论】:

    标签: swift xcode macos swift3 coremidi


    【解决方案1】:

    我遇到了同样的问题,是的,我认为它总是会创建持久连接。 NULL 的 ID 可能与空字符串相同,因为带有空字符串的 MIDIThruConnectionFind 会返回所有这些持久连接。所以,API 或文档中的错误!

    我建议使用真正的 persistentID,并在初始化 MIDI 内容时删除所有现有/陈旧的连接:

    CFDataRef data;
    MIDIThruConnectionFind(CFSTR("com.yourcompany.yourapp"), &data);
    unsigned long n = CFDataGetLength(data) / sizeof(MIDIThruConnectionRef);
    MIDIThruConnectionRef * con = (MIDIThruConnectionRef*)CFDataGetBytePtr(data);
    for(int i=0;i<n;i++) {
        MIDIThruConnectionDispose(*con);
        con++;
    }
    

    【讨论】:

    • 感谢您的回复。我已经考虑在应用程序启动和退出时使用自命名的persistendIDs 处理所有直通连接,但这更像是一种解决方法,显然不能涵盖所有极端情况,例如如果您的应用程序崩溃它将离开孤立的直通连接可能不需要。您说即使在 Objective-C 中您也无法创建非持久的直通连接?那么这将指向一个 API 错误或误导性文档,而不是 Swift API 包装层中的错误。
    • 上面的想法是在应用程序启动时清理你的任何旧连接,然后再创建任何新连接。我认为应用程序崩溃后通过连接的孤立连接不会造成任何伤害,除非它们当然只在外部端点之间进行。那么“危害”将是端点仍然连接。在我目前正在开发的应用程序中,我正在按照上面的建议进行操作,这很好,因为我只连接到我自己的虚拟端点或从我自己的虚拟端点连接,所以如果应用程序崩溃,这些端点无论如何都不会存在。跨度>
    • 知道如何在 Swift3 中执行此操作吗?我不知道如何创建指向 Swift 想要的第二个参数的非可选 Unmanaged&lt;CFData&gt; 的指针
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多