How do I programmatically play an MP3 on an iPhone?

后端 未结 4 1512
無奈伤痛
無奈伤痛 2020-12-16 03:30

I\'d like to include an MP3 with my app and play it on demand. Unfortunately I am not sure how to get started. I keep reading about .caf files, but I am not sure what thos

4条回答
  •  星月不相逢
    2020-12-16 03:52

    Use afconvert in Terminal to convert your MP3 to a .caf. man afconvert will tell you more.

    Then

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: name ofType: @"caf"];
    
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
    
    AVAudioPlayer *player = [super initWithContentsOfURL: fileURL error: nil];
    [fileURL release];
    [player play];
    

提交回复
热议问题