AVAudioEngine MIDI文件播放(当前进度+ MIDI结束回调)Swift
创始人
2024-11-12 21:00:18
0

以下是一个AVAudioEngine MIDI文件播放器的示例代码,包括当前进度和MIDI结束回调的实现:

import AVFoundation

class MidiPlayer {

    var engine: AVAudioEngine!
    var player: AVAudioPlayerNode!
    var sequencer: AVAudioSequencer!
    var currentPosition: TimeInterval = 0
    var duration: TimeInterval = 0
    var completionHandler: (() -> Void)?

    init() {
        engine = AVAudioEngine()
        player = AVAudioPlayerNode()
        engine.attach(player)
        engine.connect(player, to: engine.mainMixerNode, format: nil)
    }

    func loadMidiFile(url: URL) {
        do {
            sequencer = try AVAudioSequencer(url: url)
            sequencer.prepareToPlay()
            duration = sequencer.duration.seconds
        } catch {
            print("Failed to load MIDI file: \(error.localizedDescription)")
        }
    }

    func play() {
        do {
            try engine.start()
            player.play()
            sequencer.currentPosition = currentPosition
            sequencer.addPlayer(player)
        } catch {
            print("Failed to start audio engine: \(error.localizedDescription)")
        }
    }

    func pause() {
        player.pause()
        currentPosition = sequencer.currentPosition.seconds
        engine.stop()
    }

    func stop() {
        player.stop()
        currentPosition = 0
        engine.stop()
    }

    func addCompletionHandler(completion: @escaping () -> Void) {
        completionHandler = completion
        NotificationCenter.default.addObserver(self, selector: #selector(handleMidiCompletion), name: .AVAudioEngineConfigurationChange, object: nil)
    }

    @objc func handleMidiCompletion(notification: NSNotification) {
        if sequencer.isPlaying {
            return
        }

        completionHandler?()
        completionHandler = nil
        NotificationCenter.default.removeObserver(self, name: .AVAudioEngineConfigurationChange, object: nil)
    }
}

使用示例:

let midiPlayer = MidiPlayer()
let midiFileURL = Bundle.main.url(forResource: "example", withExtension: "mid")!
midiPlayer.loadMidiFile(url: midiFileURL)
midiPlayer.addCompletionHandler {
    print("MIDI playback completed")
}
midiPlayer.play()

这个示例代码创建了一个MidiPlayer类,封装了AVAudioEngine和AVAudioPlayerNode的功能。它提供了加载MIDI文件、播放、暂停和停止的方法,并且可以在MIDI播放完成时添加一个回调处理程序。要使用它,首先创建一个MidiPlayer对象,然后调用loadMidiFile方法加载MIDI文件,使用addCompletionHandler方法添加一个回调处理程序,最后调用play方法开始播放MIDI文件。

请将"example.mid"替换为您自己的MIDI文件的名称,并确保将其添加到项目的资源中。

相关内容

热门资讯

8分钟插件!微乐小程序辅助器代... 8分钟插件!微乐小程序辅助器代理,微乐小程序免费黑科技,妙计教程(有挂辅助器)-哔哩哔哩微乐小程序辅...
微乐小程序授权!微乐小程序黑科... 微乐小程序授权!微乐小程序黑科技(透视)技巧-好像辅助是真的挂1、实时微乐小程序黑科技透视辅助更新:...
第五分钟安装!微乐小程序游戏辅... 第五分钟安装!微乐小程序游戏辅助器脚本官网,微乐小程序免费黑科技,课程教程(有挂下载)-哔哩哔哩1、...
微信小程序微乐房间有挂吗!微乐... 微信小程序微乐房间有挂吗!微乐小程序免费黑科技(透视)挂-总是了解存在有挂;1、游戏颠覆性的策略玩法...
三分钟插件!微乐自建房辅助软件... 三分钟插件!微乐自建房辅助软件下载,微乐小程序免费黑科技,举措教程(有挂脚本)-哔哩哔哩1、金币登录...
微信小程序 微乐陕西挖坑 外挂... 微信小程序 微乐陕西挖坑 外挂!微乐小程序黑科技(透视)app-真是曝光有挂1、每一步都需要思考,不...
第一分钟插件!微乐自建房怎么挂... 第一分钟插件!微乐自建房怎么挂脚本,微乐小程序黑科技,绝活儿教程(有挂工具)-哔哩哔哩1、进入游戏-...
微乐陕西麻将破解!微乐小程序免... 微乐陕西麻将破解!微乐小程序免费黑科技(透视)软件-总是揭幕真的有挂1、在微乐小程序免费黑科技插件功...
第1分钟插件!微信小程序微乐陕... 第1分钟插件!微信小程序微乐陕西挖坑辅助,微乐小程序免费黑科技,指引教程(有挂安装)-哔哩哔哩1)微...
微信小程序微乐破解器2025!... 微信小程序微乐破解器2025!微乐小程序免费黑科技(透视)app-都是教你存在有挂1、任何微乐小程序...