Gluon移动iOS音频播放器 [英] Gluon Mobile iOS Audio Player

查看:137
本文介绍了Gluon移动iOS音频播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于JavaFx Media尚未移植到移动平台,任何人都可以帮我使用原生iOS APi播放声音mp3文件,该文件将存储在我的胶子项目的main / resources文件夹中。

Since the JavaFx Media has not been ported to the Mobile Platforms yet, can anyone help me with using the native iOS APi's to play a sound mp3 file that would be stored in my main/resources folder in my gluon project.

推荐答案

在Android上,我们可以轻松地将原生API添加到Gluon项目的Android文件夹中(请检查解决方案,在Android上使用本机媒体),使用本机代码(Objetive- C)iOS文件夹上的Media API是不够的,因为它必须被编译,并且编译的文件必须包含在ipa中。

While on Android we can easily add native API to the Android folders of a Gluon project (check this solution for using native Media on Android), the use of native code (Objetive-C) with the Media API on iOS folders is not enough, since it has to be compiled, and the compiled files have to be included into the ipa.

目前,< a href =https://bitbucket.org/gluon-oss/charm-down =nofollow noreferrer> Charm Down 正在为一堆服务做这件事。如果你看一下 build.gradle 脚本ios / build.gradle?at = default& fileviewer = file-view-defaultrel =nofollow noreferrer> iOS ,它包含 xcodebuild 任务编译并构建本机库 libCharm.a ,以后应该包含在使用任何这些服务的任何iOS项目中。

Currently, Charm Down is doing this for a bunch of services. If you have a look at the build.gradle script for iOS, it includes the xcodebuild task to compile and build the native library libCharm.a, that later should be included in any iOS project using any of those services.

我的建议是克隆Charm Down,并提供一项新服务: AudioService ,其中包括一些基本方法:

My suggestion will be cloning Charm Down, and providing a new service: AudioService, with some basic methods like:

public interface AudioService {
    void play(String audioName);
    void pause();
    void resume();
    void stop(); 
}

此服务将添加到平台 class:

This service will be added to the Platform class:

public abstract class Platform {
    ...
    public abstract AudioService getAudioService();
}

你应该提供桌面(空),Android的实现(就像一个此处)和iOS。

and you should provide implementations for Desktop (empty), Android (like the one here) and iOS.

IOS实施 - Java

您必须将此添加到 IOSPlatform class:

You will have to add this to the IOSPlatform class:

public class IOSPlatform extends Platform {
    ...
    @Override
    public AudioService getAudioService() {
        if (audioService == null) {
            audioService = new IOSAudioService();
        }
        return audioService;
    }
}

并创建 IOSAudioService class:

public class IOSAudioService implements AudioService {

    @Override
    public void play(String audioName) {
        CharmApplication.play(audioName);
    }

    @Override
    public void pause() {
        CharmApplication.pause();
    }

    @Override
    public void resume() {
        CharmApplication.resume();
    }

    @Override
    public void stop() {
        CharmApplication.stop();
    }
}

最后,您必须添加原生调用 CharmApplication

Finally, you will have to add the native calls in CharmApplication:

public class CharmApplication {
    static {
        System.loadLibrary("Charm");
        _initIDs();
    }
    ...
    public static native void play(String audioName);
    public static native void pause();
    public static native void resume();
    public static native void stop();
}

IOS实施 - 目标-C

现在,在本机文件夹的 CharmApplication.m 上,添加这些调用的实现:

Now, on the native folder, on CharmApplication.m, add the implementation of those calls:

#include "CharmApplication.h"
...
#include "Audio.h"

// Audio
Audio *_audio;

JNIEXPORT void JNICALL Java_com_gluonhq_charm_down_ios_CharmApplication_play
(JNIEnv *env, jclass jClass, jstring jTitle)
{
    NSLog(@"Play audio");
    const jchar *charsTitle = (*env)->GetStringChars(env, jTitle, NULL);
    NSString *name = [NSString stringWithCharacters:(UniChar *)charsTitle length:(*env)->GetStringLength(env, jTitle)];
    (*env)->ReleaseStringChars(env, jTitle, charsTitle);

    _audio = [[Audio alloc] init];
    [_audio playAudio:name];
    return;
}

JNIEXPORT void JNICALL Java_com_gluonhq_charm_down_ios_CharmApplication_pause
(JNIEnv *env, jclass jClass)
{
    if (_audio) 
    {
        [_audio pauseAudio];
    }
    return;   
}

JNIEXPORT void JNICALL Java_com_gluonhq_charm_down_ios_CharmApplication_resume
(JNIEnv *env, jclass jClass)
{
    if (_audio) 
    {
        [_audio resumeAudio];
    }
    return;   
}

JNIEXPORT void JNICALL Java_com_gluonhq_charm_down_ios_CharmApplication_stop
(JNIEnv *env, jclass jClass)
{
    if (_audio) 
    {
        [_audio stopAudio];
    }
    return;   
}

并创建头文件 Audio.h

#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>

@interface Audio :UIViewController <AVAudioPlayerDelegate>
{
}
    - (void) playAudio: (NSString *) audioName;
    - (void) pauseAudio;
    - (void) resumeAudio;
    - (void) stopAudio;
@end

和实施 Audio.m 。这个是基于这个教程

and the implementation Audio.m. This one is based on this tutorial:

#include "Audio.h"
#include "CharmApplication.h"

@implementation Audio 

AVAudioPlayer* player;

- (void)playAudio:(NSString *) audioName 
{
    NSString* fileName = [audioName stringByDeletingPathExtension];
    NSString* extension = [audioName pathExtension];

    NSURL* url = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"%@",fileName] withExtension:[NSString stringWithFormat:@"%@",extension]];
    NSError* error = nil;

    if(player)
    {
        [player stop];
        player = nil;
    }

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    if(!player)
    {
        NSLog(@"Error creating player: %@", error);
        return;
    }
    player.delegate = self;
    [player prepareToPlay];
    [player play];

}

- (void)pauseAudio
{
    if(!player)
    {
        return;
    }
    [player pause];
}

- (void)resumeAudio
{
    if(!player)
    {
        return;
    }
    [player play];
}

- (void)stopAudio
{
    if(!player)
    {
        return;
    }
    [player stop];
    player = nil;
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    NSLog(@"%s successfully=%@", __PRETTY_FUNCTION__, flag ? @"YES"  : @"NO");
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    NSLog(@"%s error=%@", __PRETTY_FUNCTION__, error);
}

@end

构建原生库

编辑iOS模块的 build.gradle ,并将音频服务添加到 xcodebuild 任务:

Edit the build.gradle for the iOS module, and add the Audio service to the xcodebuild task:

def nativeSources = ["$project.projectDir/src/main/native/CharmApplication.m",
                     ...,
                     "$project.projectDir/src/main/native/Audio.m"]

...
def compileOutputs = [
                "$project.buildDir/native/$arch/CharmApplication.o",
                "$project.buildDir/native/$arch/Charm.o",
                 ...,
                "$project.buildDir/native/$arch/Audio.o"]

构建项目

保存项目,并从Charm Down项目的根目录运行命令:

Save the project, and from the root of the Charm Down project, on command line run:

./gradlew clean build

如果一切都很好,你就开始了ld有:

If everything is fine, you should have:


  • Common / build / libs / charm-down-common-2.1.0-SNAPSHOT.jar

  • Desktop / build / libs / charm-down-desktop-2.1.0-SNAPSHOT.jar

  • Android / build / libs / charm-down-android-2.1.0 -SNAPSHOT.jar

  • IOS / build / libs / charm-down-ios-2.1.0-SNAPSHOT.jar

  • IOS / build / native /libCharm.a

  • Common/build/libs/charm-down-common-2.1.0-SNAPSHOT.jar
  • Desktop/build/libs/charm-down-desktop-2.1.0-SNAPSHOT.jar
  • Android/build/libs/charm-down-android-2.1.0-SNAPSHOT.jar
  • IOS/build/libs/charm-down-ios-2.1.0-SNAPSHOT.jar
  • IOS/build/native/libCharm.a

胶子项目

使用这些依赖项和本机库,您将能够创建一个新的Gluon项目,并将jar作为本地依赖项添加到 libs 文件夹中。

With those dependencies and the native library, you will be able to create a new Gluon Project, and add the jars as local dependencies (to the libs folder).

对于本机库,应将其添加到此路径: src / ios / jniLibs / libCharm.a

As for the native library, it should be added to this path: src/ios/jniLibs/libCharm.a.

更新 build.gradle 脚本:

repositories {
    flatDir {
       dirs 'libs'
   }
    jcenter()
    ...
}

dependencies {
    compile 'com.gluonhq:charm-glisten:3.0.0'
    compile 'com.gluonhq:charm-down-common:2.1.0-SNAPSHOT'
    compile 'com.gluonhq:charm-glisten-connect-view:3.0.0'

    iosRuntime 'com.gluonhq:charm-glisten-ios:3.0.0'
    iosRuntime 'com.gluonhq:charm-down-ios:2.1.0-SNAPSHOT'
}

在您的View上,检索服务并提供一些基本UI来调用 play(audio.mp3) pause() resume() stop() 方法:

On your View, retrieve the service and provide some basic UI to call the play("audio.mp3"), pause(), resume() and stop() methods:

private boolean pause;

public BasicView(String name) {
    super(name);

    AudioService audioService = PlatformFactory.getPlatform().getAudioService();
    final HBox hBox = new HBox(10, 
            MaterialDesignIcon.PLAY_ARROW.button(e -> audioService.play("audio.mp3")),
            MaterialDesignIcon.PAUSE.button(e -> {
                if (!pause) {
                    audioService.pause();
                    pause = true;
                } else {
                    audioService.resume();
                    pause = false;
                }

            }),
            MaterialDesignIcon.STOP.button(e -> audioService.stop()));
    hBox.setAlignment(Pos.CENTER);
    setCenter(new StackPane(hBox));
}

最后,放置一个音频文件,如 audio.mp3 ,在 src / ios / assets / audio.mp3 下,构建并部署到iOS。

Finally, place an audio file like audio.mp3, under src/ios/assets/audio.mp3, build and deploy to iOS.

希望这个API将很快由Charm Down提供。此外,如果您想出一个很好的实现,请随时分享它并创建一个拉取请求

Hopefully, this API will be provided by Charm Down any time soon. Also if you come up with a nice implementation, feel free to share it and create a Pull Request.

这篇关于Gluon移动iOS音频播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆