gpt4 book ai didi

ios - 避免音频在另一个 View 中继续

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

我有一个应用程序,我在 View Controller 中启动时播放音频文件。我还有一个按钮可以切换到我的第二个 View 。我的问题是即使在我切换 View 后音频仍继续播放。这是我的 ViewController.m 代码:​​

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@end

@implementation ViewController

//When I go to this view, I want the audio to stop
- (IBAction)SwitchView:(id)sender {
SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:second animated:YES completion:NULL];
}

- (void)viewDidLoad
{
[super viewDidLoad];

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"home_sound"
ofType:@"mp3"]];

NSError *error;
homeaudioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
homeaudioPlayer.delegate = self;
[homeaudioPlayer setNumberOfLoops: -1];
[homeaudioPlayer prepareToPlay];
}

[homeaudioPlayer play];

}


@end

我该怎么做?

谢谢。

最佳答案

更新您的 SwitchView 代码-

- (IBAction)SwitchView:(id)sender {
[homeaudioPlayer stop];
SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:second animated:YES completion:NULL];
}

关于ios - 避免音频在另一个 View 中继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21713548/

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