How to convert mp3 to pcm












0














The mpmediaitem information must be converted to a byte array stream for server upload. So the code below



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();
}
}
}
}


But the byte array is not played. In my opinion, you need to convert pcm to mp3, etc. xamarin How to convert in c# environment?



In fact, pcm data may work well. If the pcm does not work properly, you should think back to the beginning. T.T.










share|improve this question





























    0














    The mpmediaitem information must be converted to a byte array stream for server upload. So the code below



    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();
    }
    }
    }
    }


    But the byte array is not played. In my opinion, you need to convert pcm to mp3, etc. xamarin How to convert in c# environment?



    In fact, pcm data may work well. If the pcm does not work properly, you should think back to the beginning. T.T.










    share|improve this question



























      0












      0








      0







      The mpmediaitem information must be converted to a byte array stream for server upload. So the code below



      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();
      }
      }
      }
      }


      But the byte array is not played. In my opinion, you need to convert pcm to mp3, etc. xamarin How to convert in c# environment?



      In fact, pcm data may work well. If the pcm does not work properly, you should think back to the beginning. T.T.










      share|improve this question















      The mpmediaitem information must be converted to a byte array stream for server upload. So the code below



      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();
      }
      }
      }
      }


      But the byte array is not played. In my opinion, you need to convert pcm to mp3, etc. xamarin How to convert in c# environment?



      In fact, pcm data may work well. If the pcm does not work properly, you should think back to the beginning. T.T.







      c# ios xamarin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 8:15









      James Westgate

      8,64654258




      8,64654258










      asked Nov 21 at 3:10









      최신혜

      1




      1
























          1 Answer
          1






          active

          oldest

          votes


















          0














          PCM is an encoding scheme,not a file format.If you want to convert your voice to mp3 .I recommend that you can use the package NAudio.Lame from the Nuget.



          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);
          }
          }





          share|improve this answer





















          • Thank you very much Lucas Zhang But the key is to create an mp3 mpmediaitem using the audio library mpmediapickercontroller. pcm data is only one of the processes. Do you know Lucas Zhang? How to convert mpmediaItem to WAV or MP3
            – 최신혜
            Nov 22 at 5:49










          • Can you provide the code about play the voice?
            – Lucas Zhang - MSFT
            Nov 22 at 7:01










          • I can provide you with music, but it is probably a useless mp3 file. There is an API in my application that uploads data to aws in byte format, and Android has already been tested.
            – 최신혜
            Nov 22 at 7:55











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404751%2fhow-to-convert-mp3-to-pcm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          PCM is an encoding scheme,not a file format.If you want to convert your voice to mp3 .I recommend that you can use the package NAudio.Lame from the Nuget.



          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);
          }
          }





          share|improve this answer





















          • Thank you very much Lucas Zhang But the key is to create an mp3 mpmediaitem using the audio library mpmediapickercontroller. pcm data is only one of the processes. Do you know Lucas Zhang? How to convert mpmediaItem to WAV or MP3
            – 최신혜
            Nov 22 at 5:49










          • Can you provide the code about play the voice?
            – Lucas Zhang - MSFT
            Nov 22 at 7:01










          • I can provide you with music, but it is probably a useless mp3 file. There is an API in my application that uploads data to aws in byte format, and Android has already been tested.
            – 최신혜
            Nov 22 at 7:55
















          0














          PCM is an encoding scheme,not a file format.If you want to convert your voice to mp3 .I recommend that you can use the package NAudio.Lame from the Nuget.



          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);
          }
          }





          share|improve this answer





















          • Thank you very much Lucas Zhang But the key is to create an mp3 mpmediaitem using the audio library mpmediapickercontroller. pcm data is only one of the processes. Do you know Lucas Zhang? How to convert mpmediaItem to WAV or MP3
            – 최신혜
            Nov 22 at 5:49










          • Can you provide the code about play the voice?
            – Lucas Zhang - MSFT
            Nov 22 at 7:01










          • I can provide you with music, but it is probably a useless mp3 file. There is an API in my application that uploads data to aws in byte format, and Android has already been tested.
            – 최신혜
            Nov 22 at 7:55














          0












          0








          0






          PCM is an encoding scheme,not a file format.If you want to convert your voice to mp3 .I recommend that you can use the package NAudio.Lame from the Nuget.



          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);
          }
          }





          share|improve this answer












          PCM is an encoding scheme,not a file format.If you want to convert your voice to mp3 .I recommend that you can use the package NAudio.Lame from the Nuget.



          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);
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 2:24









          Lucas Zhang - MSFT

          1,770127




          1,770127












          • Thank you very much Lucas Zhang But the key is to create an mp3 mpmediaitem using the audio library mpmediapickercontroller. pcm data is only one of the processes. Do you know Lucas Zhang? How to convert mpmediaItem to WAV or MP3
            – 최신혜
            Nov 22 at 5:49










          • Can you provide the code about play the voice?
            – Lucas Zhang - MSFT
            Nov 22 at 7:01










          • I can provide you with music, but it is probably a useless mp3 file. There is an API in my application that uploads data to aws in byte format, and Android has already been tested.
            – 최신혜
            Nov 22 at 7:55


















          • Thank you very much Lucas Zhang But the key is to create an mp3 mpmediaitem using the audio library mpmediapickercontroller. pcm data is only one of the processes. Do you know Lucas Zhang? How to convert mpmediaItem to WAV or MP3
            – 최신혜
            Nov 22 at 5:49










          • Can you provide the code about play the voice?
            – Lucas Zhang - MSFT
            Nov 22 at 7:01










          • I can provide you with music, but it is probably a useless mp3 file. There is an API in my application that uploads data to aws in byte format, and Android has already been tested.
            – 최신혜
            Nov 22 at 7:55
















          Thank you very much Lucas Zhang But the key is to create an mp3 mpmediaitem using the audio library mpmediapickercontroller. pcm data is only one of the processes. Do you know Lucas Zhang? How to convert mpmediaItem to WAV or MP3
          – 최신혜
          Nov 22 at 5:49




          Thank you very much Lucas Zhang But the key is to create an mp3 mpmediaitem using the audio library mpmediapickercontroller. pcm data is only one of the processes. Do you know Lucas Zhang? How to convert mpmediaItem to WAV or MP3
          – 최신혜
          Nov 22 at 5:49












          Can you provide the code about play the voice?
          – Lucas Zhang - MSFT
          Nov 22 at 7:01




          Can you provide the code about play the voice?
          – Lucas Zhang - MSFT
          Nov 22 at 7:01












          I can provide you with music, but it is probably a useless mp3 file. There is an API in my application that uploads data to aws in byte format, and Android has already been tested.
          – 최신혜
          Nov 22 at 7:55




          I can provide you with music, but it is probably a useless mp3 file. There is an API in my application that uploads data to aws in byte format, and Android has already been tested.
          – 최신혜
          Nov 22 at 7:55


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404751%2fhow-to-convert-mp3-to-pcm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'