iPhone:应用程序在后台时播放音乐

发布于 2024-11-06 23:33:38 字数 860 浏览 7 评论 0原文

我有一个应用程序,可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意犹 2024-11-13 23:33:38

当应用程序在后台时,您可以实现委托来处理不同的远程控制状态。

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent { 
switch (receivedEvent.subtype) {
    case UIEventSubtypeRemoteControlTogglePlayPause:
        if (player.isPlaying) {
            [player pause];
        } else {
            [player start];
        }
        break;

    case UIEventSubtypeRemoteControlPreviousTrack:
        break;

    case UIEventSubtypeRemoteControlNextTrack:
        [self skipSong:nil];
        break;

    default:
        break;
} }

像这样的东西对我有用。

While the app is in background you can implemente the delegate to handle the different remote control states.

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent { 
switch (receivedEvent.subtype) {
    case UIEventSubtypeRemoteControlTogglePlayPause:
        if (player.isPlaying) {
            [player pause];
        } else {
            [player start];
        }
        break;

    case UIEventSubtypeRemoteControlPreviousTrack:
        break;

    case UIEventSubtypeRemoteControlNextTrack:
        [self skipSong:nil];
        break;

    default:
        break;
} }

Something like this works for me.

少女的英雄梦 2024-11-13 23:33:38

我已将我的 AudioPlayer/streamer 类上传到,部分灵感来自 Matt Gallagher 的 AudioStreamer
https://code.google.com/p/audjustable

最酷的功能之一是它支持无缝播放。这意味着 AudioQueue 在间隙之间永远不会关闭;防止 iOS 暂停您的应用程序。

您可以通过调用 AudioPlayer:queueDataSource 实现 AudioPlayerDelegate:didFinishBufferingSourceWithQueueItemIdAudioPlayerDelegate: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 and AudioPlayerDelegate:didFinishPlayingQueueItemId to queue up the next track by calling AudioPlayer:queueDataSource.

Let me know if you need help using it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文