Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 避免音频以在其他视图中继续_Ios_Objective C_Avfoundation - Fatal编程技术网

Ios 避免音频以在其他视图中继续

Ios 避免音频以在其他视图中继续,ios,objective-c,avfoundation,Ios,Objective C,Avfoundation,我有一个应用程序,在启动时在View Controller中播放音频文件。我还有一个按钮,可以切换到第二个视图。我的问题是,即使我切换了视图,音频仍会继续播放。以下是我的ViewController.m代码: #import <AVFoundation/AVFoundation.h> @interface ViewController () @end @implementation ViewController //When I go to this view, I want

我有一个应用程序,在启动时在View Controller中播放音频文件。我还有一个按钮,可以切换到第二个视图。我的问题是,即使我切换了视图,音频仍会继续播放。以下是我的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
#导入
@界面视图控制器()
@结束
@实现视图控制器
//当我转到此视图时,我希望音频停止
-(iAction)SwitchView:(id)发送方{
SecondViewController*second=[[SecondViewController alloc]initWithNibName:nil bundle:nil];
[自我呈现视图控制器:第二个动画:是完成:空];
}
-(无效)viewDidLoad
{
[超级视图下载];
NSURL*url=[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@“home_sound”
类型:@“mp3”]];
n错误*错误;
homeaudioPlayer=[[AVAudioPlayer alloc]
initWithContentsOfURL:url
错误:&错误];
如果(错误)
{
NSLog(@“音频播放器中的错误:%@”,
[错误本地化描述];
}否则{
homeaudioPlayer.delegate=self;
[homeaudioPlayer设置NumberOfLoops:-1];
[家庭音频播放器准备播放];
}
[家庭音频播放器播放];
}
@结束
我该怎么做


谢谢。

只需添加[homeaudioPlayer停止];到您的switchView方法。这将停止播放机播放的所有声音。

只需添加[homeaudioPlayer停止];到您的switchView方法。这将停止播放机播放的所有声音。

更新您的SwitchView代码-

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

更新您的SwitchView代码-

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

好的,我解决了这个问题,添加了:[homeaudioPlayer停止];到SwitchView-function。好的,我解决了它,添加了:[homeaudioPlayer停止];切换到SwitchView功能。