AVAudioPlayer.play() 不播放声音 [英] AVAudioPlayer.play() does not play sound
本文介绍了AVAudioPlayer.play() 不播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么下面的代码没有声音?它为 play() 返回true",但我听不到任何声音.
Why doesn't the following code play a sound? It returns "true" for play(), but I cannot hear anything.
let path = "/Users/account/Music/sound.mp3";
let fileURL = NSURL(fileURLWithPath: path)
var Player = AVAudioPlayer(contentsOfURL:fileURL, error:nil);
Player.delegate = self;
Player.prepareToPlay();
Player.volume = 1.0;
var res = Player.play();
println(res);
如果我改用下面的代码,我可以听到声音.
If I use the following code instead, I can hear the sound.
var inFileURL:CFURL = fileURL!;
var mySound = UnsafeMutablePointer<SystemSoundID>.alloc(sizeof(SystemSoundID));
AudioServicesCreateSystemSoundID(inFileURL, mySound);
AudioServicesPlaySystemSound(mySound.memory)
- OS X 优胜美地 10.10.3
- Xcode 6.2
推荐答案
问题在于 Player
,你的 AVAudioPlayer,是一个局部变量.所以它会立即消失 - 甚至可以开始播放,更不用说完成播放了.
The problem is that Player
, your AVAudioPlayer, is a local variable. So it goes out of existence immediately - before it can even start playing, let alone finish playing.
解决方案:改为将其设为属性,以便它持久化.
Solution: make it a property instead, so that it will persist.
这篇关于AVAudioPlayer.play() 不播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文