我现在正在使用CocosDenshion来获得小音效(半秒长),现在
我需要播放服务器上托管的短30秒音频剪辑(mp3),而不是app资源包内。我正在尝试使用两个“要求”来获取一些代码:
我尝试过使用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。
答案 0 :(得分:1)
在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");
}
});