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屋!
查看全文