Elk*_*cho 4 avaudioplayer ios7 xcode5
我已经尝试了这里找到的所有不同选项,甚至使用NSData来加载音频文件.我尝试过mp3,m4a,caf文件,但没有任何工作.
没有错误消息,没有.
这是一款使用XCODE 5,iOS7的新应用.
这就是我正在使用的
- (void)playOnce:(NSString *)aSound
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[audioSession setActive:YES error:nil];
// Gets the file system path to the sound to play.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:aSound ofType:@"m4a"];
// Converts the sound's file path to an NSURL object
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL: soundURL error:nil];
[newAudio prepareToPlay];
// set it up and play
[newAudio setNumberOfLoops:0];
[newAudio setVolume: 1];
[newAudio setDelegate: self];
[newAudio play];
}
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏.我现在已经坚持了这么久了.
提前谢谢.
Pri*_*ung 17
您需要在视图控制器中将AVAudioPlayer声明为强引用的@property,如下所示
@property (strong, nonatomic) AVAudioPlayer * newAudio;
Run Code Online (Sandbox Code Playgroud)
如果在方法中声明AVAudioPlayer,它将在方法执行后立即释放,没有足够的时间发出声音.