【问题标题】:playing radio in background在后台播放收音机
【发布时间】:2011-07-24 23:10:00
【问题描述】:

我只需要确认 iPhone 应用程序是否可以在后台播放或流式传输广播。

我创建了一个运行良好的收音机应用程序。

我想添加一个功能,用户可以通过它在后台进程中播放收音机,即他们不必保持应用程序打开来播放收音机。

他们可以关闭应用程序,收音机仍在播放。

我很久以前读到,Apple 有一个私有 API,应用程序可以通过它在后台运行收音机,但由于它是私有的,我不能使用它。

有什么建议吗?

【问题讨论】:

  • 我不知道如何实现它,但是是的,您可以在应用程序处于后台时播放音乐。它是 iOS 支持的后台进程之一。

标签: iphone cocoa-touch background-application


【解决方案1】:

iOS SDK 应在 4.0 以上; 首先在您的委托代码中:

- (void)applicationDidEnterBackground:(UIApplication *)application {

    if (isAbove4_0)

 {

        CTCallCenter *_center = [[CTCallCenter alloc] init];

        _center.callEventHandler = ^(CTCall *call) 
       {

            //NSLog(@"call:%@", call.callState);
            if ([call.callState isEqualToString:CTCallStateIncoming]) 

                {

                [RadioPlayer pausePlayer];

        }           
    };
    }

}

那么, 我用的是MPMoviePlayerController,AVPlayer还可以;

if (isAbove4_0) {
    if (_mpmovieplayer == nil) {
        _mpmovieplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
        [_mpmovieplayer setMovieControlMode:MPMovieControlModeHidden];
    }

    [_mpmovieplayer setContentURL:url];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    // This is necessary if you want to play a sequence of songs, otherwise your app will be
    // killed after the first one finishes.
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;

    [_mpmovieplayer play];

    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    if (newTaskId != UIBackgroundTaskInvalid && bgTaskId != UIBackgroundTaskInvalid)
        [[UIApplication sharedApplication] endBackgroundTask: bgTaskId];
    bgTaskId = newTaskId;

}

PS:我的英文很差,希望对你有帮助:)

【讨论】:

    【解决方案2】:

    您可以使用这些音频流媒体之一在后台播放

    https://github.com/mattgallagher/AudioStreamer

    https://github.com/DigitalDJ/AudioStreamer

    它可能会帮助你。

    【讨论】:

      【解决方案3】:

      这对我来说是这样的。

      将以下内容添加到您的 app-info.plist 文件中

      <key>UIBackgroundModes</key>
      <array>
      <string>audio</string>
      </array>
      

      然后在 application:didFinishLaunchingWithOptions 添加以下

      UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
           /* just fail if this happens. */
           NSLog(@"BackgroundTask Expiration Handler is called");
           [application endBackgroundTask:backgroundTask];
      }];
      

      【讨论】:

        猜你喜欢
        • 2017-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        相关资源
        最近更新 更多