简体   繁体   English

iPhone上的AVAudioPlayer声音意外停止循环:如何调试?

[英]AVAudioPlayer sound on iPhone stops looping unexpectedly: How to debug?

I am implementing a sound effect that plays while a user is dragging a UISlider. 我实现了在用户拖动UISlider时播放的声音效果。

In a previous question , I used AudioServicesPlaySystemSound() but that type of sound can't be halted. 在上一个问题中 ,我使用了AudioServicesPlaySystemSound(),但是这种类型的声音无法停止。 I need the sound to halt when the user is not actively dragging the slider but has not released it. 当用户未主动拖动滑块但尚未释放时,我需要声音停止。

Now I am creating a sound using an AVAudioPlayer object. 现在,我正在使用AVAudioPlayer对象创建声音。 I initialize it in my View Controller like this: 我在View Controller中像这样初始化它:

@interface AudioLatencyViewController : UIViewController <AVAudioPlayerDelegate> {
AVAudioPlayer *player1;
}

@implementation AudioLatencyViewController
@property (nonatomic, retain) AVAudioPlayer *player1;
@synthesize player1;

-(void)viewDidLoad {

[super viewDidLoad];

// the sound file is 1 second long
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"tone1" ofType:@"caf"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];

AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

[fileURL release];
self.player1 = newPlayer;;
[newPlayer release];

[self.player1 prepareToPlay];
[self.player1 setDelegate:self];
}

I have two methods to control the sound. 我有两种方法可以控制声音。 The first one plays the sound whenever the slider is being moved. 每当移动滑块时,第一个播放声音。 It is connected to the UISlider's Value Changed event in Interface Builder. 它连接到Interface Builder中的UISlider的Value Change事件。

-(IBAction)sliderChanged:(id)sender;
{
    // only play if previous sound is done. helps with stuttering
    if (![self.player1 isPlaying]) {
        [self.player1 play];
    }
}

The second method halts the sound when the user releases the slider button. 当用户释放滑块按钮时,第二种方法将停止声音。 It's connected to the UISlider's Touch Up Inside event. 它连接到UISlider的内部Touch Up事件。

-(IBAction)haltSound;
{
    [self.player1 stop];
}

This works when the slider button is moved then released quickly, but if you hold down the button longer than two or three seconds, the sound repeats (good) then breaks up (bad). 当移动并快速释放滑块按钮时,此方法起作用,但是如果按住按钮超过两或三秒钟,声音会重复(良好)然后破裂(不良)。 From then on, the sound won't play again (super bad). 从那时起,声音将不再播放(非常糟糕)。

I have tried setting the sound to loop using: 我尝试使用以下方法将声音设置为循环播放:

[self.player1 setNumberOfLoops:30];

...but that leads to the app freezing (plus locking up the Simulator). ...但是导致应用冻结(加上锁定模拟器)。

What can I do to debug this problem? 我该怎么做才能调试此问题?

When a user is holding the UISlider, Value Changed gets called MANY MANY times (any time the user moves just a slight amount). 当用户握住UISlider时,“ Value Changed”被称为“多次”(每次用户仅移动少量)。 I would use Touch Down instead. 我会改用Touch Down。

You might want to still try using the loop for this route... 您可能仍要尝试使用该路线的回路...

I like the idea of playing a sound while the user is moving the slider though. 我喜欢在用户移动滑块时播放声音的想法。 Let me know how it works out. 让我知道它是如何工作的。

deggstand@gmail.com deggstand@gmail.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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