gpt4 book ai didi

ios - 使用 MusicPlayer 和 Music Sequence 播放 midi 文件

转载 作者:可可西里 更新时间:2023-11-01 04:31:29 28 4
gpt4 key购买 nike

我已经成功地让 iOS 使用以下代码播放带有 soundfont 示例的 .mid (midi) 文件:

-(void) playMusic:(NSString*) name
{
NSString *presetURLPath = [[NSBundle mainBundle] pathForResource:@"GortsMiniPianoJ1" ofType:@"SF2"];
NSURL * presetURL = [NSURL fileURLWithPath:presetURLPath];
[self loadFromDLSOrSoundFont: (NSURL *)presetURL withPatch: (int)3];

NSString *midiFilePath = [[NSBundle mainBundle] pathForResource:name ofType:@"mid"];
NSURL * midiFileURL = [NSURL fileURLWithPath:midiFilePath];

NewMusicPlayer(&musicPlayer);

if (NewMusicSequence(&musicSequence) != noErr)
{
[NSException raise:@"play" format:@"Can't create MusicSequence"];
}

if(MusicSequenceFileLoad(musicSequence, (CFURLRef)midiFileURL, 0, 0 != noErr))
{
[NSException raise:@"play" format:@"Can't load MusicSequence"];
}

MusicPlayerSetSequence(musicPlayer, musicSequence);
MusicSequenceSetAUGraph(musicSequence, _processingGraph);
MusicPlayerPreroll(musicPlayer);
MusicPlayerStart(musicPlayer);
}

但是,当第一个文件仍在播放时我尝试播放第二个文件时出现问题。

我尝试了很多变体。首先,上面的代码将同时播放两个轨道。或者,我试过:

DisposeMusicPlayer(musicPlayer);
DisposeMusicSequence(musicSequence);

NewMusicPlayer(&musicPlayer) 之前,但这会产生一个奇怪的曲调版本,只播放零星的音符。

我很乐意简单地调用此方法,然后播放下一首轨道。

最佳答案

好的,我找到了关于如何正确处理 MusicPlayerMusicSequence 的答案。

-(void) stop
{
OSStatus result = noErr;

result = MusicPlayerStop(musicPlayer);

UInt32 trackCount;
MusicSequenceGetTrackCount(musicSequence, &trackCount);

MusicTrack track;
for(int i=0;i<trackCount;i++)
{
MusicSequenceGetIndTrack (musicSequence, i, &track);
result = MusicSequenceDisposeTrack(musicSequence, track);
}

result = DisposeMusicPlayer(musicPlayer);
result = DisposeMusicSequence(musicSequence);
result = DisposeAUGraph(_processingGraph);
}

关于ios - 使用 MusicPlayer 和 Music Sequence 播放 midi 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10249346/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com