【发布时间】:2014-06-28 13:50:03
【问题描述】:
仍然没有解决方案 - 减少测试用例项目:
http://www.friendlycode.co.uk/assets/Bugfix.zip
我是 Xcode/Objective C 的新手,做了很多研究,但找不到答案。这里有很多类似的问题,但没有一个能帮助我解决这个问题。
文件:
app.h
app.m
Settings.h
Settings.m
我播放了一些背景音乐,当应用程序通过 ViewController.m 文件中的 ViewDidLoad 启动时开始播放。
如果音乐开关被触摸并设置为关闭,我会尝试从 Settings.m 文件中停止此操作。
请参阅下面的代码(已删除不必要的插座/方法等)
NSLog 输出“试图停止音频”,但音频并未停止。我想我已经正确引用了 ViewController 类,所以不确定为什么它不会停止?
app.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
app.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];
}
【问题讨论】:
-
如果有人想测试他们的答案,我已经创建了一个简化的测试用例项目:friendlycode.co.uk/assets/Bugfix.zip
-
我已经通过在 AppDelegate 中声明 AVAudioPlayer 来修复它。
-
我无法让你的方法工作 - 请编辑简化的测试用例并上传到某个地方。如果它有效,我会奖励你最好的答案
标签: ios iphone objective-c xcode xcode5