[英]No sound out of an android MIDI driver integrated in Unity3d
我想在Android上使用Unity3d輸出MIDI音頻。 我認為使用開放源代碼庫可以正常工作,但是設備沒有聲音,並且沒有任何錯誤消息/日志。
我正在使用這個庫: https : //github.com/billthefarmer/mididriver
我已經使用Android Studio使用提供的gradle項目測試了庫,並且運行良好。
Unity通過將該庫的AAR文件拖放到Assets \\ Plugins \\ Android中來識別它。 然后,我添加了一個Java類來包裝方法調用,並創建了一個小的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;
}
}
}
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中提取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;
}
}
}
最終在設備上有音頻輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.