2

this code works fine for looping a MusicTrack in iOS 8.4, but will halt the app under iOS 9.0 when setting the sequence with MusicPlayerSetSequence

var loopInfo = MusicTrackLoopInfo(loopDuration: 1.0,numberOfLoops: 0)
            MusicTrackSetProperty(track, UInt32(kSequenceTrackProperty_LoopInfo), &loopInfo, UInt32(sizeofValue(loopInfo)))

is there another way to get the track to loop in iOS 9?

1 Answers1

0

Others are having a similar problem: https://forums.developer.apple.com/thread/9940 the general idea for a current work around is to set the playback marker back to 0 once it reaches the end of the track.

For example use this in the method you're using to start your music player:

MusicTrack track = NULL;
MusicTimeStamp trackLen = 0;
UInt32 trackLenLen = sizeof(trackLen);
//Get main track
MusicSequenceGetIndTrack(musicSequence, 0, &track);
//Get length of track
MusicTrackGetProperty(track, kSequenceTrackProperty_TrackLength, &trackLen, &trackLenLen);
//Create UserData for User Event with any data
static MusicEventUserData userData = {1, 0x09};
//Put new user event at the end of the track
MusicTrackNewUserEvent(track, trackLen, &userData);
//Set a callback for when User Events occur
MusicSequenceSetUserCallback(musicSequence, sequenceUserCallback, musicPlayer);

And then you can have a callback function:

static void sequenceUserCallback(void *inClientData,
                             MusicSequence             inSequence,
                             MusicTrack                inTrack,
                             MusicTimeStamp            inEventTime,
                             const MusicEventUserData *inEventData,
                             MusicTimeStamp            inStartSliceBeat,
                             MusicTimeStamp            inEndSliceBeat)
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        MusicPlayerSetTime((MusicPlayer) inClientData, 0.0);
    }];
}

Which will set the player back to zero.

fiw
  • 716
  • 5
  • 8
  • Thanks, I am following that thread too. Your suggested work around does not help me though. I use the looping to time the metronome in my app - so without the loop there is no timing. – inatreecrown Oct 16 '15 at 01:23
  • I'm looking to create a work round now so if I find something that works I'll post it and we'll see if it can be helpful to you as well – fiw Oct 16 '15 at 05:40
  • I've added what worked for me, let me know how if it works for you too. – fiw Oct 16 '15 at 13:05