本文介绍了使用 Monotouch 播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我尝试什么(构建 -> 内容、NSUrl、文件名),我都会收到空异常":当我尝试在单声道中播放 .caf 声音文件时找不到文件.

No matter what I try (build -> content, NSUrl, filename) I get a 'null exception': file not found when I try to play a .caf sound file in monotouch.

//var path = NSBundle.MainBundle.PathForResource("MatchGame", "caf");
//var gameSong = SystemSound.FromFile( new NSUrl(path, false));
var gameSong = SystemSound.FromFile("MatchGame.caf");
gameSong.PlaySystemSound();

我还尝试组合使用文件夹名称images/MatchGame.caf"并将 MatchGame.caf 移动到根文件夹中.

I also try combinations using the folder name "images/MatchGame.caf" and moving MatchGame.caf into the root folder.

我错过了什么?非常感谢.

What am I missing? Thanks a lot.

这是在单点触控中添加声音的视频的链接.http://www.screencast.com/t/MmE0ZmFh有什么问题?

Here is a link to a video of adding the sound in monotouch. http://www.screencast.com/t/MmE0ZmFh What is wrong?

推荐答案

Bryan,

从您的截屏视频来看 - 您正在尝试播放 mp3 文件而不是 caf 文件.Mp3 文件的编码方式不同,并且无法与存在的 SystemSound 类一起播放(我不记得您是否可以在 Obj-C 中执行此操作.)

From looking at your screencast - you are trying to play a mp3 file and not a caf file. Mp3 files are encoded differently and will not play with the SystemSound class that's there (I can't remember if you can do this in Obj-C or not.)

您需要使用 AVFoundation 命名空间和 AVAudioPlayer 类.

You'll want to use the AVFoundation Namespace and AVAudioPlayer class.

using Monotouch.AVFoundation;

var mediaFile = NSUrl.FromFilename("myMp3.mp3");
var audioPlayer = AVAudioPlayer.FromUrl(mediaFile);
audioPlayer.FinishedPlaying += delegate { audioPlayer.Dispose(); };
audioPlayer.Play();

您可能需要调整上面的代码 - 我手头没有 MonoDevelop,但这应该会对您有所帮助.

You might need to tweak the code above - I don't have MonoDevelop to hand but that should help you a little further.

干杯,

克里斯NTR

这篇关于使用 Monotouch 播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:48