iPhone:应用程序在后台时播放音乐
我有一个应用程序,可以使用 Matt Gallagher 的 AudioStreamer 类来流式传输音乐。这作为后台进程工作得很好,除了我希望能够在流完成后跳到下一首歌曲。不幸的是这部分不起作用。 最初,我有一个计时器正在监视流,但意识到当应用程序后台时,该计时器不再运行。因此,我尝试在数据包读取函数中添加委托回调:
void ASReadStreamCallBack(CFReadStreamRef aStream, CFStreamEventType eventType, void* inClientInfo)
{
AudioStreamer* streamer = (AudioStreamer *)inClientInfo;
double percent = [streamer progress]/[streamer duration];
if(percent>=0.98 || (percent>=0.95 && [streamer isIdle])){
if([streamer.delegate respondsToSelector:@selector(didFinishPlayingStream:)] ){
[streamer.delegate didFinishPlayingStream:streamer];
streamer.delegate = nil;
}
}
[streamer handleReadFromStream:aStream eventType:eventType];
}
当应用程序位于前台时,这可以正常工作,但当应用程序处于后台时,这不再起作用。委托方法基本上发送一个请求来获取下一首歌曲的流 URL,然后一旦获得它就会创建一个新的 AudioStreamer 类
I have an app that streams music using AudioStreamer class by Matt Gallagher. This works fine as a background process except I want to be able to skip to the next song once the stream is finished. Unfortunately this part doesn't work.
Initially I had a timer that was monitoring the stream but realized that when the app backgrounds this timer no longer runs. So I tried adding a delegate callback in the packet read function:
void ASReadStreamCallBack(CFReadStreamRef aStream, CFStreamEventType eventType, void* inClientInfo)
{
AudioStreamer* streamer = (AudioStreamer *)inClientInfo;
double percent = [streamer progress]/[streamer duration];
if(percent>=0.98 || (percent>=0.95 && [streamer isIdle])){
if([streamer.delegate respondsToSelector:@selector(didFinishPlayingStream:)] ){
[streamer.delegate didFinishPlayingStream:streamer];
streamer.delegate = nil;
}
}
[streamer handleReadFromStream:aStream eventType:eventType];
}
This works fine when the app is in the foreground but no longer works when the app is backgrounding. The delegate method basically sends a request to get the stream URL for the next song, then once it has it creates a new AudioStreamer class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当应用程序在后台时,您可以实现委托来处理不同的远程控制状态。
像这样的东西对我有用。
While the app is in background you can implemente the delegate to handle the different remote control states.
Something like this works for me.
我已将我的 AudioPlayer/streamer 类上传到,部分灵感来自 Matt Gallagher 的 AudioStreamer
https://code.google.com/p/audjustable。
最酷的功能之一是它支持无缝播放。这意味着 AudioQueue 在间隙之间永远不会关闭;防止 iOS 暂停您的应用程序。
您可以通过调用
AudioPlayer:queueDataSource
实现AudioPlayerDelegate:didFinishBufferingSourceWithQueueItemId
和AudioPlayerDelegate:didFinishPlayingQueueItemId
来排队下一个曲目。如果您需要使用它的帮助,请告诉我。
I've uploaded my AudioPlayer/streamer class inspired in part by Matt Gallagher's AudioStreamer to
https://code.google.com/p/audjustable.
One of the cooler features is its support for gapless playback. This means the AudioQueue is never closed between gaps; keeping iOS from suspending your app.
You can implement
AudioPlayerDelegate:didFinishBufferingSourceWithQueueItemId
andAudioPlayerDelegate:didFinishPlayingQueueItemId
to queue up the next track by callingAudioPlayer:queueDataSource
.Let me know if you need help using it.