简体   繁体   中英

My SFML application crash?

I am coding in C# with SFML and Xamarin Studio on my Mac an application to record audio input microphone. Look at the code:

using System;
using Gtk;
using SFML;
using SFML.Audio;
using SFML.System;

public partial class MainWindow: Gtk.Window
{
    public MainWindow () : base (Gtk.WindowType.Toplevel)
    {
        Build ();
    }

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }

    static SoundBufferRecorder recorder;


    protected void OnButton2Clicked (object sender, EventArgs e)
    {
        recorder.Start (44110);
    }

    protected void OnButton3Clicked (object sender, EventArgs e)
    {
        recorder.Stop ();
        SoundBuffer sf = recorder.SoundBuffer;
        sf.SaveToFile ("audio.mp3");
    }

    protected void OnButton1Clicked (object sender, EventArgs e)
    {
        Sound sound = new Sound (recorder.SoundBuffer);
        sound.Play ();
    }
}

When I click on OnButton3Clicked... My application crashes without give any reason, why ?

You never actually instantiated your recorder:

static SoundBufferRecorder recorder;

This means recorder is null and you will get an "Object not set to an instance"-error.

static SoundBufferRecorder recorder = new SoundBufferRecorder();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM