5

我已经成功让 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),但这会产生一个奇怪的曲调版本,只播放零星的音符。

我很想简单地调用这个方法,以及要播放的下一首曲目。

4

1 回答 1

7

好的,我找到了有关如何正确处理 aMusicPlayerMusicSequence.

-(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);
}
于 2012-05-11T10:08:21.667 回答