Playing a Sound With Monotouch

前端 未结 2 1215
野趣味
野趣味 2020-12-20 00:13

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          


        
相关标签:
2条回答
  • 2020-12-20 00:27

    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

    0 讨论(0)
  • 2020-12-20 00:34

    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

    0 讨论(0)
提交回复
热议问题