我在C#中使用SFML和Xamarin Studio在我的Mac上编写一个记录音频输入麦克风的应用程序。看一下代码:
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 ();
}
}
当我点击OnButton3Clicked时...我的应用程序在没有任何理由的情况下崩溃,为什么?
答案 0 :(得分:0)
你从未真正实例化你的录音机:
static SoundBufferRecorder recorder;
这意味着记录器为null
,您将获得“未设置为实例的对象” - 错误。
static SoundBufferRecorder recorder = new SoundBufferRecorder();