title: how to create AVAudioPCMBuffer from NSData
body: <p>I receive mp3 from stream and convert pcm data. I try to use AVAudioEngine but can not create AVAudioPCMBuffer without file.</p>
  
  <pre><code>- (void)playAudio:(NSNotification *)notification {
      self.runAudioThread = YES;
      dispatch_queue_t globalDispatchDefaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
  
      [_player play];
  
      dispatch_async(globalDispatchDefaultQueue, ^{
          while (_runAudioThread) {
              if (_rawDataArray.count &gt; BUFFER_COUNT) {
                  NSMutableData *rawData = [[NSMutableData alloc] init];
                  NSArray *rawDataArray = _rawDataArray.copy;
  
                  @synchronized(_rawDataArray) {
                      [self.rawDataArray removeAllObjects];
                  }
  
                  for (AudioPacketInfo *audioPacketInfo in rawDataArray) {
                      [rawData appendData:audioPacketInfo.data];
                  }
  
                  NSData *mp3Data = [AudioUtil makeMP3Data:rawData];
                  if (mp3Data) {
                      NSData *pcmData = [AudioUtil convertToPCMWithData:mp3Data];
                      if (pcmData.length &gt; 0) {
                          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                          NSString *documentsDirectory = paths[0];
                          NSString *filePath = [documentsDirectory stringByAppendingString:@"/temp.mp3"];
                          [[NSFileManager defaultManager] createFileAtPath:filePath contents:mp3Data attributes:nil];
  
                          AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:[NSURL URLWithString:filePath] error:nil];
                          AVAudioPCMBuffer *buffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:audioFile.processingFormat frameCapacity:(AVAudioFrameCount)audioFile.length];
  
                          [audioFile readIntoBuffer:buffer error:nil];
                          [_player scheduleBuffer:buffer completionHandler:nil];
                      }
                  }
              }
          }
      });
  }
  </code></pre>
  
  <p>how would i do that?</p>
  
  <p><a href="https://gist.github.com/bonggil-jeon/1d8c0ed11e7ee5ce843b" rel="nofollow">Convert mp3 to pcm</a></p>