4

I am trying to play a short sound when user taps on the specific button. But the problem is that I always receive Object reference not set to an instance object. means Null!

I first tried MonoTouch.AudioToolBox.SystemSound.

MonoTouch.AudioToolbox.AudioSession.Initialize();   
MonoTouch.AudioToolbox.AudioSession.Category = MonoTouch.AudioToolbox.AudioSessionCategory.MediaPlayback;   
MonoTouch.AudioToolbox.AudioSession.SetActive(true);   

var t = MonoTouch.AudioToolbox.SystemSound.FromFile("click.mp3");
t.PlaySystemSound();

Let me notice that "click.mp3" is in my root solution folder and it is flagged as Content. The other approach is MonoTouch.AVFoundation.AVAudioPlayer.

var url = NSUrl.FromFilename("click.mp3");
AVAudioPlayer player = AVAudioPlayer.FromUrl(url);
player.FinishedPlaying += (sender, e) => { player.Dispose(); };
player.Play();

But same error. I googled it and I see that many people has this problem. We need to know whether it is a bug or not.

poupou
  • 43,413
  • 6
  • 77
  • 174
Peyman
  • 3,059
  • 1
  • 33
  • 68
  • Note: the top 3 lines above are deprecated in iOS7 (Initialize, Category, SetActive) ... works great without them. – Ender2050 Apr 26 '14 at 15:11

3 Answers3

7

About using SystemSound and MP3 see this question and answer: Playing a Sound With Monotouch

For AVAudioPlayer be aware that the following pattern is dangerous:

AVAudioPlayer player = AVAudioPlayer.FromUrl(url);
player.FinishedPlaying += (sender, e) => { player.Dispose(); };
player.Play();

since Play is asynchronous. This means the managed player instance can get out of scope before FinishedPlaying occurs. In turn this out of scope means the the GC could already have collected the instance.

A way to fix this is to promote the player local variable into a type field. That will ensure the GC won't collect the instance while the sound is playing.

Community
  • 1
  • 1
poupou
  • 43,413
  • 6
  • 77
  • 174
  • The player won't be disposed before the callback happens. The OS has a live reference to the callback lambda (it has to, to actually do the callback when it's finished playing). Until that reference goes away, player itself is still in the live object graph. After the call to player.Dispose(), player is eligible for GC, but not before that point. – James Moore Jul 07 '16 at 17:30
4

Your code looks correct (I compared to the code here, which is able to play audio).

What might be the problem is that the audio file isn't included in the app bundle somehow. You can easily check it with this code:

if (!System.IO.File.Exists ("click.mp3"))
    Console.WriteLine ("bundling error");
Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86
  • Awesome! you are right. I tried your approach and I see that this file does not exists. So I checked the file again, and now I see that it is not flagged as Content. I change it, but it won't work. I deleted the obj folder and the problem finally solved. Thank you. – Peyman Mar 28 '12 at 15:34
  • Still does not work for me. The sound file exists. I set a brakpoint. My `sound.PlaySystemSound()` line gets hit, and `sound` is a valid instance of SystemSound. My device volume is turned all the way up. My sound doesn't play. Period. I'm beginning to really hate Xamarin.iOS for just about NEVER doing what it says it does, even in the most stupid simple scenarios. – Perrin Larson Feb 01 '16 at 23:38
1

In most cases it would be the File does not exist. If you are like me, and you made sure that the file exists. Ensure the following:

  1. The Path of the file should be relative to your Class (ie: Sounds\beep.wav) (Absolute path did not work for me on the simulator)
  2. Ensure that you are defining the SoundSystem in the class level. This is because MT has a ver agressive Garbage Collector and could dispose your SoundSystem before it even starts playing. see this question
Community
  • 1
  • 1
Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37