简体   繁体   English

如何在 AVAudioPlayer 中播放歌曲的二进制数据

[英]how to play binarydata of song in AVAudioPlayer

I'm receiving songs url from xml and saving them in sqlite as binary data.我从 xml 接收歌曲 url 并将它们作为二进制数据保存在 sqlite 中。 Now I want to play those songs in AVAudioPlayer.现在我想在 AVAudioPlayer 中播放这些歌曲。

How would I achieve this?我将如何实现这一目标? Is this method is right for playing binarydata of song in AVAudioPlayer.?这种方法适合在 AVAudioPlayer 中播放歌曲的二进制数据吗?

Please have a look at this post:请看一下这个帖子:

AVAudioPlayer with external URL to *.m4p 带有外部 URL 到 *.m4p 的 AVAudioPlayer

AVAudioPlayer only works with local URL's. AVAudioPlayer 仅适用于本地 URL。 It must be a File URL (file://)它必须是文件 URL (file://)

See Apple's Technical Q&A QA1634请参阅Apple 的技术问答 QA1634

You could get your binary data and write it to a file and then play it:您可以获取二进制数据并将其写入文件然后播放:

NSURL *url = [NSURL URLWithString:@"http://a825.phobos.apple.com/us/r2000/005/Music/d8/a8/d2/mzi.jelhjoev.aac.p.m4p"]; 
NSData *soundData = [NSData dataWithContentsOfURL:url];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                NSUserDomainMask, YES) objectAtIndex:0] 
                                                stringByAppendingPathComponent:@"sound.caf"];
[soundData writeToFile:filePath atomically:YES];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
                                fileURLWithPath:filePath] error:NULL];  
NSLog(@"error %@", error);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM