Reputation: 5634
I am recording voice in my .net maui app in iOS using AVFoundation.AVAudioRecorder.
private AVAudioRecorder recorder;
...
{
var filePath = GetTempFilePath();
url = NSUrl.FromFilename(filePath);
audioFilePath = filePath;
NSObject[] values =
{
//NSNumber.FromFloat(44100.0f), //Sample rate
NSNumber.FromFloat(16000.0f), //Sample rate
NSNumber.FromInt32((int)AudioFormatType.MPEG4AAC), //AVFormat
//NSNumber.FromInt32((int)AudioFormatType.Flac), //AVFormat
NSNumber.FromInt32(1), //Channel
//NSNumber.FromInt32((int)AVAudioQuality.Low), //PCMBitDept
NSNumber.FromInt32(16), //PCMBitDept
NSNumber.FromBoolean(false), //IsBigEndianKey
NSNumber.FromBoolean(false) //IsFloatKey
};
NSObject[] keys =
{
AVAudioSettings.AVSampleRateKey,
AVAudioSettings.AVFormatIDKey,
AVAudioSettings.AVNumberOfChannelsKey,
AVAudioSettings.AVLinearPCMBitDepthKey,
AVAudioSettings.AVLinearPCMIsBigEndianKey,
AVAudioSettings.AVLinearPCMIsFloatKey
};
settings = NSDictionary.FromObjectsAndKeys(values, keys);
recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);
recorder.PrepareToRecord();
}
recorder.Record();
Once the audio has been recorded, I read the byte[] of the temp file, store it in my db, and then pull it down to playback in Android using Android.Media.MediaPlayer. The Android player is as follows:
var stream = new MemoryStream(audioBytes);
var mediaSource = new StreamMediaDataSource(stream);
player = new MediaPlayer();
player.Completion += OnPlaybackEnded;
player.SetDataSource(mediaSource);
player.Prepare();
The StreamMediaDataSource class is as follows:
public class StreamMediaDataSource(Stream data) : Android.Media.MediaDataSource
{
private Stream data = data;
public override long Size => data.Length;
public override int ReadAt(long position, byte[] buffer, int offset, int size)
{
ArgumentNullException.ThrowIfNull(buffer);
if (data.CanSeek)
{
data.Seek(position, SeekOrigin.Begin);
}
return data.Read(buffer, offset, size);
}
Recording on iOS and playing back on iOS works fine. Recording on Android (recorder not above) and playing back on Android works fine. Recording on Android (recorder not above) and playing back on iOS (player not above) works fine.
Recording on iOS and playing back on Android - FAILS.
The player gets the audio byte[], loads into in memory stream, but doesn't seem to load at player.Prepare. The return player has no data compared to when I had recorded something on Android and playback on Android.
Exception: java.io.IOException: Prepare failed.: status=0x1 at android.media.MediaPlayer._prepare(Native Method) at android.media.MediaPlayer.prepare(MediaPlayer.java:1441)
[![enter image description here][1]][1]
I have spent quite some time trying to debug this. I have tried several combinations of Sample Rate, AVFormat, PCMBitDepth (commented above to show) as well. Any suggestions? [1]: https://i.sstatic.net/iVd3T2Tj.png
Upvotes: 0
Views: 72