我正在让用户移动一个对象,移动结束后它将更新显示并播放声音。一切正常,但我不希望在视图首次加载时播放声音。如何防止这种情况发生?我需要在viewDidLoad中放入一些内容吗?这是触发事件的代码。谢谢你的帮助。
`
// Updates the text field with the current rotation angle and play sound.
- (void) updateTextDisplay
{
NSError *error = nil;
if (imageAngle >=0 && imageAngle <=180){
ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"nice" ofType:@"m4a"]] error:&error];
ComplimentPlayer.delegate = self;
textDisplay.text = [NSString stringWithFormat:@"blah blah blah"];
} else if (imageAngle >180 && imageAngle <=360){
ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"good" ofType:@"m4a"]] error:&error];
ComplimentPlayer.delegate = self;
textDisplay.text = [NSString stringWithFormat:@"blah blah"];
}
NSTimeInterval playbackDelay = 0.5;
[ComplimentPlayer playAtTime: ComplimentPlayer.deviceCurrentTime + playbackDelay];
}
`
最佳答案
将updateTextDisplay方法分为两个方法
- (void) updateTextDisplay;
- (void) playUpdateTextSound;
并且在视图首次加载时不要调用playUpdateTextSound()。
并在调用updateTextDisplay()之后再调用playUpdateTextSound()
关于iphone - 希望在加载 View 时不播放声音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9504123/