Additionally, seen the related post about player modifiable audio here and followed:
Followed that to produce this code. I have “sound1.wav” in Assets/StreamingAssets. This code produces the error:
[FMOD] SoundSourceInstrumentInstance::startChannelIfReady : Waveform instance in error state 18
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using UnityEngine;
public class eatme : MonoBehaviour {
FMOD.Studio.EVENT_CALLBACK soundCallback;
public FMODUnity.EventReference programmerSoundEventRef;
// Start is called before the first frame update
void Start() {
soundCallback = new FMOD.Studio.EVENT_CALLBACK(PlayFileCallBack);
}
[AOT.MonoPInvokeCallback(typeof(FMOD.Studio.EVENT_CALLBACK))]
static FMOD.RESULT PlayFileCallBack(FMOD.Studio.EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr) {
FMOD.Studio.EventInstance instance = new FMOD.Studio.EventInstance(instancePtr);
if (instance.isValid() == false) {
UnityEngine.Debug.Log("Failed to create instance, instance invalid");
return FMOD.RESULT.ERR_EVENT_NOTFOUND;
}
//Retrive the user data
IntPtr stringPtr;
instance.getUserData(out stringPtr);
// Retrieving the path of the audio source from the user data
GCHandle stringHandle = GCHandle.FromIntPtr(stringPtr);
String location = stringHandle.Target as String;
switch (type) {
case FMOD.Studio.EVENT_CALLBACK_TYPE.CREATE_PROGRAMMER_SOUND: {
FMOD.MODE soundMode = FMOD.MODE.LOOP_NORMAL | FMOD.MODE.CREATECOMPRESSEDSAMPLE | FMOD.MODE.NONBLOCKING;
var parameter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));
FMOD.Sound audioSound;
FMOD.RESULT result;
// Creating the sound using the location of the audio source
result = FMODUnity.RuntimeManager.CoreSystem.createSound(location, soundMode, out audioSound);
if (result == FMOD.RESULT.OK) {
parameter.sound = audioSound.handle;
parameter.subsoundIndex = -1;
Marshal.StructureToPtr(parameter, parameterPtr, false);
}
break;
}
case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROY_PROGRAMMER_SOUND: {
var paramerter = (FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES)Marshal.PtrToStructure(parameterPtr, typeof(FMOD.Studio.PROGRAMMER_SOUND_PROPERTIES));
var sound = new FMOD.Sound(paramerter.sound);
sound.release();
break;
}
case FMOD.Studio.EVENT_CALLBACK_TYPE.DESTROYED: {
// Now the event has been destroyed, unpin the string memory so it can be garbaged collected
stringHandle.Free();
break;
}
}
return FMOD.RESULT.OK;
}
public void PlayAudioFile(string location) {
var eventInstance = FMODUnity.RuntimeManager.CreateInstance(programmerSoundEventRef);
// FMOD.RESULT is a very useful debugging tool to ensure that FMOD functions have run properly
FMOD.RESULT result;
if (eventInstance.isValid() == false) {
Debug.Log("Failed to create instance, instance invalid");
return;
}
// Lock the file location in memory to be used in the CALLBACK function to find the audio source
GCHandle stringLocation = GCHandle.Alloc(location);
// Assigning this data to the created instance
result = eventInstance.setUserData(GCHandle.ToIntPtr(stringLocation));
// Using an if statement like this after most FMOD function calls will help debug errors in code
if (result != FMOD.RESULT.OK) {
Debug.Log("Failed to create evenInstance with result of " + result);
return;
}
// Check the result after each function call
result = eventInstance.setCallback(soundCallback);
result = eventInstance.start();
result = eventInstance.release();
// The sound has successfully been created and played
}
void Update() {
if (Input.GetKeyDown(KeyCode.Alpha1)) {
PlayAudioFile("sound1.wav");
}
}
}