Search code examples
iphoneobjective-ciosavplayerpcm

how can i play pcm with AVPlayer


I'm using AVPlayer to play local .mp3 file and audio stream from server. And i want to play local .pcm file too.

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
                                                   , NSUserDomainMask 
                                                   , YES); 
NSString * voiceFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"OutPut.pcm"]; 

But it didn't work. i got unknown error.

It seems AudioQueue can play .pcm correctly.

But is there a sample way can let AVPlayer direct play .pcm just like .mp3?


Solution

  • Neither .pcm as a file extension or PCM data specifies a readable format. The player cannot recognize an arbitrary data stream. It is certainly capable of reading file formats which contain PCM data, but this PCM representation is missing several things typical audio file formats represent:

    • Sample Rate
    • Sample Size
    • Sample Format
    • Channel Count

    and so on.

    You should instead save that PCM data in an audio file format the player supports (e.g. a WAV file).

    If you prefer to simply stream PCM audio information and you know the stream format, you can approach that problem using an AudioQueue.