2009-11-07 96 views
0

我在我的應用程序中使用avaudioplayer。在viewwilldisappear我想暫停/停止聲音,並在看法會出現我想再次播放聲音。我該怎麼做?我使用viewWillAppear中此代碼: -爲什麼AVAudioplayer在viewWillDisappear時不停止/暫停?

if(self.TickPlayer) 
{ 
    [self.TickPlayer play]; 
} 
if(self.TickPlayer.volume<0) 
{ 
    self.TickPlayer.volume=1.0; 
} 

,這對viewWillDisAppear

if(self.TickPlayer) 
{ 
     [self.TickPlayer stop]; 
} 

這裏是播放聲音的方法

-(void)PlayTickTickSound:(NSString*)SoundFileName 
{ 

//Get the filename of the sound file: 
NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],[NSString stringWithFormat:@"/%@",SoundFileName]]; 
//Get a URL for the sound file 
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 
NSError *error; 
if(self.TickPlayer==nil) 
{ 
self.TickPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:&error]; 
    // handle errors here. 
    self.TickPlayer.delegate=self; 
    [self.TickPlayer setNumberOfLoops:-1]; // repeat forever 
    [self.TickPlayer play]; 
} 
if(self.TickPlayer) 
{ 
[self.TickPlayer play]; 
} 

} 

,我稱其爲該觸發間隔爲第二方法使用定時器

在viewDidLoad-

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES]; 

-(void) updateCountdown { 
    [self PlayTickTickSound:@"tick.caf"]; 
} 

我也打了蜂鳴聲時,警報appears.that工作正常使用avaudioplayer在特定的條件,但這不是按預期工作

+0

我想查看更多的代碼。我在我的應用程序中執行相同的操作,並且在視圖出現時啓動聲音,並在視圖消失時停止播放。 – mahboudz

+0

我已經編輯我的問題,並張貼更多的代碼 –

+0

或者如果你告訴我的方式,我可以打一個聲音倒計時和第二的alert.means我想這兩個聽起來倒計時是conditions.also永久和提示音是有重複系統聲音的方式,並在我想停止時停止。 –

回答

1

看起來像一個大寫的問題 - viewWillDisAppear永遠不會因爲所謂的簽名是不正確的,正確的簽名是:

- (void)viewWillDisappear:(BOOL)animated 
+0

哦,我有正確的簽名只是忘記糾正它發佈 –