Create a gist now

Instantly share code, notes, and snippets.

@yuzoh /volume.cs
Last active Apr 20, 2017

What would you like to do?
Xamarinで音量を検出するコード
using AudioToolbox;
private InputAudioQueue mAudioQueue;
private NSTimer mAudioTimer;
public void autoStart(){
AudioStreamBasicDescription basic = new AudioStreamBasicDescription();
basic.SampleRate = 44100.00;
basic.Format = AudioFormatType.LinearPCM;
basic.FormatFlags = AudioFormatFlags.LinearPCMIsBigEndian |
AudioFormatFlags.LinearPCMIsSignedInteger |
AudioFormatFlags.LinearPCMIsPacked;
basic.BytesPerPacket = 2;
basic.BytesPerFrame = 2;
basic.FramesPerPacket = 1;
basic.ChannelsPerFrame = 1;
basic.BitsPerChannel = 16;
mAudioQueue = new InputAudioQueue(basic);
mAudioQueue.Start();
mAudioQueue.EnableLevelMetering = true;
mAudioTimer = NSTimer.CreateRepeatingTimer(0.5,(obj) => {
workVolume();
});
NSRunLoop.Current.AddTimer(mAudioTimer, NSRunLoopMode.Default);
}
void workVolume(){
int channel = 0;
AudioQueueLevelMeterState [] levels = mAudioQueue.CurrentLevelMeterDB;
Console.WriteLine("{0} {1}", levels[channel].AveragePower, levels[channel].PeakPower);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment