gpt4 book ai didi

iphone - 如何在用户回调和 View 之间进行对话?

转载 作者:行者123 更新时间:2023-11-29 04:45:04 25 4
gpt4 key购买 nike

我试图弄清楚如何根据 MIDI 播放期间传递用户事件时调用的用户事件回调的内容来更新 UI 元素 (UIImageViews)。更具体地说,这些用户事件包含传递到用户回调函数中的音符数据(例如,演奏的音符为 60,又名中音 C)。

我想根据演奏的音符更新我的 UIImageViews。我尝试从回调中访问 UIImageViews,但由于它无法直接访问 ViewController,并且它在主线程以外的线程上运行,因此建议我采用不同的方式。

所以,我想做的是创建一个单独的 Controller ,它可以将回调函数中的信息转发到 UI,但我不知道该由谁来做这件事。我在下面发布了 ViewController 的代码。它包括回调函数以及用于设置用户事件和其他 View 相关内容的所有相关代码。

我正在使用 iOS 5 的 Xcode 4.3.1 工作,并且使用 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

最佳答案

您的方向是正确的 your other question .我不知道为什么你认为你需要一种完全不同的方法。

在回调中,确保在主线程上执行任何涉及 UI 的工作,使用 -performSelectorOnMainThread:dispatch_asyncdispatch_get_main_queue 。也许,任何使用 PracticeViewController 的代码都应该在主线程上运行。

例如

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];
});
...
}

关于iphone - 如何在用户回调和 View 之间进行对话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9776430/

25 4 0