gpt4 book ai didi

objective-c - NSSound 无法正常工作

转载 作者:行者123 更新时间:2023-12-03 17:43:12 27 4
gpt4 key购买 nike

我在 Mac 10.6 上使用 Cocoa。我试图让节拍器敲出时间,但是当我运行代码时,只播放前两个声音,然后直到声音再次播放时才结束。如果我用 NSBeep() 替换 [player play] 语句,它就可以正常工作。有什么建议吗?

#import <Cocoa/Cocoa.h>


@interface Metronome : NSObject {

NSInteger timeSig;
NSInteger noteValue;
NSInteger bpm;
NSUInteger beatNumber;
CGFloat duration;
NSSound *tickPlayer;
NSSound *tockPlayer;

}
@property(readwrite) NSInteger timeSig;
@property(readwrite) NSInteger noteValue;
@property(readwrite) NSInteger bpm;
@property(readwrite) float duration;
@property(readwrite) NSUInteger beatNumber;

-(id)init;
-(id)initWithTimeSig:(NSInteger)ts andNoteValue:(NSInteger)nv andBpm:(NSInteger)bp;
-(void)BeginMetronome;
-(void)PlaySound;

@end

#import "Metronome.h"

#define defaultBpm 80
#define defaultTimeSig 4
#define defaultNoteValue 4

@implementation Metronome

@synthesize timeSig, noteValue, duration, bpm, beatNumber;
-(id)init{
return [self initWithTimeSig:defaultTimeSig andNoteValue:defaultNoteValue andBpm:defaultBpm];
}
-(id)initWithTimeSig:(NSInteger)ts andNoteValue:(NSInteger)nv andBpm:(NSInteger)bp {
[super init];
if(self){
self.timeSig = ts;
self.noteValue = nv;
self.bpm = bp;
self.duration = 60.0/bpm;
tickPlayer = [NSSound soundNamed:@"Hat1"];
tockPlayer = [NSSound soundNamed:@"Hat2"];
self.beatNumber = 1;


}
return self;
}
-(void)BeginMetronome{
BOOL continuePlaying = YES;
NSInteger iter=0;

while (continuePlaying){
if(iter > 12){
continuePlaying = FALSE;
}
iter++;
[self PlaySound];
NSDate *curtainTime = [[NSDate alloc] initWithTimeIntervalSinceNow:self.duration];
NSDate *currentTime = [[NSDate alloc] init];

while (continuePlaying && ([currentTime compare:curtainTime] != NSOrderedDescending)) {
// if ([soundPlayerThread isCancelled] == YES) {
// continuePlaying = NO;
//}
//[NSThread sleepForTimeInterval:0.01];
sleep(.01);
//[tickPlayer stop];
//[tockPlayer stop];
[currentTime release];
currentTime = [[NSDate alloc] init];
}
[curtainTime release];
[currentTime release];

}
}
- (void)PlaySound { //***********Problem in this loop
if ([tickPlayer isPlaying])
[tickPlayer stop];
if ([tockPlayer isPlaying])
[tockPlayer stop];

if (beatNumber == 1) {
[tockPlayer play];
}
else if (beatNumber == 2){
[tickPlayer play];
}
else if (beatNumber == self.timeSig) {
beatNumber = 0;
[tickPlayer play];
}
else{
//[tickPlayer stop];
[tickPlayer play];
NSBeep();
}


beatNumber++;
//NSLog(@" %d ", beatNumber);
// NSBeep();
}



@end

最佳答案

我相信您对 sleep() 的使用会阻止 NSSound 的播放。这是一种非常草率的方法,而是使用像这样的计时器:

static NSInteger iter;

- (void) beginMetronome {

iter = 0;

NSTimer *timer = [NSTimer timerWithTimeInterval:self.duration target:self selector:@selector(continueMetronome:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[timer fire];
}

- (void) continueMetronome:(NSTimer *)theTimer {

[self PlaySound];

if (++iter > 12) {

[theTimer invalidate];
}
}

此外,您必须保留计划在 init 方法中使用的声音。

tickPlayer = [[NSSound soundNamed:@"Hat1"] retain];
tockPlayer = [[NSSound soundNamed:@"Hat2"] retain];

Cocoa 方法应该以小写字母开头,就像我写的那样。我不太确定节拍器应该如何工作,因此您可能需要将 12 的值更改为其他值。

关于objective-c - NSSound 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6280453/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com