public void TranscodeAudio(SourceMedia sourceMedia, TargetMedia targetMedia, TrimConfig trimConfig, TransformationState transformationState) { if (targetMedia.targetFile.Exists()) { targetMedia.targetFile.Delete(); } transformationState.requestId = UUID.RandomUUID().ToString(); /* MediaTransformationListener transformationListener = new MediaTransformationListener(context, transformationState.requestId, transformationState, targetMedia); */ MediaRange mediaRange = trimConfig.enabled ? new MediaRange( TimeUnit.Milliseconds.ToMicros((long)(trimConfig.range[0] * 1000)), TimeUnit.Milliseconds.ToMicros((long)(trimConfig.range[1] * 1000))) : new MediaRange(0, long.MaxValue); try { var targetMimeType = targetMedia.writeToWav ? "audio/raw" : "audio/mp4a-latm"; IMediaTarget mediaTarget; if (targetMedia.writeToWav) { mediaTarget = new WavMediaTarget(targetMedia.targetFile.Path); } else { mediaTarget = new MediaMuxerMediaTarget(targetMedia.targetFile.Path, 1, 0, (int)MuxerOutputType.Mpeg4); } var mediaSource = new MediaExtractorMediaSource(context, sourceMedia.uri, mediaRange); List<TrackTransform> trackTransforms = new List<TrackTransform>(1); foreach (TargetTrack targetTrack in targetMedia.tracks) { if (targetTrack.format is AudioTrackFormat trackFormat) { MediaFormat mediaFormat = MediaFormat.CreateAudioFormat( targetMimeType, trackFormat.samplingRate, trackFormat.channelCount); mediaFormat.SetInteger(MediaFormat.KeyBitRate, trackFormat.bitrate); mediaFormat.SetLong(MediaFormat.KeyDuration, trackFormat.duration); IEncoder encoder; if (targetMedia.writeToWav) { encoder = new PassthroughBufferEncoder(8192); } else { encoder = new MediaCodecEncoder(); } //IEncoder encoder = targetMedia.writeToWav ? new PassthroughBufferEncoder(8192) : new MediaCodecEncoder(); TrackTransform trackTransform = new TrackTransform.Builder(mediaSource, targetTrack.sourceTrackIndex, mediaTarget) .SetTargetTrack(0) .SetDecoder(new MediaCodecDecoder()) .SetEncoder(encoder) .SetRenderer(new AudioRenderer(encoder)) .SetTargetFormat(mediaFormat) .Build(); trackTransforms.Add(trackTransform); break; } } _mediaTransformer.Transform( transformationState.requestId, trackTransforms, this, MediaTransformer.GranularityDefault); } catch (System.Exception err) { System.Diagnostics.Debug.WriteLine($"Exception when trying to transcode audio: {err.Message}"); } }