简体   繁体   English

Gluon 移动视频服务在后台无法在 iphone 上运行

[英]Gluon Mobile VideoService doesn't work on iphone in background

I'm using VideoService to playback a local audio file (mp3) in my application, and it works fine while the application is active, both on Android and iPhone.我正在使用 VideoService 在我的应用程序中播放本地音频文件 (mp3),当应用程序处于活动状态时,它在 Android 和 iPhone 上都可以正常工作。 But on iPhone, when the application is in background it doesn't work: nothing happens when service.play() is called.但是在 iPhone 上,当应用程序在后台时它不起作用:调用 service.play() 时没有任何反应。 The code is trivial:代码很简单:

            Services.get(VideoService.class).ifPresent(service -> {
                service.getPlaylist().add("1.mp3");
                service.play();
            });

I can see "AVPlayer hidden" and "AVPlayerStatusReadyToPlay" in my IDEA's console.我可以在 IDEA 的控制台中看到“AVPlayer hidden”和“AVPlayerStatusReadyToPlay”。

If the playback is already started and I put my application in background (using iPhone's "home" button or by turning off the screen) - it stops playing and resumes only after I bring the application back to active state manually.如果播放已经开始并且我将我的应用程序置于后台(使用 iPhone 的“主页”按钮或通过关闭屏幕) - 它停止播放并仅在我手动将应用程序恢复到活动状态后恢复。

JavaDocs say no specific iOS configuration is required, though I put "audio" in plist's UIBackgroundModes array (doesn't help either). JavaDocs 说不需要特定的 iOS 配置,尽管我将“音频”放在 plist 的 UIBackgroundModes 数组中(也没有帮助)。

iPhone 6, iOS 12.1 (16B92) iPhone 6,iOS 12.1 (16B92)

On Android the same code works just fine both in active and background modes without any problems.在 Android 上,相同的代码在活动和后台模式下都可以正常工作,没有任何问题。

What am I missing?我错过了什么?

To be able to play audio in background, and based on this answer , it seems that the current Charm Down Video Service requires some modifications, in order to set the category to AVAudioSessionCategory.Playback .为了能够在后台播放音频,并基于此答案,似乎当前的 Charm Down Video Service 需要进行一些修改,以便将类别设置为AVAudioSessionCategory.Playback

One possible way to do this, is by modifying applicationDidFinishLaunching , from the iOS Launcher class.一种可能的方法是从 iOS Launcher 类修改applicationDidFinishLaunching The jfxmobile plugin 1.3.16 creates this launcher here . jfxmobile插件 1.3.16 在这里创建了这个启动器。

So we could modify that class, and build a custom version of the jfxmobile plugin, or, as the OP mentioned in the comments, it is possible to create a custom launcher.所以我们可以修改那个类,并构建一个自定义版本的jfxmobile插件,或者,正如评论中提到的 OP,可以创建一个自定义启动器。

The other possible way is adding this to the Charm Down video service directly, but it will require compiling a new version.另一种可能的方法是将其直接添加到 Charm Down 视频服务中,但这需要编译一个新版本。

Let's try the custom launcher as it won't require building new versions.让我们尝试自定义启动器,因为它不需要构建新版本。

Creating a CustomLauncher创建自定义启动器

Let's copy the default launcher into our project, into the src/ios/java folder, as it requires some specific dependencies for iOS.让我们将默认启动器复制到我们的项目中,到src/ios/java文件夹中,因为它需要一些特定的 iOS 依赖项。

Then add the required code to set the Playback option, starting from the main class:然后添加所需的代码来设置Playback选项,从主类开始:

private static final Class<? extends Application> mainClass = your.package.YourGluonApplication.class;

private static final Class<? extends Preloader> preloaderClass = null;

@Override
public boolean didFinishLaunching(UIApplication application, 
    UIApplicationLaunchOptions launchOptions) {

    // Audio settings to play in background mode ---
    try {
        AVAudioSession session = AVAudioSession.getSharedInstance();
        session.setActive(true);
        session.setCategory(AVAudioSessionCategory.Playback);
    } catch (NSErrorException nse) {
        System.out.println("Error AVAudioSession " + nse);
        nse.printStackTrace();
    }
    // --- End Audio settings

    Thread launchThread = new Thread() { ... }
    launchThread.setDaemon(true);
    launchThread.start();

    return true;
}

Using the custom launcher使用自定义启动器

As commented in the launcher class, the custom launcher can be loaded from the build.gradle file:正如启动器类中所评论的,自定义启动器可以从build.gradle文件加载:

jfxmobile {
    downConfig {
        version = '3.8.6'
        plugins 'display', 'lifecycle', 'statusbar', 'storage', 'video'
    }
    ios {
        javafxportsVersion = '8.60.11'
        launcherClassName = 'your.package.CustomLauncher'
        infoPList = file('src/ios/Default-Info.plist')
        ...
    }
}

Allow background audio允许背景音频

The last required step to get audio playing not only when the app is running on the foreground but also when it goes to the background: modify the plist file.不仅在应用程序在前台运行时而且在它进入后台时播放音频的最后一个必需步骤:修改 plist 文件。

We need to add this key to the Default-info.plist file:我们需要将此键添加到Default-info.plist文件中:

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

Test测试

Let's add an mp3 file to src/main/resources/ , like 1.mp3 , and include this call in our Java code:让我们向src/main/resources/添加一个 mp3 文件,比如1.mp3 ,并在我们的 Java 代码中包含这个调用:

Services.get(VideoService.class).ifPresent(service -> {
            service.getPlaylist().add("1.mp3");
            service.play();
        });

Time to deploy to an iOS device:部署到 iOS 设备的时间:

./gradlew launchIOSDevice

The app should play the audio in both foreground and background mode as expected.应用程序应按预期在前台和后台模式下播放音频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM