我目前正在使用 csharp 在 Xamarin 工作室中为 Android 制作一个音乐应用程序。我正在制作一个简单的合成器应用程序,目前我被 csharp 的 GetMinBufferSize 方法困住了。
Xamarin.Android 文档为 GetMinBufferSize 方法提供了以下语法(这里是链接:xamarin API 音轨文档:
[Android.Runtime.Register("getMinBufferSize", "(III)I", "")]
public static int GetMinBufferSize (int sampleRateInHz,
[Android.Runtime.GeneratedEnum] ChannelOut channelConfig,
[Android.Runtime.GeneratedEnum] Encoding audioFormat)
- 它对应于 Java 中的以下内容(链接:android 参考):
公共静态 int getMinBufferSize (int sampleRateInHz, int channelConfig, int audioFormat)
我不明白这些东西是什么以及我应该如何使用它们:[Android.Runtime.Register("getMinBufferSize", "(III)I", "")] [Android.Runtime.GeneratedEnum] [Android.Runtime.Register("getMinBufferSize", "(III)I", "")] Runtime.GeneratedEnum]
Java 中的代码更简单:
int buffsize = AudioTrack.getMinBufferSize(_sampleRate,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
其中 _sampleRate 是 int _sampleRate = 44100;表示频率率。
因此,如果您至少告诉我 xamarin 文档中括号中的那三行是什么 - 我将不胜感激。
提前感谢您,祝您有美好的一天。
到目前为止我的代码:
namespace simple_synth
{
[Activity (Label = "_secondAct")] //activity that opens the second screen
public class _secondAct : Activity
{
Thread _thread; //audio processing thread
int _sRate = 44100; //sampling rate
bool isRunning = true; //switch on/off
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout._secondLay);
Button btn2 = FindViewById<Button> (Resource.Id.myButton_synth);
_audio _audioSound = new _audio ();
btn2.Click += (sender, e) => {
btn2.Text = "GOOD";
Thread _audioThread = new Thread(_audioSound._makeSound);
_audioThread.Start();
Console.WriteLine("audio thread: started");
while (!_audioThread.IsAlive);
Thread.Sleep(1000);
_audioSound._stopRequest();
_audioThread.Join();
Console.WriteLine("audio thread: terminated now!");
_audioSound._startRequest();
};
}
}//_secondAct
public class _audio{
private volatile bool _stopItNow;
public void _makeSound(){ // This method will be called when the thread is started.
while (!_stopItNow) {
Console.WriteLine ("audio thread: is playing the sound...");
AudioTrack _audioTrack = new AudioTrack (Stream.Music, 22050,
ChannelConfiguration.Mono,
Android.Media.Encoding.Pcm16bit,
_audBuffer.Length, AudioTrackMode.Stream);
_audioTrack.Play ();
_audioTrack.Write (_audBuffer, 0, _audBuffer.Length);
}
Console.WriteLine ("audio thread: terminated.");
} //doWork
public void _stopRequest()
{
_stopItNow = true;
}
public void _startRequest()
{
_stopItNow = false;
}
}
}