在后台播放广播

发布于 2024-10-28 05:09:03 字数 248 浏览 6 评论 0原文

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

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

我想添加一项功能,用户可以通过该功能在后台进程中播放广播,即他们不必保持应用程序打开即可播放广播。

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

我很早以前就读到,Apple 有一个私有 API,应用程序可以通过该 API 在后台运行无线电,但由于它是私有的,所以我无法使用它。

有什么建议吗?

I just need to confirm if an iPhone app can play or stream radio in the background.

I created a radio app which is working fine.

I want to add a feature by which the user can play radio in a background process, i.e. they don't have to keep the application open to play the radio.

They can close the app, and the radio is still playing.

I read a long time back that Apple has a private API by which an app can run radio in the background, but since it is private, I can't use it.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一口甜 2024-11-04 05:09:03

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:我的英语很差,我希望这对你有帮助:)

iOS SDK should be above 4.0;
first in your delegate codes:

- (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];

        }           
    };
    }

}

then,
I used MPMoviePlayerController, and AVPlayer is OK;

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:my english is poor, I hope this could be helpful to you :)

荒路情人 2024-11-04 05:09:03

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

https://github.com/mattgallagher/AudioStreamer

https://github.com/DigitalDJ/AudioStreamer

它可能对您有帮助。

You can use one of these audio streamer for play in background

https://github.com/mattgallagher/AudioStreamer

https://github.com/DigitalDJ/AudioStreamer

it might help you.

那小子欠揍 2024-11-04 05:09:03

这对我来说就是这样的。

将以下内容添加到 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];
}];

This is how it works for me.

Add following to you app-info.plist file

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

Then in application:didFinishLaunchingWithOptions add following

UIBackgroundTaskIdentifier backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{
     /* just fail if this happens. */
     NSLog(@"BackgroundTask Expiration Handler is called");
     [application endBackgroundTask:backgroundTask];
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文