Search code examples
xamarinavfoundationavaudioplayeravaudioengineavaudiofile

Get samples of audio clip in AVAudioPlayer?


Is there a property on the AVAudioPlayer class that I can use to get the samples? If not is there another class I can use to get this information?

Here's what I have:

var openDialog = NSOpenPanel.OpenPanel;
openDialog.CanChooseFiles = true;
openDialog.CanChooseDirectories = false;
openDialog.AllowedFileTypes = new string[] { "wav" };

if (openDialog.RunModal() == 1)
{

    var url = openDialog.Urls[0];

    if (url != null)
    {
        var path = url.Path;
        var audioplayer = AVFoundation.AVAudioPlayer.FromUrl(file);
        var samples = audioplayer.SAMPLES?;

Visual Studio Mac (C# / Xamarin)


Solution

  • AVAudioPlayer does not give you access to the sample data, but if you switch playback to AVPlayer you can use an MTAudioProcessingTap to "tap" the samples as they are played.

    If you simply want to examine the samples in your file you can use AVAudioFile.

    // get the total number of samples
    var audioFile = new AVAudioFile(file, out outError);
    var samples = audioFile.Length;