0

I'm trying to play an .wav audio file, for which I followed this answer.

I followed this answer but I get an error message on audio.Load(stream); says System.NullReferenceException: Object reference not set to an instance of an object.

To me it seems that the problem is that, audio.load() couldn't find the audio file, which is in Resource/raw path.

Complete Code

private void Counter(object sender, EventArgs e)
{
    timer_countdown = new Timer();
    timer_countdown.Interval = 1000;
    timer_countdown.Elapsed += OnTimedEventIDCamera;
    timer_countdown.Enabled = true;
    timer_countdown.AutoReset = true;
    timer_countdown.Start();
}

private void OnTimedEventIDCamera(object source, ElapsedEventArgs e)
{
    Seconds++;

    if (Seconds == 3)
    {
        var stream = GetStreamFromFile("beepSound.wav");
        var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
        audio.Load(stream);
        audio.Play();
    }
}

Stream GetStreamFromFile(string filename)
{
    var assembly = typeof(App).GetTypeInfo().Assembly;

    var stream = assembly.GetManifestResourceStream("yourprojectname." + filename);

    return stream;
}
4

1 Answer 1

4

According to your description and code, you have one audio file in Android platform Resource/raw folder. If you use the code you provide above, you can not get audio file and stream is null.

You can put .wav audio file in Forms shared and setting Build Action to Embedded Resource

 private void Btnwav(object sender, EventArgs e)
    {
        var stream = GetStreamFromFile("xxx.wav");
        var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
        audio.Load(stream);
        audio.Play();
    }
    Stream GetStreamFromFile(string filename)
    {
        var assembly = typeof(App).GetTypeInfo().Assembly;

        var stream = assembly.GetManifestResourceStream("yourprojectname." + filename);

        return stream;
    }

Update:

My project name is playvideo, add minions.mp4

 private void Btnwav(object sender, EventArgs e)
    {
        var stream = GetStreamFromFile("minions.mp4");
        var audio = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
        audio.Load(stream);
        audio.Play();
    }
    Stream GetStreamFromFile(string filename)
    {
        var assembly = typeof(App).GetTypeInfo().Assembly;

        var stream = assembly.GetManifestResourceStream("playvideo." + filename);

        return stream;
    }

enter image description here

3
  • thanks for response. what do u mean by Forms shared? how to put file in Forms shared? and this yourprojectname. will be different for my project?
    – Stavrogin
    Commented Jan 11, 2021 at 7:50
  • @Tarkovsky Please see my update code and image. The Forms shared is .Net standard library project(shared code). Commented Jan 11, 2021 at 8:05
  • @Tarkovsky I have updated my reply, please take a look. Commented Jan 11, 2021 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.