Playing a Sound With Monotouch

前端 未结 2 1223
野趣味
野趣味 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

提交回复
热议问题