AVAudioPlayer对不同歌曲的音量控制

时间:2011-11-25 00:06:57

标签: ios xcode avaudioplayer volume

我使用AVAudioplayer通过按下按钮播放3首不同的歌曲,使用以下代码:

-(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未声明”!如何设置声明并按下按钮可以将其中一个声音静音?

1 个答案:

答案 0 :(得分:0)

“mySong1 unclared”正在呈现中,因为您正尝试从另一个函数中的一个函数访问本地指针。基本上,删除.m中的指针(AVAudioPlayer *),然后离开mySong3(或2或1)= //等等。然后在.h声明mysong1; 2;和3;包括指针。