Objective C在下载音频时播放 [英] objective c play audio as it is downloading
问题描述
我有一个有关下载和播放音频文件的iphone设计问题.多亏了Matt Gallagher的AudioStreamer示例,我可以流式传输音频并进行播放,但不将其保存在手机上供以后使用.使用NSURLConnection,我可以下载和保存音频文件,但是在data属性为只读的情况下,我必须等到将它们下载后才能初始化我的AVAudioPlayer.我无法添加到缓冲区.
I have an iphone design question regarding downloading and playing an audio file. Thanks to Matt Gallagher's AudioStreamer sample I can stream audio and play it back, bu that does not save it locally on the phone for later. Using an NSURLConnection I can download and save the audio files, but I have to wait until I have downloaded them to init my AVAudioPlayer given the data property is read only. I can not add to the buffer.
我的问题是如何开始下载,但是在足够"下载之后但尚未完成之后才开始播放文件?除了AVAudioPlayer之外,还有其他音频播放器,可以在下载数据时将其馈入其中.
My question is how can I start the download, but then start playing the file after "enough" of it is downloaded, but before it is finished? Is there another audio player other then AVAudioPlayer that I can feed data into as it is being downloaded.
我现在看到的唯一方法是在不同的线程中同时执行这两个操作,但是当然我要拉两次数据.
The only way I can see to do it now would be to do both in different threads, but then of course I would be pulling down the data twice.
有想法吗?
推荐答案
我将使用AVAudioSession和MPMoviePlayerViewController.
I would use an AVAudioSession along with a MPMoviePlayerViewController.
/**
* Play audio session
*
* @version $Revision: 0.1
*/
- (void)playAudioWithURL:(NSString *)audioURL {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (setCategoryError) {
}
NSError *activationError = nil;
[audioSession setActive:YES error:&activationError];
if (activationError) {
}
MPMoviePlayerViewController *tempPlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:[audioURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
// Make sure we have airplay
if([tempPlayer.moviePlayer respondsToSelector:@selector(setAllowsAirPlay:)]) {
tempPlayer.moviePlayer.allowsAirPlay = YES;
}
tempPlayer.moviePlayer.shouldAutoplay = YES;
tempPlayer.moviePlayer.useApplicationAudioSession = NO;
[self presentMoviePlayerViewControllerAnimated:tempPlayer];
[tempPlayer.moviePlayer play];
[tempPlayer release];
}//end
这篇关于Objective C在下载音频时播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!