简体   繁体   English

Android.Media.AudioTrack.GetMinBufferSize方法-如何在Xamarin.Android(c#)中使用其语法?

[英]Android.Media.AudioTrack.GetMinBufferSize Method - how to use its syntax in Xamarin.Android (c#)?

I am currently making a music app for Android in Xamarin studio using csharp. 我目前正在使用csharp在Xamarin Studio中为Android开发音乐应用。 I am making a simple synthesizer app and at the moment I am stuck with GetMinBufferSize method with csharp. 我正在制作一个简单的合成器应用程序,此刻,我陷入了csharp的GetMinBufferSize方法的困扰。

Xamarin.Android documentation provides the following syntax for the GetMinBufferSize method (here's the link: xamarin API audiotrack documentation : 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)

public static int getMinBufferSize (int sampleRateInHz, int channelConfig, int audioFormat) 公共静态int getMinBufferSize(int sampleRateInHz,int channelConfig,int audioFormat)

The thing I do not understand is what are theses things and how I should use them: [Android.Runtime.Register("getMinBufferSize", "(III)I", "")] [Android.Runtime.GeneratedEnum] [Android.Runtime.GeneratedEnum] 我不明白的是这些东西是什么,以及如何使用它们:[Android.Runtime.Register(“ getMinBufferSize”,“(III)I”,“”)] [Android.Runtime.GeneratedEnum] [Android。 Runtime.GeneratedEnum]

The code in Java was easier: Java代码更容易:

int buffsize = AudioTrack.getMinBufferSize(_sampleRate, 
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

where _sampleRate is int _sampleRate = 44100; 其中_sampleRate为整型_sampleRate = 44100; and represents frequency rate. 代表频率

So, if you at least told me what those three lines in brackets from xamarin documentation are - I would be so grateful. 因此,如果您至少告诉我xamarin文档括号中的三行是什么,我将非常感激。

Thank you in advance and have a great day. 在此先感谢您,祝您度过愉快的一天。

My code so far: 到目前为止,我的代码:

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;
}
}
}

Very simple in C# as well: 在C#中也非常简单:

var minBufSize = AudioTrack.GetMinBufferSize (
    44100, 
    ChannelOut.Mono, 
    Encoding.Pcm16bit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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