MonoTouch的:播放声音 [英] MonoTouch: Playing sound
问题描述
我想,当用户点击特定的按钮来播放一短声。但问题是,我总是收到对象引用未设置为一个实例对象。指空!
I am trying to play a short sound when user taps on the specific button. But the problem is that I always receive Object reference not set to an instance object. means Null!
我第一次尝试MonoTouch.AudioToolBox.SystemSound。
I first tried MonoTouch.AudioToolBox.SystemSound.
MonoTouch.AudioToolbox.AudioSession.Initialize();
MonoTouch.AudioToolbox.AudioSession.Category = MonoTouch.AudioToolbox.AudioSessionCategory.MediaPlayback;
MonoTouch.AudioToolbox.AudioSession.SetActive(true);
var t = MonoTouch.AudioToolbox.SystemSound.FromFile("click.mp3");
t.PlaySystemSound();
让我注意到,click.mp3是我的根文件夹的解决方案,它被标记为内容。
中的另一种方法是 MonoTouch.AVFoundation.AVAudioPlayer
。
Let me notice that "click.mp3" is in my root solution folder and it is flagged as Content.
The other approach is MonoTouch.AVFoundation.AVAudioPlayer
.
var url = NSUrl.FromFilename("click.mp3");
AVAudioPlayer player = AVAudioPlayer.FromUrl(url);
player.FinishedPlaying += (sender, e) => { player.Dispose(); };
player.Play();
但同样的错误。我GOOGLE了它,我看到很多人有这个问题。我们需要知道它是否是一个错误或没有。
But same error. I googled it and I see that many people has this problem. We need to know whether it is a bug or not.
推荐答案
您的代码看起来是正确的(我比较的代码的这里,这是能够播放音频)。
Your code looks correct (I compared to the code here, which is able to play audio).
什么可能是问题是,音频文件不包含在该应用束不知。您可以轻松地使用此代码检查:
What might be the problem is that the audio file isn't included in the app bundle somehow. You can easily check it with this code:
if (!System.IO.File.Exists ("click.mp3"))
Console.WriteLine ("bundling error");
这篇关于MonoTouch的:播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!