我试图阻止来自另一个类的声音。 只要应用程序打开,声音开始播放,并设置为在循环播放,除非'用户'更改设置并将声音关闭。如何暴露类成员或如何实现对这些成员行事的方法
这可以工作,但只适用于启动应用程序时,它检查设置声音是否设置为“开启/关闭”,但我希望在设置中更改设置时它会这样做。
这是我迄今为止...
的Firstclass
// grab the path to the caf file
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"Menu_Loop"
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
// create a new AVAudioPlayer initialized with the URL to the file
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
// set our ivar equal to the new player
self.player = newPlayer;
[newPlayer release];
// preloads buffers, gets ready to play
[player prepareToPlay];
player.numberOfLoops = -1; // Loop indefinately
if ([SoundSwitch isEqualToString:@"1"]){
[self.player play]; // Plays the sound
}else{
[self.player stop]; // Stops the sound
}
此播放声音。 如果我想简单地停止它:
[self.player stop]
但在同一类这仅适用于我将如何得到它在其他工作吗?
所以你的问题基本上是如何将'self.player'暴露给其他类。我会为球员创造一个属性。它看起来好像你可能已经有一个,但我不能没有更多的代码。 –