简体   繁体   中英

How to get access to MusicTracks in a MusicSequence?

So, I'm trying to figure out the MusicPlayer API for iOS. I have been able to get a MIDI to play, so my sequence is there and populated with something useful but I can't seem to get access to the tracks (or in this case track, as there is only one music track, the other is a tempo track).

If I run a MusicSequenceGetTrackCount on "mySequence" I get a return of 2; this is expected.

If I run a MusicSequenceGetInfoDictionary, it returns 3 entries:

Printing description of sequenceInfo:
{
    tempo = 120;
    "time signature" = "4/4";
    title = "RHand";
}

This makes sense since all of this info matches up with my track.

So, all of this would make me think if I use the method MusicSequenceGetIndTrack(MusicSequence inSequence, UInt32 inTrackIndex, MusicTrack *outTrack) I should be able to output the track so I can make some modification.

Here's my code so far. If anyone could take a look and see where I'm going wrong, that'd be great. Also, if anyone has some good resources about using MusicPlayer, I'd be really grateful. Thanks.

ViewController.h

#import <UIKit/UIKit.h>
#import <AudioToolbox/MusicPlayer.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *noteDisplayLabel;
@property MusicSequence mySequence;
@property MusicPlayer player;
@property MusicEventIterator iterator;
@property MusicTrack RHand;
@property MusicTrack LHand;


- (IBAction)practiceLesson:(id)sender;
- (IBAction)changeTempo:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize noteDisplayLabel, mySequence, player, iterator, RHand, LHand;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CFBundleRef appBundle = CFBundleGetMainBundle();
    CFStringRef filename = CFSTR("simpleCScale");
    CFStringRef ext = CFSTR("mid");

    CFURLRef fileLocation = CFBundleCopyResourceURL(appBundle, filename, ext, NULL);

    NewMusicSequence(&mySequence);

    MusicSequenceFileLoad(mySequence, fileLocation, 0, kMusicSequenceLoadSMF_ChannelsToTracks);

    NewMusicPlayer(&player);

    UInt32 trackCount;
    MusicSequenceGetTrackCount(mySequence, &trackCount);

    NSLog(@"Number of tracks: %lu", trackCount);

    CFDictionaryRef sequenceInfo;
    sequenceInfo = MusicSequenceGetInfoDictionary(mySequence);

    UInt32 trackIndex = 0;
    MusicTrack track;
    MusicSequenceGetIndTrack (mySequence,trackIndex,&track);

    MusicEventIterator iter;
    Boolean hasEvent = 0;
    NewMusicEventIterator(track, &iter);

    Boolean hasNextEvent = 1;

    while (hasNextEvent == 1) {
        MusicEventIteratorHasCurrentEvent(iter, &hasEvent);
        NSLog(@"Has Event: %i", hasEvent);

        MusicTimeStamp timestamp;
        MusicEventType eventType = 0;
        void *eventData = NULL;
        UInt32 eventDataSize;

        MusicEventIteratorGetEventInfo(iter, &timestamp, &eventType, eventData, &eventDataSize);
        NSLog(@"Event %f: Type = %lu, Data = %p, Size = %lu", timestamp, eventType, eventData, eventDataSize);

        MusicEventIteratorNextEvent(iter);
        MusicEventIteratorHasNextEvent(iter, &hasNextEvent);
        NSLog(@"More Events? : %i", hasNextEvent);
    }
}

- (void)viewDidUnload
{
    [self setNoteDisplayLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)practiceLesson:(id)sender {
    MusicPlayerSetSequence(player, mySequence);
    MusicPlayerStart(player);
}

- (IBAction)changeTempo:(id)sender {
}
@end

EDIT

Tried doing the following in the viewDidLoad method but it didn't work:

UInt32 trackIndex = 1;
MusicTrack *track;
MusicSequenceGetIndTrack (mySequence,trackIndex,&track);

EDIT

Updated the above code to include changes. I have the eventIterator working now but I can't seem to get any event data out of it? Running the program gives me the following output:

2012-03-13 14:09:39.709 musicPlayerSandbox[61775:f803] Number of tracks: 2
2012-03-13 14:09:39.711 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.712 musicPlayerSandbox[61775:f803] Event 0.000000: Type = 7, Data = 0x0, Size = 4
2012-03-13 14:09:39.713 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.714 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.714 musicPlayerSandbox[61775:f803] Event 0.000000: Type = 7, Data = 0x0, Size = 4
2012-03-13 14:09:39.715 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.716 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.716 musicPlayerSandbox[61775:f803] Event 0.000000: Type = 7, Data = 0x0, Size = 4
2012-03-13 14:09:39.717 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.718 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.719 musicPlayerSandbox[61775:f803] Event 0.000000: Type = 7, Data = 0x0, Size = 4
2012-03-13 14:09:39.719 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.720 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.845 musicPlayerSandbox[61775:f803] Event 0.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.846 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.846 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.847 musicPlayerSandbox[61775:f803] Event 1.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.847 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.848 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.849 musicPlayerSandbox[61775:f803] Event 2.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.850 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.850 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.851 musicPlayerSandbox[61775:f803] Event 3.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.851 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.852 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.853 musicPlayerSandbox[61775:f803] Event 4.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.887 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.888 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.888 musicPlayerSandbox[61775:f803] Event 5.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.889 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.890 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.891 musicPlayerSandbox[61775:f803] Event 6.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.891 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.892 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.893 musicPlayerSandbox[61775:f803] Event 7.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.893 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.894 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.895 musicPlayerSandbox[61775:f803] Event 8.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.899 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.900 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.901 musicPlayerSandbox[61775:f803] Event 9.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.902 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.902 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.903 musicPlayerSandbox[61775:f803] Event 10.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.904 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.905 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.905 musicPlayerSandbox[61775:f803] Event 11.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.906 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.907 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.908 musicPlayerSandbox[61775:f803] Event 12.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.908 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.909 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.928 musicPlayerSandbox[61775:f803] Event 13.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.929 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.930 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.930 musicPlayerSandbox[61775:f803] Event 14.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.931 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.932 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.932 musicPlayerSandbox[61775:f803] Event 15.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.933 musicPlayerSandbox[61775:f803] More Events? : 1
2012-03-13 14:09:39.933 musicPlayerSandbox[61775:f803] Has Event: 1
2012-03-13 14:09:39.934 musicPlayerSandbox[61775:f803] Event 16.000000: Type = 6, Data = 0x0, Size = 8
2012-03-13 14:09:39.935 musicPlayerSandbox[61775:f803] More Events? : 0

To access any track you will first need to access musicSequence. In your case you have mySequence for this also you know the total number of tracks in music sequence as trackCount . Now to access particular track you will simply need to use

MusicTrack *track;
MusicSequenceGetIndTrack (mySequence,trackIndex,&track);

This will give you track. Also, make sure that trackIndex < trackCount .

Update

As per document .

The music track and related opaque types are declared in the MusicPlayer.h header file.

So you will need to use (MusicTrack is opaque type not a class.)

MusicTrack track; //without star sign. rest remains the same.

Update for EventIterator

As you said MusicEventIteratorGetEventInfo() method takes parameter const void **outEventData for event data therefore you will need to pass it as &outEventData and not the outEventData only.

So in your code change following line:

MusicEventIteratorGetEventInfo(iter, &timestamp, &eventType, &eventData, &eventDataSize);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM