gpt4 book ai didi

ios - Xcode:无法停止从子类播放 AVAudioPlayer 音乐

转载 作者:行者123 更新时间:2023-11-29 02:51:27 26 4
gpt4 key购买 nike

仍然没有解决方案 - 此处减少测试用例项目:

http://www.friendlycode.co.uk/assets/Bugfix.zip

我是 Xcode/Objective C 的新手,做了很多研究但找不到答案。这里有很多类似的问题,但没有一个能帮助我解决这个问题。

文件:

app.h
app.m
Settings.h
Settings.m

我有一些背景音乐播放,当应用程序通过 ViewController.m 文件中的 ViewDidLoad 启动时开始播放。

如果触摸音乐开关并将其设置为关闭,我将尝试从 Settings.m 文件中停止此操作。

请看下面的代码(删除了不必要的 socket /方法等)

NSLog 输出'attempting to stop audio' 但音频没有停止。我想我已经正确引用了 ViewController 类,所以不确定为什么它不会停止?

应用.h

#import <UIKit/UIKit.h>
#import <Social/Social.h>
#import "AVFoundation/AVAudioPlayer.h"

@interface ViewController : GAITrackedViewController <AVAudioPlayerDelegate, UIActionSheetDelegate>
{
// removed
}

@property (nonatomic, retain) AVAudioPlayer *BackgroundMusicPlayer;

@end

应用.m

- (void)viewDidLoad
{
[super viewDidLoad];

// Play Background music
[self PlayBackgroundMusic];
}

-(void)PlayBackgroundMusic
{
NSString* resourcePath = [[NSBundle mainBundle]
pathForResource:@"music-file"
ofType:@"aiff"];

NSLog(@"Path to play: %@", resourcePath);
NSError* err;

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

if( err ){
//bail!
NSLog(@"Failed with reason: %@", [err localizedDescription]);
}
else{
//set our delegate and begin playback
_BackgroundMusicPlayer.delegate = self;
[_BackgroundMusicPlayer play];
_BackgroundMusicPlayer.numberOfLoops = -1;
_BackgroundMusicPlayer.currentTime = 0;
_BackgroundMusicPlayer.volume = 0.5;
}
}

设置.h

#import <UIKit/UIKit.h>
#import "app.h"

@interface Settings : GAITrackedViewController <AVAudioPlayerDelegate, UIActionSheetDelegate>
{
IBOutlet UIButton *BackButton;
IBOutlet UISwitch *MusicSwitch;
IBOutlet UISwitch *SoundFXSwitch;


// Get instance of ViewController object
ViewController *home;
}

-(IBAction)BackButton:(id)sender;
-(IBAction)ToggleMusic:(id)sender;
-(IBAction)ToggleSoundFX:(id)sender;


@end

设置.m

#import "Settings.h"

@interface Settings ()

@end

@implementation Settings

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(IBAction)ToggleMusic:(id)sender {

// Get instance of ViewController object

//home = [[ViewController alloc] init];

if (MusicSwitch.on)
{
[home.BackgroundMusicPlayer play];
}
else {
[home.BackgroundMusicPlayer stop];
NSLog(@"Attempting to stop audio");

}
}

-(IBAction)ToggleSoundFX:(id)sender {

if (SoundFXSwitch.on)
{
}
else{

}
}

-(IBAction)BackButton:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}

最佳答案

我认为问题出在 ViewController *home 上。

您的 AvAudioPlayer 对象位于 ViewController 界面的 app.h 中。

但是在您的代码中,您没有初始化 ViewController 对象“home”在settings.m中。如此有效地,您正在尝试访问并阻止一个玩家未创建。

要访问 AVAudioPlayer 对象,请在 settings.h 的 viewDidload 中添加以下代码。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//initialise the audioPlayer object.
home=[[ViewController alloc] init];
}

关于ios - Xcode:无法停止从子类播放 AVAudioPlayer 音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24467470/

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