網(wǎng)上很早有人提這個類不能播放出來聲音并闲。這里避坑總結(jié)下真實(shí)原因俗冻。
1.音頻文件后綴類型需要跟文件二禁止頭部的類型相同梨水,initWithContentsOfURL才能成功乏矾,否則init失敗,player初始化為nil,肯定不能播放润匙。
蘋果API注釋如下:
/* all data must be in the form of an audio file understood by CoreAudio */
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError **)outError;
2.可以使用initWithData不用判斷文件類型诗眨,直接讀取和播放二禁止文件。