2011-11-25 77 views
0

我使用AVAudioplayer通過按下一個按鈕,使用這下面的代碼打3名型動物的歌曲:AVAudioPlayer量CONTROLE

-(IBAction)play 
{ 
//Play sound1 
NSString *pathToMusicFile1 = [[NSBundle mainBundle] pathForResource:@"Sample1" ofType:@"mp3"]; 
AVAudioPlayer* mySong1 = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile1] error:NULL]; 
NSLog(@"Song1 Loaded"); 
mySong1.numberOfLoops = 0; 
mySong1.volume = 1.0; 
[mySong1 play]; 


    //Play sound2 
NSString *pathToMusicFile2 = [[NSBundle mainBundle] pathForResource:@"Sample2" ofType:@"mp3"]; 
AVAudioPlayer* mySong2 = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile2] error:NULL]; 
NSLog(@"song2 Loaded"); 
mySong2.numberOfLoops = 0; 
mySong2.volume = 1.0; 
[mySong2 play]; 


    //Play sound3 
NSString *pathToMusicFile3 = [[NSBundle mainBundle] pathForResource:@"Sample3" ofType:@"mp3"]; 
AVAudioPlayer* mySong3 = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile3] error:NULL]; 
NSLog(@"song3 Loaded"); 
mySong3.numberOfLoops = 0; 
mySong3.volume = 1.0; 
[mySong3 play]; 

} 

這工作得很好,但現在我想用3個按鍵靜音關閉Sound1例子,SOUND2和聲音3. 我試過:

-(IBAction)sound1 
{ 
mySong1.volume = 0.0; 

} 


-(IBAction)sound2 
{ 

mySong2.volume = 0.0; 


} 


-(IBAction)sound3 
{ 

mySong3.volume = 0.0; 

} 

但我得到的錯誤 「mySong1未申報」!我怎樣才能設法聲明並有可能通過按下按鈕來關閉其中一個聲音?

回答

0

「mySong1未聲明」正在呈現,因爲您試圖從另一個函數中的一個函數訪問本地指針。基本上,在.m中刪除你的指針(AVAudioPlayer *),然後離開mySong3(或2或1)= //等等等等。然後在.h聲明mysong1; 2;和3;包括指針。