iPhone: AVAudioPlayer unsupported file type

后端 未结 4 1500
一整个雨季
一整个雨季 2020-11-30 03:00

My app downloads an mp3 from our server and plays it back to the user. The file is 64 kbps (which is well within the acceptable range for iPhone if I understand correctly).

4条回答
  •  遥遥无期
    2020-11-30 04:00

    At long last i have found a solution to this problem! Instead of initializing the audio player with the NSData object, I saved the file to the Documents folder, and then initialized the player with the file URL

    //download file and play from disk
    NSData *audioData = [NSData dataWithContentsOfURL:someURL];
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", docDirPath , fileName];
    [audioData writeToFile:filePath atomically:YES];
    
    NSError *error;
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
    if (player == nil) {
        NSLog(@"AudioPlayer did not load properly: %@", [error description]);
    } else {
        [player play];
    }
    

    When the app is done with the file, it can be deleted. Hope that his helps more than just me!

提交回复
热议问题