我目前正在使用 CocosDenshion 来制作小音效(半秒长),现在
我需要播放服务器上托管的 30 秒短音频剪辑 (mp3),而不是在应用程序资源包中。我正在尝试获取一些代码来满足两个“要求”:
- 它在加载时播放,因此我不必等待整个内容加载完毕就可以播放。
- 它将有某种委托(delegate)来显示播放轨道的进度。
我试过使用 AVAudioPlayer,但它对我不起作用,而且它不会“缓冲”数据,它会等待整个加载(如果重要的话,在模拟器上试过)。我尝试的是:
- (IBAction)play:(id)sender{
NSString *_mp3file = @"http://www.somesite.com/somefile.mp3";
NSData *_mp3data = [NSData dataWithContentsOfURL:[NSURL URLWithString: _mp3file]];
NSError *error;
AVAudioPlayer *avp = [[AVAudioPlayer alloc] initWithData:_mp3data error:&error];
avp.numberOfLoops = 0;
avp.volume = 1.0f;
avp.delegate = self;
[avp prepareToPlay];
if(avp == nil)
GTMLoggerError(@"%@", error);
else
[avp play];
}
希望得到您的帮助:)
干杯,
Shai。
最佳答案
在你的 didLoad 方法中
dispatch_queue_t dispatchQueue =
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(dispatchQueue, ^(void){
NSString *_mp3file = @"http://cs4405.vk.me/u360087/audios/4efe178f2608.mp3";
NSData *_mp3Data = [NSData dataWithContentsOfURL:[NSURL URLWithString: _mp3file]];
NSError *_error = nil;
self._audioPlayer = [[AVAudioPlayer alloc] initWithData:_mp3Data error:&_error];
if (self._audioPlayer != nil)
{
self._audioPlayer.delegate = self;
if ([self._audioPlayer prepareToPlay] && [self._audioPlayer play])
{
NSLog(@"Successfully started playing");
} else {
NSLog(@"Failed to play");
}
} else {
NSLog(@"Failed to instanciate player");
}
});
关于iphone - 使用缓冲/流媒体 block 播放远程 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8160271/