0

I am trying to play audio only if it isn't already playing.

I use this method in my class :

private MediaPlayer mediaPlayer;

public bool HasAudio(){
  return mediaPlayer.HasAudio; // this returns null and it says property is readonly
}

Sound game = new Sound();
if (game.HasAudio() == false)//play sound

How do I get true or false value?

Edit: Sound class structure, Class instance

f.macek
  • 3
  • 3
  • 2
    If you debug your code, is mediaPlayer null ? Then you should create a new instance of MediaPlayer with the keyword ``new`` – Antoine Thiry Dec 14 '18 at 13:01
  • Can you show us where exactly you instantiate MediaPlayer, and if Sound inherits from MediaPlayer somehow? Because currently your code shows you checking a null private object to see if it has audio, but then ignoring it and calling HasAudio from the Sound class? – zuckerburg Dec 14 '18 at 13:03
  • If `mediaPlayer` is `null`, the method won't return `null`, it'll throw `NullReferenceException` becuase you're trying to access a property on a `null` object. If `mediaPlayer` is fine, `HasAudio` can't be `null` because it's not a nullable type. Unless the `MediaPlayer` class is yours? What namespace does it belong to? – Furkan Kambay Dec 14 '18 at 13:26
  • Sidenote: you should not instantiate a new `MediaPlayer` each time you `Play` a file. (That might cause problems with garbage collection if the method gets called a lot) Instead, create it in the constructor and just `Open` the file in `Play` and call `.Play( )` on it. – Furkan Kambay Dec 14 '18 at 13:27
  • Thanks a lot! The problem was caused by not creating new MediaPlayer() before I used the property. So I put it to constructor as @FurkanKambay recommended. – f.macek Dec 14 '18 at 13:37
  • @f.macek Glad you solved it. I was just writing the answer. You should describe the problem a little more clearly next time. You said "this returns null", it actually throws a `NullReferenceException`. So this question is a duplicate of one of the most-asked question on SO: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Furkan Kambay Dec 14 '18 at 13:46
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Furkan Kambay Dec 14 '18 at 13:47

1 Answers1

0

I think I get what's going on here.

Here, you're instantiating the Sound, and then calling HasAudio on it. At this point, it's trying to access its m_mediaPlayer field but it is null because you're only instantiating it in the Play method. As I suggested in my sidenote comment, you need to first instantiate the MediaPlayer either in the constructor or in the field declaration.

internal class Sound
{
    public bool HasAudio { get { return mediaPlayer.HasAudio; } }

    private MediaPlayer mediaPlayer = new MediaPlayer();

    public void Play(string fileName)
    {
        mediaPlayer.Open(new Uri(@"sounds/" + fileName, UriKind.RelativeOrAbsoute));
        mediaPlayer.Play();
    }

    public void Stop()
    {
        mediaPlayer.Stop();
    }
}
Furkan Kambay
  • 751
  • 1
  • 7
  • 18