gpt4 book ai didi

ios - Objective-C : Turning off music after switching views

转载 作者:行者123 更新时间:2023-11-28 21:27:11 25 4
gpt4 key购买 nike

所以如果我留在同一个 View Controller 上,我的音频工作正常,音乐会按照我想要的方式打开和关闭。但是,当我使用不同的头文件模态到另一个 View Controller 并返回到我原来的 View Controller 时,我无法再打开或关闭我的音乐。这是我的代码:

-(void) viewDidLoad {
//Play Background Music in .1 second after game loads
[self performSelector:@selector(playBackgroundMusic) withObject:nil afterDelay:0.1];
}

-(void) playBackgroundMusic {

//Music int is used so music doesnt play over itself
if (musicInt == 0) {

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/JungleMusic.wav"];
NSLog(@"Path to play: %@", resourcePath);
NSError* err;

//Initialize our player pointing to the path to our resource
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];

musicInt = musicInt + 1;
//set our delegate and begin playback
player.delegate = self;
[player play];
player.numberOfLoops = -1;
player.currentTime = 0;
player.volume = 1.0;
}
}

//Used to turn the music on or off
-(IBAction)musicTest:(id)sender{
if (player.playing) {
[player stop];
} else {
[player play];
}
}

我还在同一个 .m 文件中合成了我的播放器:

@synthesize player;

在我的 .h 文件中,我声明了 AVAudioPlayer

    @interface ViewController1 : UIViewController <AVAudioPlayerDelegate>

{
AVAudioPlayer *player;
}

@property (nonatomic, retain) AVAudioPlayer *player;

如果您能帮助解决这个问题,我们将不胜感激。我只是不明白为什么在简单地建模到另一个 View Controller 之后我无法打开/关闭我的音频?谢谢!

最佳答案

根据我们在评论中的对话,答案是你有一个 segue 去第二个 View Controller ,第二个 segue 返回,这实际上创建了原始 View Controller 的一个新实例(你结束了使用三个 View Controller 。)

解决方案是将后退按钮连接到调用 dismissViewController:animated:completion: 的 IBAction,或者将后退按钮连接到展开转场。当您执行其中一项操作时,后退按钮会关闭第二个 View Controller 并公开现有的第一个 View Controller ,而不是创建第二个 View Controller 的新副本。

关于ios - Objective-C : Turning off music after switching views,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37643757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com