我想在我的游戏中播放背景音乐(理想情况下我喜欢它淡入/淡出但我希望它先播放)
我已经查看了SO的各种答案,我的代码看起来似乎正确但由于某种原因文件没有播放,实际上它在AVAudioPlayer *player
行崩溃(没有有用的输出)。如果我继续在[player play]
行再次崩溃,但error
为0
。
再次更新
GameViewController.h
@property (nonatomic, retain) AVAudioPlayer *player;
GameViewController.m
@synthesize player; // the player object
viewDidLoad中:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"bgtrack" ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error: nil]; // breaks here
[player prepareToPlay];
我包括
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
更新
根据评论,我尝试直接从Apple Dev中心复制/粘贴代码。我正在使用最新的XCode 4和iOS 6.1模拟器。
当它崩溃时,我没有得到它触及断点的信息。该文件包含在捆绑包中
答案 0 :(得分:3)
您需要存储对player
的强引用(可能在您的视图控制器的ivar中)。如果您只是将代码放入-viewDidLoad
方法或某些内容而不强烈引用它,那么发生的事情就是玩家在有机会开始播放之前会被解除分配。例如:
@interface MyViewController : UIViewController
@end
@implementation MyViewController
{
AVAudioPlayer* player;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"YourMP3FileName" ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSError *error;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
player.numberOfLoops = -1; //infinite
player.currentTime = 139;// 2:19;
[player play];
}
@end
这对我来说很好,但是如果player
不是视图控制器的ivar,我就没有声音。
或者,我掀起了下面的课程,这个课程将作为一个单一的&#34;&#34; AVAudioPlayer。对于numberOfLoops
为-1(无限)的情况,这显然会出现问题,因为这意味着它永远不会停止播放或消失,但如果您想要播放一次然后播放,则可能会使事情变得更容易一些走开,不需要随身携带它。
<强> OneShotAVAudioPlayer.h 强>
#import <AVFoundation/AVFoundation.h>
@interface OneShotAVAudioPlayer : AVAudioPlayer
@end
<强> OneShotAVAudioPlayer.m 强>
#import "OneShotAVAudioPlayer.h"
#import <AVFoundation/AVFoundation.h>
#import <objc/runtime.h>
@interface OneShotAVAudioPlayer () <AVAudioPlayerDelegate>
@property(weak) id<AVAudioPlayerDelegate> p_exogenousDelegate;
@end
@implementation OneShotAVAudioPlayer
static void * const OneShotAVAudioPlayerKey = (void*)&OneShotAVAudioPlayerKey;
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError
{
if (self = [super initWithContentsOfURL:url error:outError])
{
// Retain ourself
objc_setAssociatedObject(self, OneShotAVAudioPlayerKey, self, OBJC_ASSOCIATION_RETAIN);
[super setDelegate: self];
}
return self;
}
- (id)initWithData:(NSData *)data error:(NSError **)outError;
{
if (self = [super initWithData:data error:outError])
{
// Retain ourself
objc_setAssociatedObject(self, OneShotAVAudioPlayerKey, self, OBJC_ASSOCIATION_RETAIN);
[super setDelegate: self];
}
return self;
}
- (void)setDelegate:(id<AVAudioPlayerDelegate>)delegate
{
self.p_exogenousDelegate = delegate;
}
- (id<AVAudioPlayerDelegate>)delegate
{
return self.p_exogenousDelegate;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
@try
{
if ([self.p_exogenousDelegate respondsToSelector: _cmd])
[self.p_exogenousDelegate audioPlayerDidFinishPlaying:player successfully:flag];
}
@finally
{
// Make a strong ref so we stay alive through the scope of this function
typeof(self) keepAlive = self;
// Give up the self retain
objc_setAssociatedObject(keepAlive, OneShotAVAudioPlayerKey, nil, OBJC_ASSOCIATION_RETAIN);
// Push in the "real" (outside) delegate, cause our job is done here.
[super setDelegate: self.p_exogenousDelegate];
}
}
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
@try
{
if ([self.p_exogenousDelegate respondsToSelector: _cmd])
[self.p_exogenousDelegate audioPlayerDecodeErrorDidOccur:player error:error];
}
@finally
{
// Make a strong ref so we stay alive through the scope of this function
typeof(self) keepAlive = self;
// Give up the self retain
objc_setAssociatedObject(keepAlive, OneShotAVAudioPlayerKey, nil, OBJC_ASSOCIATION_RETAIN);
// Push in the "real" (outside) delegate, cause our job is done here.
[super setDelegate: self.p_exogenousDelegate];
}
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
BOOL retVal = [super respondsToSelector: aSelector];
if (!retVal)
{
struct objc_method_description method = protocol_getMethodDescription(@protocol(AVAudioPlayerDelegate), aSelector, YES, YES);
if (method.name)
{
retVal = [self.p_exogenousDelegate respondsToSelector: aSelector];
}
}
return retVal;
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
id retVal = [super forwardingTargetForSelector:aSelector];
if (!retVal)
{
struct objc_method_description method = protocol_getMethodDescription(@protocol(AVAudioPlayerDelegate), aSelector, YES, YES);
if (method.name && [self.p_exogenousDelegate respondsToSelector: aSelector])
{
retVal = self.p_exogenousDelegate;
}
}
return retVal;
}
- (void)setNumberOfLoops:(NSInteger)numberOfLoops
{
if (numberOfLoops < 0)
{
NSLog(@"Warning! You have set an infinite loop count for an instance of %@ (%p). This means the instance will effectively be leaked.", NSStringFromClass([self class]), self);
}
[super setNumberOfLoops: numberOfLoops];
}
@end
答案 1 :(得分:0)
如果玩家为零,则将玩家ivar提升为强大的属性并记录[error description]
。
如果它崩溃了肯定是错的。您是否添加了异常断点?