我正在创建一个应用程序,其中我需要让背景音乐一直播放。在应用程序启动时,我开始播放音乐,一切都很好,直到我关闭模式视图控制器(我这样做是为了返回应用程序的主屏幕)。在这一点上,音乐突然停止,当我试图调整音量时,它实际上是“铃声”……5-10秒后,它回到“音量”,我可以再次启动音乐。
有没有人遇到过这个问题?我真的很难找到问题的根源……
编辑:这是我开始播放音乐的方式(theData是我的共享数据对象)
NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"bg" ofType:@"mp3"];
theData.backgroundMusicPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicPath] error:nil];
theData.backgroundMusicPlayer.delegate = self;
[theData.backgroundMusicPlayer prepareToPlay];
[theData.backgroundMusicPlayer play];
theData.backgroundMusicPlayer.numberOfLoops = -1; 在这个应用程序中,我有两个场景:我执行一个从场景1转到场景2的片段,然后返回,我使用以下代码:
[self dismissModalViewControllerAnimated:NO];
[theScene removeSubviews]; 方法removeSubviews:
[fbInfoView removeFromSuperview];
[logoView removeFromSuperview];
self.captureSession=nil; // ending AVCaptureSession发布于 2012-08-30 02:50:09
现在还不清楚如何创建和设置theData,但我建议您使用单例对象来实现此目的。如下所示:
@interface MySingleton : NSObject {
}
+(MySingleton*)sharedHelper;
-(void)playMusic;
@end实现:
@implementation MySingleton
static MySingleton* _sharedMySingleton = nil;
+(MySingleton*)sharedHelper
{
if (!_sharedMySingleton)
[[self alloc] init];
return _sharedMySingleton;
return nil;
}
+(id)alloc
{
NSAssert(_sharedMySingleton == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedMySingleton = [super alloc];
return _sharedMySingleton;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
// initialize stuff here
}
return self;
}
-(void)playMusic
{
// your code
}
@end然后这样叫它:
[[MySingleton sharedHelper] playMusic];https://stackoverflow.com/questions/12179159
复制相似问题