[英]AVAudioPlayer sound on iPhone stops looping unexpectedly: How to debug?
我實現了在用戶拖動UISlider時播放的聲音效果。
在上一個問題中 ,我使用了AudioServicesPlaySystemSound(),但是這種類型的聲音無法停止。 當用戶未主動拖動滑塊但尚未釋放時,我需要聲音停止。
現在,我正在使用AVAudioPlayer對象創建聲音。 我在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];
}
我有兩種方法可以控制聲音。 每當移動滑塊時,第一個播放聲音。 它連接到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];
}
}
當用戶釋放滑塊按鈕時,第二種方法將停止聲音。 它連接到UISlider的內部Touch Up事件。
-(IBAction)haltSound;
{
[self.player1 stop];
}
當移動並快速釋放滑塊按鈕時,此方法起作用,但是如果按住按鈕超過兩或三秒鍾,聲音會重復(良好)然后破裂(不良)。 從那時起,聲音將不再播放(非常糟糕)。
我嘗試使用以下方法將聲音設置為循環播放:
[self.player1 setNumberOfLoops:30];
...但是導致應用凍結(加上鎖定模擬器)。
我該怎么做才能調試此問題?
當用戶握住UISlider時,“ Value Changed”被稱為“多次”(每次用戶僅移動少量)。 我會改用Touch Down。
您可能仍要嘗試使用該路線的回路...
我喜歡在用戶移動滑塊時播放聲音的想法。 讓我知道它是如何工作的。
deggstand@gmail.com
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.