Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7658365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:11:32+00:00 2026-05-31T13:11:32+00:00

I am trying to figure out how to update of UI elements (UIImageViews) based

  • 0

I am trying to figure out how to update of UI elements (UIImageViews) based upon the content of user event callbacks that are invoked when a user event is passed during MIDI playback. More specifically, these user events contain note data (e.g. played note is 60, aka middle C) that is passed into the user callback function.

I want to update my UIImageViews based upon what note is played. I have tried to access the UIImageViews from within the callback but since it doesn’t have direct access to the ViewController and since it runs on a thread other than the main one, I have been advised to do it a different way.

So, what I would like to do is to create a separate controller that can relay information from the callback function to the UI but I have no idea who to go about doing that. I’ve posted the code for my ViewController below. It includes the callback function and all of the associated code for setting up the user events and other view related stuff.

I’m working in Xcode 4.3.1 with iOS 5 and I am using ARC.

PracticeViewController.h

#import <UIKit/UIKit.h>
#import "Lesson.h"
#import "Note.h"
#import <AudioToolbox/AudioToolbox.h>

@interface PracticeViewController : UIViewController

@property (strong, nonatomic) Lesson *selectedLesson;
@property (strong, nonatomic) IBOutlet UINavigationItem *practiceWindowTitle;
@property MusicPlayer player;

//Outlets for White Keys
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey21;
// […]
@property (strong, nonatomic) IBOutlet UIImageView *whiteKey108;

//Outlets for Black Keys
@property (strong, nonatomic) IBOutlet UIImageView *blackKey22;
// […]
@property (strong, nonatomic) IBOutlet UIImageView *blackKey106;

// Key Highlight Images
@property (strong, nonatomic) UIImage *highlightA;
@property (strong, nonatomic) UIImage *highlightB;
@property (strong, nonatomic) UIImage *highlightC;
@property (strong, nonatomic) UIImage *highlightD;
@property (strong, nonatomic) UIImage *highlightE;
@property (strong, nonatomic) UIImage *highlightF;
@property (strong, nonatomic) UIImage *highlightG;
@property (strong, nonatomic) UIImage *highlightH;

- (IBAction)practiceLesson:(id)sender;

@end

PracticeViewController.m

#import "PracticeViewController.h"

@interface PracticeViewController ()

@end

@implementation PracticeViewController
@synthesize blackKey22;
// […]
@synthesize blackKey106;
@synthesize whiteKey21;
// […]
@synthesize whiteKey108;

@synthesize selectedLesson, practiceWindowTitle, player, highlightA, highlightB, highlightC, highlightD, highlightE, highlightF, highlightG, highlightH;

// Implement the UserEvent structure.

typedef struct UserEvent {
    UInt32 length;
    UInt32 typeID;
    UInt32 trackID;
    MusicTimeStamp tStamp;
    MusicTimeStamp dur;
    int playedNote;
} UserEvent;

// Implement the UserCallback function.

void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
{       
    UserEvent* event = (UserEvent *)inEventData;
    UInt32 size = event->length;
    UInt32 note = event->playedNote;
    UInt32 timestamp = event->tStamp;
    NSLog(@"Size: %lu Note: %lu, Timestamp: %lu", size, note, timestamp);
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.practiceWindowTitle.title = selectedLesson.titleAndSubtitle;

    // Load in the images for the glow.
    highlightA = [UIImage imageNamed:@"glow_whiteKeysA.png"];
    highlightB = [UIImage imageNamed:@"glow_whiteKeysB.png"];
    highlightC = [UIImage imageNamed:@"glow_whiteKeysC.png"];
    highlightD = [UIImage imageNamed:@"glow_whiteKeysD.png"];
    highlightE = [UIImage imageNamed:@"glow_whiteKeysE.png"];
    highlightF = [UIImage imageNamed:@"glow_whiteKeysF.png"];
    highlightG = [UIImage imageNamed:@"glow_blackKey.png"];
    highlightH = [UIImage imageNamed:@"glow_whiteKeysH.png"];

    // Create player, sequence, left/right hand tracks, and iterator.

    NewMusicPlayer(&player);
    MusicSequence sequence;
    NewMusicSequence(&sequence);
    MusicTrack rightHand;
    MusicTrack leftHand;
    MusicEventIterator iterator;

    // Load in MIDI file.

    NSString *path = [[NSString alloc] init];
    path = [[NSBundle mainBundle] pathForResource:selectedLesson.midiFilename ofType:@"mid"];
    NSURL *url = [NSURL fileURLWithPath:path];
    MusicSequenceFileLoad(sequence, (__bridge CFURLRef)url, 0, kMusicSequenceLoadSMF_ChannelsToTracks);

    // Get the right and left hand tracks from the sequence.

    int rightHandIndex = 0;
    //int leftHandIndex = 1;

    MusicSequenceGetIndTrack(sequence, rightHandIndex, &rightHand); //Get right hand.
    //MusicSequenceGetIndTrack(sequence, leftHandIndex, leftHand); //Get left hand.

    //Iterate through the right hand track and add user events.

    Boolean hasNextEvent = false;
    Boolean hasEvent = false;

    NewMusicEventIterator(rightHand, &iterator);
    MusicEventIteratorHasCurrentEvent(iterator, &hasEvent);
    MusicEventIteratorHasNextEvent(iterator, &hasNextEvent);

    while (hasNextEvent == true) {
        MusicTimeStamp timestamp = 0;
        MusicEventType eventType = 0;
        const void *eventData = NULL;
        int note;
        MusicTimeStamp duration;

        MusicEventIteratorGetEventInfo(iterator, &timestamp, &eventType, &eventData, NULL);

        if (eventType == kMusicEventType_MIDINoteMessage) {
            MIDINoteMessage *noteMessage = (MIDINoteMessage *)eventData;
            note = noteMessage->note;
            duration = noteMessage->duration;
            UserEvent event;

            event.length = 0;
            event.length = sizeof(UserEvent);
            event.playedNote = note;
            event.tStamp = timestamp;

            MusicEventUserData* data = (MusicEventUserData *)&event;
            MusicTrackNewUserEvent(rightHand, timestamp, data);
        }

        MusicEventIteratorHasNextEvent(iterator, &hasNextEvent);
        MusicEventIteratorNextEvent(iterator);
    }

    MusicSequenceSetUserCallback(sequence, noteUserCallback, NULL);

    MusicPlayerSetSequence(player, sequence);
    MusicPlayerPreroll(player);
}

- (void)viewDidUnload
{
    [self setPracticeWindowTitle:nil];
    [self setWhiteKey21:nil];
    // […]
    [self setWhiteKey108:nil];
    [self setBlackKey22:nil];
    // […]
    [self setBlackKey106:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

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

- (IBAction)practiceLesson:(id)sender {
    MusicPlayerStart(player);
}
@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:11:33+00:00Added an answer on May 31, 2026 at 1:11 pm

    You were on the right track with your other question. I don’t know why you think you need a whole different approach.

    In your callback, make sure to do any work that touches UI on the main thread, using -performSelectorOnMainThread: or dispatch_async with dispatch_get_main_queue. Probably, any code that uses PracticeViewController should go on the main thread.

    e.g.

    void noteUserCallback (void *inClientData, MusicSequence inSequence, MusicTrack inTrack, MusicTimeStamp inEventTime, const MusicEventUserData *inEventData, MusicTimeStamp inStartSliceBeat, MusicTimeStamp inEndSliceBeat)
    {   
        PracticeViewController* pvc = (__bridge PracticeViewController *)inClientData;
    
        dispatch_async(dispatch_get_main_queue(), ^{
            [pvc.whiteKey21 setImage:pvc.highlightA];
        });
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how to insert/update data into offsite databases that don't
I am trying to figure out how I can update my sql query dynamically.
I am trying to figure out how can I update the petevents table with
I'm trying to figure out a way to add a self-update feature to a
I'm trying to do a simple update, but can't figure out somthing. I have
Trying to figure out how to write a jquery formula that will sum all
I am trying to figure out how to manage a Datagrid based on an
I have been trying to figure out how to make a custom chronometer that
I've been trying to figure out some layout update xml directives to add, remove
I'm trying to figure out how I can track how the elements in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.