播放声音的MonoTouch [英] Playing a Sound With Monotouch

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

问题描述

不管我什么(建设 - >内容,NSURL,文件名)我得到一个零例外:当我尝试在MonoTouch的发挥.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();

我也使用文件夹名称图像/ 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.

下面是在MonoTouch的添加声音的视频的链接。 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?

推荐答案

布莱恩,

从看你的截屏 - 你正在尝试播放MP3文件,而不是一个CAF文件。 MP3文件连接codeD不同,不会与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();

您可能需要调整上述code - 我没有MonoDevelop的手,但应该帮助你远一点

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

干杯,

ChrisNTR

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆