如何将mp3转换为pcm

时间:2018-11-21 03:10:57

标签: c# ios xamarin

mpmediaitem信息必须转换为字节数组流以用于服务器上载。所以下面的代码

Reference

async void SoundPickingMedia(object sender, ItemsPickedEventArgs args)
    {
        if (args.MediaItemCollection.Items != null)
            foreach (var item in args.MediaItemCollection.Items)
            {
                var settings = NSDictionary.FromObjectsAndKeys(new object[]
                {
                    NSNumber.FromUInt32((int)AudioFormatType.LinearPCM),
                    NSNumber.FromFloat(44100.0f),
                    NSNumber.FromUInt32(16),
                    NSNumber.FromUInt32(2),
                    NSNumber.FromBoolean(false),
                    NSNumber.FromBoolean(false),
                    NSNumber.FromBoolean(false)
                },
                new object[]
                {
                    AVAudioSettings.AVFormatIDKey,
                    AVAudioSettings.AVSampleRateKey,
                    AVAudioSettings.AVLinearPCMBitDepthKey,
                    AVAudioSettings.AVNumberOfChannelsKey,
                    AVAudioSettings.AVLinearPCMIsBigEndianKey,
                    AVAudioSettings.AVLinearPCMIsFloatKey,
                    AVAudioSettings.AVLinearPCMBitDepthKey,
                });

                NSError outError;
                byte[] src;
                int size = 0;
                using (var asset = new AVUrlAsset(item.AssetURL))
                using (var assetReader = new AVAssetReader(asset, out outError))
                using (var assetOutput = new AVAssetReaderTrackOutput(asset.Tracks[0], settings))
                {                     

                    if (outError != null)
                        return;

                    if (!assetReader.CanAddOutput(assetOutput))
                        return;

                    assetReader.AddOutput(assetOutput);

                    if (!assetReader.StartReading())
                        return;
                    using (var allData = new NSMutableData())
                    {
                        while (assetReader.Status != AVAssetReaderStatus.Completed)
                        {
                            using (var buffer = assetOutput.CopyNextSampleBuffer())
                            {
                                if (buffer == null)
                                    continue;
                                using (var blockbuffer = buffer.GetDataBuffer())
                                {
                                    if (blockbuffer == null)
                                        continue;
                                    size = (int)(blockbuffer.DataLength);
                                    if (size <= 0)
                                        continue;
                                    using (var data = NSMutableData.FromLength((nint)size))
                                    {
                                        var err = blockbuffer.CopyDataBytes(0, (uint)size, data.MutableBytes);
                                        if (err != CMBlockBufferError.None)
                                            continue;
                                        allData.AppendData(data);
                                    }
                                }
                            }
                        }
                        src = allData.ToArray();
                    }
                }
             }
          }

但是不播放字节数组。我认为您需要将pcm转换为mp3等。xamarin如何在c#环境中进行转换?

实际上,pcm数据可能效果很好。如果pcm无法正常工作,则应重新考虑。 T.T。

1 个答案:

答案 0 :(得分:0)

PCM是一种编码方案,不是文件格式。如果要将语音转换为mp3。我建议您可以使用Nuget的软件包NAudio.Lame

using System.IO;
using NAudio.Wave;
using NAudio.Lame;

public static class Codec
{
 // Convert WAV to MP3 using libmp3lame library
 public static void WaveToMP3(string waveFileName, string mp3FileName, int bitRate = 128)
  {
    using (var reader = new AudioFileReader(waveFileName))
    using (var writer = new LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate))
        reader.CopyTo(writer);
  }

 // Convert MP3 file to WAV using NAudio classes only
 public static void MP3ToWave(string mp3FileName, string waveFileName)
  {
    using (var reader = new Mp3FileReader(mp3FileName))
    using (var writer = new WaveFileWriter(waveFileName, reader.WaveFormat))
        reader.CopyTo(writer);
  }
}