使用缓冲/流媒体块播放远程 mp3 文件
我目前正在使用 CocosDenshion 来实现小声音效果(半秒长),现在 我需要播放托管在服务器上而不是应用程序资源包内的 30 秒短音频剪辑 (mp3)。我正在尝试获取一些代码来播放它,并满足两个“要求”:
- 它在加载时播放,因此我不必等待整个内容加载即可播放。
- 它将有某种委托来显示播放曲目的进度。
我尝试过使用 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。
I'm currently using CocosDenshion for small sound effects (half a second long), And now
I need to play short 30-second audio clips (mp3) that are hosted on a server, not inside the app resource bundle. I'm trying to get some code to play it with two "requirements":
- that it plays as it loads, so i wouldn't have to wait for the entire thing to load just to play it.
- That it would have some sort of delegate to show the progress of the played track.
I've tried using AVAudioPlayer but it doesnt work for me, plus it doesn't "Buffer" the data, it waits for the entire thing to load (Tried on simulator, if it matters). What i tried is:
- (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];
}
Would love your help an experience on this :)
Cheers,
Shai.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的 didLoad 方法中
In your didLoad method