You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Android音频文件分割与拼接实现方案咨询

Audio Manipulation in Xamarin.Android: Split, Record, Concatenate

Hey there! I’ve built similar audio features for Xamarin.Android apps before, so let’s walk through your questions step by step.

Can this be done without third-party libraries?

It’s partially possible, but with big caveats:

  • Recording audio: Yes, you can use Xamarin.Android’s built-in MediaRecorder class to capture audio and save it in formats like WAV or MP3. This is straightforward and doesn’t require external dependencies.
  • Splitting/concatenating audio: This gets tricky. For uncompressed formats like WAV, you could manually parse the file header, calculate byte offsets for splitting, and concatenate raw PCM data (then rewrite a valid header). But for compressed formats like MP3, there’s no native Android API to handle frame-level manipulation—you’d have to implement complex decoding/encoding logic yourself, which is error-prone and time-consuming. Unless you’re only working with WAV and have the bandwidth to handle low-level file operations, I’d recommend avoiding this path.

If you want reliable, maintainable code, these libraries are your best bet:

  • NAudio: A mature, .NET-focused audio library that works seamlessly with Xamarin.Android. It supports recording, splitting, concatenating, and converting most common audio formats (MP3, WAV, etc.). The API is intuitive, and there’s plenty of community support for troubleshooting.
  • FFmpeg via Xamarin bindings: For more advanced use cases (like handling rare formats or complex processing), you can use a Xamarin-compatible FFmpeg library. FFmpeg is a powerhouse for media manipulation, and you can control it via command-line-style commands. Just note that it adds some app size overhead, so you’ll need to configure your build to include only the necessary codecs.

Quick code snippets to get started

Recording with NAudio

using NAudio.Wave;

var outputPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "recording.wav");
var recorder = new WaveInEvent();
recorder.WaveFormat = new WaveFormat(44100, 16, 1); // 44.1kHz, 16-bit, mono

using (var writer = new WaveFileWriter(outputPath, recorder.WaveFormat))
{
    recorder.DataAvailable += (sender, e) =>
    {
        writer.Write(e.Buffer, 0, e.BytesRecorded);
    };

    recorder.StartRecording();
    // Stop recording later with recorder.StopRecording();
}

Splitting an MP3 with NAudio

using NAudio.Wave;

var inputPath = "path/to/input.mp3";
var outputPath = "path/to/split_part.mp3";
var splitDuration = TimeSpan.FromSeconds(30); // Keep first 30 seconds

using (var reader = new Mp3FileReader(inputPath))
using (var writer = new Mp3FileWriter(outputPath, reader.WaveFormat))
{
    var bytesToRead = (int)(reader.WaveFormat.AverageBytesPerSecond * splitDuration.TotalSeconds);
    var buffer = new byte[1024];
    int bytesRead;

    while (bytesToRead > 0 && (bytesRead = reader.Read(buffer, 0, Math.Min(buffer.Length, bytesToRead))) > 0)
    {
        writer.Write(buffer, 0, bytesRead);
        bytesToRead -= bytesRead;
    }
}

Concatenating audio files with NAudio

using NAudio.Wave;

var outputPath = "path/to/concatenated.mp3";
var inputFiles = new List<string> { "file1.mp3", "file2.mp3" };

using (var writer = new Mp3FileWriter(outputPath, new Mp3WaveFormat(44100, 1, 128000)))
{
    foreach (var file in inputFiles)
    {
        using (var reader = new Mp3FileReader(file))
        {
            reader.CopyTo(writer);
        }
    }
}

内容的提问来源于stack exchange,提问作者claudiu.tms

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:37:47