简体   繁体   中英

xamarin iOS: How to show the audio amplitude of the voice when recording

On Android, I have found to show the amplitude of the recording I can use getMaxAmplitude at a regular basis. Does iOS have the same function? I want to show the input voice volume meter while recording from the microphone.

Assuming you are using AVAudioRecorder you can enable metering and read the average and peak volumes (in decibels) for each audio channel.

Sample Microphone 10 times a second:

# AVAudioSettings setup ...
~~~
avAudioRecorder = AVAudioRecorder.Create(this.audioFilePath, new AudioSettings(settings), out error);
avAudioRecorder.MeteringEnabled = true;

var peakPower = 0.0;
var averagePower = 0.0;
// Note: No await, run the following Task synchronously!
Task.Run(async() => // Personally I would create my own Thread, but this works for a quick example
{
    while (true) // Do not create object/release objects or subtasks in this loop, it will cause poor performance due to GC.
    {
        if (!avAudioRecorder.Recording)
            return;
        avAudioRecorder.UpdateMeters();
        peakPower = avAudioRecorder.PeakPower(0);
        averagePower = avAudioRecorder.AveragePower(0);
        Console.WriteLine($"{averagePower}:{peakPower}");
        Thread.Sleep(100);
    }
});

Output:

2017-01-14 19:26:16.271 Sound[33399:19078946] -52.9222717285156:-48.1419296264648
2017-01-14 19:26:17.271 Sound[33399:19078946] -53.7662239074707:-45.7719497680664
2017-01-14 19:26:18.274 Sound[33399:19078946] -49.105827331543:-43.4426803588867
2017-01-14 19:26:19.275 Sound[33399:19078946] -9.97611618041992:4.59128427505493
2017-01-14 19:26:20.276 Sound[33399:19078946] -37.2582626342773:5.28962087631226
2017-01-14 19:26:21.276 Sound[33399:19078946] -24.4357891082764:4.00279855728149
2017-01-14 19:26:22.277 Sound[33399:19078946] -11.7632217407227:4.34794473648071
2017-01-14 19:26:23.278 Sound[33399:19078946] -32.4504356384277:5.66976404190063
2017-01-14 19:26:24.279 Sound[33399:19078946] -18.5233268737793:0.0205818619579077
2017-01-14 19:26:25.280 Sound[33399:19078946] -28.790828704834:-5.83084678649902

Ref: https://developer.apple.com/reference/avfoundation/avaudiorecorder

in decibels, for the sound being recorded. A return value of 0 dB indicates full scale, or maximum power; a return value of -160 dB indicates minimum power (that is, near silence).

If the signal provided to the audio recorder exceeds ±full scale, then the return value may exceed 0 (that is, it may enter the positive range).

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