Getting an AVAudioFile from an AVAudioSession

I would like to play some songs coming from the iTunes Libray in the form of MPMediaItem by changing their pitch. I already got AVAudioSessions from them and I found an assorted list of references explaing how to how to change the pitch of an AVAudioFile:

http://mhorga.org/2015/07/09/audio-processing-with-avfoundation.html

https://github.com/arielelkin/SwiftyAudio

https://swiftios8dev.wordpress.com/2015/03/05/sound-effects-using-avaudioengine/

http://stackoverflow.com/questions/25333140/using-sound-effects-with-audioengine


Yet I found no information about how to get an AVAudioFile from the AVAudioSessions I have. Have you got any idea about how to do it?

Replies

AVAudioSession and AVAudioFile are unrelated objects


If you have an MPMediaItem look at MPMediaItemPropertyAssetURL. The returned NSURL object is a URL pointing to the media item which is then used to create an AVAsset or other URL-based AV Foundation object - in this case you want an AVAudioFile which you can create with initForReading:error:. Note that the URL can be nil.


AVAudioFile.h

/! @method initForReading:error:
  @abstract Open a file for reading.
  @param fileURL
  the file to open
  @param outError
  on exit, if an error occurs, a description of the error
  @discussion
  This opens the file for reading using the standard format (deinterleaved floating point).
*/
- (nullable instancetype)initForReading:(NSURL *)fileURL error:(NSError **)outError;