ios iPhone:AVAudioPlayer 不支持的文件类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4901709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:48:02  来源:igfitidea点击:

iPhone: AVAudioPlayer unsupported file type

iphoneiosmp3avaudioplayer

提问by mtmurdock

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). I have looked up how to do this on dozens of sites and they all suggest that i do exactly this:

我的应用程序从我们的服务器下载 mp3 并将其播放给用户。该文件为 64 kbps(如果我理解正确,这完全在 iPhone 的可接受范围内)。我在几十个网站上查过如何做到这一点,他们都建议我这样做:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://.../file.mp3"]];
NSError *e = nil;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error&e];
[player setDelegate:self];

When I run the code, player comes back null and I get this error:

当我运行代码时,播放器返回 null 并出现此错误:

2011-02-04 10:44:46.176 MyApp[6052:207] Error loading audio: Error Domain=NSOSStatusErrorDomain Code=1954115647 "The operation couldn't be completed. (OSStatus error 1954115647.)"
2011-02-04 10:44:49.647 MyApp[6052:207] unsupported file type

I have checked the file and I know that it works. It will play with no problems on Windows Media Player, and Quicktime on mac. I have also uploaded the file to the iPhone emulator and it plays with no problems whatsoever. The file is fine, but for some reason AVAudioPlayer doesn't like it.

我检查了文件,我知道它有效。它可以在 Windows Media Player 和 Mac 上的 Quicktime 上正常播放。我还将文件上传到 iPhone 模拟器,它播放没有任何问题。该文件很好,但由于某种原因 AVAudioPlayer 不喜欢它。

Is there something I need to change? Is there some kind of setting for NSData to specify what kind of file it is? Does anyone have any idea?

有什么我需要改变的吗?NSData 是否有某种设置来指定它是什么类型的文件?有谁有想法吗?

回答by mtmurdock

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

终于我找到了解决这个问题的方法!我没有使用 NSData 对象初始化音频播放器,而是将文件保存到 Documents 文件夹中,然后使用文件 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!

应用程序处理完文件后,可以将其删除。希望他的帮助不仅仅是我!

回答by Don

We were dealing with the same issue - we had to write the file to disk first. What I found eventually was that the data we were downloading had extra, blank bytes at the beginning. When reading from disk using initWithContentsOfURL, AVAudioPlayer knew how to deal with this, but when loading from NSData using initWithData, it did not. Trimming those bytes fixed it.

我们正在处理同样的问题 - 我们必须先将文件写入磁盘。我最终发现我们下载的数据在开始时有额外的空白字节。当使用从磁盘读取时initWithContentsOfURL,AVAudioPlayer 知道如何处理这个,但是当使用从 NSData 加载时initWithData,它没有。修剪这些字节修复了它。

回答by Yankouski Roman

From iOS 7 AVAudioPlayer has new initializer

从 iOS 7 AVAudioPlayer 有新的初始化程序

NSError *error;
[[AVAudioPlayer alloc] initWithData:soundData fileTypeHint:AVFileTypeMPEGLayer3 error:&error]

The supported UTIs

支持的 UTI

NSString *const AVFileType3GPP;
NSString *const AVFileType3GPP2;
NSString *const AVFileTypeAIFC;
NSString *const AVFileTypeAIFF;
NSString *const AVFileTypeAMR;
NSString *const AVFileTypeAC3;
NSString *const AVFileTypeMPEGLayer3;
NSString *const AVFileTypeSunAU;
NSString *const AVFileTypeCoreAudioFormat;
NSString *const AVFileTypeAppleM4V;
NSString *const AVFileTypeMPEG4;
NSString *const AVFileTypeAppleM4A;
NSString *const AVFileTypeQuickTimeMovie;
NSString *const AVFileTypeWAVE;

回答by kordiseps

If your product name contains space, you will receive the error.
My project's product name was: Panorama 1453 TR. initWithContentsOfURLmethod cannot fetch file path. so it was not working. you can put a breakPoint to NSURL *fileURL =. After a step you can see fileURL what data has.
I changed to Panorama1453TR , it did work.

如果您的产品名称包含空格,您将收到错误消息。
我项目的产品名称是:Panorama 1453 TR。initWithContentsOfURL方法无法获取文件路径。所以它不起作用。您可以将断点放置到NSURL *fileURL =. 一步后,您可以看到 fileURL 有哪些数据。
我改为 Panorama1453TR ,它确实有效。