2012-05-22 67 views
-1

我使用AVFoundation framework播放声音。在文件的开头,我有:应用程序崩溃时播放声音

#import <AVFoundation/AVAudioPlayer.h> 

AVFoundation framework已正确添加到我的项目中。然而,当我使用此代码播放声音,应用程序崩溃:

NSString *pathForBgMusicFile = [[NSBundle mainBundle] pathForResource:@"ClenchedTeeth" ofType:@"mp3"]; 
NSURL *bgMusicFile = [[NSURL alloc] initFileURLWithPath:pathForBgMusicFile]; 
AVAudioPlayer *bgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:bgMusicFile error:NULL]; 
[bgMusic play]; 

有谁知道如何解决这一问题?提前致谢!

+2

你能显示崩溃日志吗? – rishi

+0

你的应用程序做其他事情吗?如果你用这个做一个虚拟应用程序,它会崩溃吗? ARC还是手动内存管理?以上代码的 –

+1

似乎很亮。 – samfisher

回答

0

尝试建立AVAudioSession以及尝试播放之前。就像这样:

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    audioSession.delegate = self; 
    [audioSession setActive:YES error:nil]; 
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; 

记住要导入它,并确保你的类同时实现了AVAudioPlayerDelegateAVAudioSessionDelegate

0

我不会在我的Mac面前,试试这个(我只改第二行):

NSString *pathForBgMusicFile = [[NSBundle mainBundle] pathForResource:@"ClenchedTeeth" ofType:@"mp3"]; 
NSURL *bgMusicFile = [NSURL fileURLWithPath:pathForBgMusicFile]; 
AVAudioPlayer *bgMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:bgMusicFile error:NULL]; 
[bgMusic play]; 
+0

如果这没有帮助,如果你给出应用程序崩溃的确切路线 –

+0

[bgMusic play];线是什么崩溃。这里的崩溃日志: – Toby

+0

2012-05-22 15:51:27.552麦克白[67496:10703]错误加载/System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn:dlopen的(/System/Library/Extensions/AudioIPCDriver.kext/Contents/Resources/AudioIPCPlugIn.bundle/Contents/MacOS/AudioIPCPlugIn,262):未找到符号:___CFObjCIsCollectable 引用自:/System/Library/Frameworks/Security.framework/Versions/A/Security – Toby

1
#import <AudioToolbox/AudioToolbox.h> 

和your.m文件

FBundleRef mainBundle = CFBundleGetMainBundle(); 
     CFURLRef soundFileURLref; 
     soundFileURLref = CFBundleCopyResourceURL(mainBundle ,(CFStringRef)@"yourSoundFile",CFSTR ("wav"),NULL); 
     UInt32 soundID; 
     AudioServicesCreateSystemSoundID(soundFileURLref, &soundID); 
     AudioServicesPlaySystemSound(soundID);} 

可以如果是你的情况,请在按钮内使用此代码。不要忘记添加音频工具框架​​。好运

相关问题