iOS Objective-C / Swift下载时读取文件
我正在下载播放音频文件,是否可能在iOS中?
I'm trying to play an audio file while it's downloading, is that possible in iOS?
我知道我可以获取文件的位置after通过以下方式下载:
I know I can get the location of the file "after" it's downloaded through:
(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
但是,当我们还在下载的时候,我需要阅读/播放。
But I need to read/play it while it's still downloading.
更新
我原来的问题是:
我需要在右耳机或左耳机上播放流式音频文件。 AVAudioPlayer中已经有方法setPan,但AVAudioPlayer不能与流数据完美配合。另一方面,AVPlayer使用流数据完美,但不允许setPan。
Update My original problem is: I need to play a streamable audio file in either right or left headphones. There's already method setPan in AVAudioPlayer, but AVAudioPlayer does not work perfectly with streaming data. On the other hand, AVPlayer works perfect with streaming data, but it does not allow to "setPan".
我的想法是开始下载音频文件并传递给AVAudioPlayer并调用setPan,但在下载文件时无法访问该文件。而且我仍然不确定AVAudioPlayer是否会在播放文件时附加添加到文件中的新下载的数据。
My idea was to start downloading the audio file and pass it to AVAudioPlayer and call "setPan", but i can't access the file while it's being downloaded. And I'm still not sure if AVAudioPlayer will append the new downloaded data that is added to the file, while the file is playing.
直接使用
AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://my.url.com/my.mp3"]];
[anAudioStreamer play];