English 中文(简体)
Playing a Sound With Monotouch
原标题:

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();

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.

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

最佳回答

Bryan,

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.)

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();

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

Cheers,

ChrisNTR

问题回答

You need the first line:

var path = NSBundle.MainBundle.PathForResource("MatchGame", "caf");

Then make sure that your audio file is included in the application by making sure that your CAF file is flagged as "Content" in the Properties pane, otherwise the file is not copied to the resulting application package (your .app)

You can follow the steps documented here (they are for images, but apply the same to audio files):

http://wiki.monotouch.net/HowTo/Images/Add_an_Image_to_your_Project

Additionally, this is a good resource for where you should store files:

http://wiki.monotouch.net/HowTo/Files/HowTo%3a_Store_Files





相关问题
Playing a Sound With Monotouch

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("...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

Play sound in iPhone web app with Javascript?

I m currently creating an iPhone web application for piano chords, and I wanted to add an option that would let the user press a Play button and play the selected chord. However, I m not sure how to ...

热门标签