java - Unity3d 中集成的 Android MIDI 驱动程序没有声音

标签 java c# android unity-game-engine midi

我想在 Android 上使用 Unity3d 输出 MIDI 音频。使用开源库,我认为我可以正常工作,但设备没有发出声音,并且没有任何类型的错误消息/日志。

我正在使用这个库: https://github.com/billthefarmer/mididriver

我已经使用 Android Studio 使用提供的 gradle 项目测试了该库,并且运行良好。

Unity 通过将其放入 Assets\Plugins\Android 中来识别该库的 AAR 文件。然后我添加了一个java类来包装方法调用, 并创建了一个小的 C# MonoBehaviour 脚本来测试它。我隔离了代码,以便您可以重现问题。

重现:

  1. 使用 Unity3d 创建一个新的 3d 项目。
  2. 添加一个空的游戏对象。
  3. 将以下 C# 脚本附加到其中。
  4. 在 Assets 文件夹中创建文件夹 Plugins\Android。
  5. 在 Android 文件夹中创建一个新文件,其中包含以下 Java 代码。
  6. 添加最新版本的链接库的 AAR 文件。
  7. 构建并将其加载到 Android 设备。

C# MonoBehaviour

每 2 秒发送一条 NOTE_ON 消息,然后在一秒后发送一条 NOTE_OFF 消息。 AndroidJavaClass 用于调用库的方法。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MidiDriverDemo : MonoBehaviour
{
    private const string CLASS_PATH = "com.example.driver.AndroidMidiDriver";

    public static readonly float CYCLE_TIME_SEC = 1f;

    private float remainingTime;

    private Dictionary<int, int[]> messages;
    private int currentMessage;

    AndroidJavaClass midiDriver;

    // Start is called before the first frame update
    void Start()
    {
        remainingTime = CYCLE_TIME_SEC;
        currentMessage = 0;
        messages = new Dictionary<int, int[]>() {
            {  0, new int[] { 0x90, 48, 63 } },
            {  1, new int[] { 0x80, 48, 0 } }
        };

        midiDriver = new AndroidJavaClass(CLASS_PATH);
        midiDriver.Call("start");
    }

    // Update is called once per frame
    void Update()
    {
        remainingTime -= Time.deltaTime;
        if(remainingTime <= 0) {
            remainingTime = CYCLE_TIME_SEC;

            midiDriver.Call("send", messages[currentMessage][0],
                messages[currentMessage][1], messages[currentMessage][2]);

            currentMessage++;
            currentMessage %= messages.Count;
        }
    }
}

java 包装器

package com.example.driver;

import org.billthefarmer.mididriver.MidiDriver;

public class AndroidMidiDriver {

    private MidiDriver midi;

    public AndroidMidiDriver(){
        midi = new MidiDriver();
    }

    public void start(){
        if (midi != null) {
            midi.start();
        }
    }

    public void stop() {
        if (midi != null) {
            midi.stop();
        }
    }

    public void send(int command, int noteNumber, int velocity){
        byte[] msg = new byte[3];

        msg[0] = (byte) command;
        msg[1] = (byte) noteNumber;
        msg[2] = (byte) velocity;

        midi.write(msg);
    }
}

因此它可以完美编译,没有任何警告,并且可以在设备上运行,但仍然没有声音。声级设置正确。

在我隔离问题之前,对“发送”方法的调用因段错误而导致我的应用程序崩溃,所以我猜有些东西正在工作。

所以我认为问题在于:

  • 这不是访问 AAR 的正确方式。
  • 我错过了 Unity 中的一些设置。
  • 问题出在库本身,不太可能,因为它与提供的 gradle 项目独立工作。

欢迎任何想法/建议。

最佳答案

从.aar中提取jni/armv7a文件夹中的libmidi.so后,删除java包装器并将代码更改为以下内容:

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class MidiDriverDemo : MonoBehaviour
{
    public static readonly float CYCLE_TIME_SEC = 1f;

    private float remainingTime;

    private Dictionary<int, byte[]> messages;
    private int currentMessage;

    [DllImport("midi")]
    private static extern float midi_init();

    [DllImport("midi")]
    private static extern float midi_shutdown();

    [DllImport("midi")]
    private static extern float midi_write(byte[] msg, int length);

    // Start is called before the first frame update
    void Start()
    {
        remainingTime = CYCLE_TIME_SEC;
        currentMessage = 0;
        messages = new Dictionary<int, byte[]>() {
            {  0, new byte[] { 0x90, 48, 100 } },
            {  1, new byte[] { 0x80, 48, 0 } }
        };

        midi_init();
    }

    // Update is called once per frame
    void Update()
    {
        remainingTime -= Time.deltaTime;
        if(remainingTime <= 0) {
            remainingTime = CYCLE_TIME_SEC;

            midi_write(messages[currentMessage], 3);

            currentMessage++;
            currentMessage %= messages.Count;
        }
    }
}

设备上终于有音频输出了。

关于java - Unity3d 中集成的 Android MIDI 驱动程序没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57502815/

相关文章:

java - JPA-Hibernate-从 Select 语句生成的 ID 值

java - 将大文件上传到 Amazon S3 时出现问题

c# - 是否可以通过关联文件将命令行参数传递给可执行文件?

android - 如何在圆形 ImageView 中设置位图?

java - Espresso : Ui Watcher

java - MainActivity 类型中的 onCreate(Bundle) 方法重复吗?

java - 我如何按所有字段进行 Elasticsearch (Spring)?

java - 在 Java 中 fork 一个进程

c# - 如何使用C#验证Powershell .ps1脚本的签名?

c# - 找到超速的时期?