当试图使用加载在CSCore项目中的Unity3D播放声音时,编辑器将终止。
IWaveSource audio = CodecFactory.Instance.GetCodec(pathToMP3File);
ISoundOut device = new WasapiOut();
device.Initialize(audio);
device.Play(); // the call causing the crash我选择的输出(WasapiOut、DirectSoundOut、WaveOut)不会改变结果。
CSCore.dll已在VS 2015中通过“Unity3.5 .net全基类库”目标设置进行了编译。这是完整的脚本:
using UnityEngine;
using CSCore;
using CSCore.Codecs;
using CSCore.SoundOut;
using System.Threading;
public class CScorePlayback : MonoBehaviour {
static string testAudio = "C:/Path/to/audio.mp3";
IWaveSource audioSource;
ISoundOut audioDevice;
Thread audioThread;
void Start () {
audioThread = new Thread(PlaySoundTest);
audioThread.Start();
}
void PlaySoundTest()
{
audioSource = CodecFactory.Instance.GetCodec(testAudio);
audioDevice = new WaveOut();
audioDevice.Initialize(audioSource);
try
{
audioDevice.Play();
Debug.Log("Sound Played!");
}
catch (System.Exception e)
{
Debug.Log("Error playing sound: " + e.Message);
}
}
void OnDestroy()
{
if (audioThread != null) audioThread.Join();
if (audioDevice != null)
{
audioDevice.Dispose();
}
if (audioSource != null)
{
audioSource.Dispose();
}
}
}发布于 2016-05-01 00:32:01
我建议您做的第一件事是将崩溃的Play()函数放在that块中。
void Start()
{
IWaveSource audio = CodecFactory.Instance.GetCodec(pathToMP3File);
ISoundOut device = new WasapiOut();
device.Initialize(audio);
try
{
device.Play(); // the call causing the crash
Debug.Log("Sound Played!");
}
catch (System.Exception e)
{
Debug.Log("Error playing sound: " + e.Message);
}
}如果它崩溃,请查看是否有一条消息打印出来。编辑您的问题并发布错误消息。
现在还尝试在其他线程中调用整个函数,以确保这不是问题所在。让我知道在这两种情况下发生了什么。
void Start()
{
playSound();
}
void playSound()
{
new System.Threading.Thread(___playSound)
{
}.Start();
}
void ___playSound()
{
IWaveSource audio = CodecFactory.Instance.GetCodec(pathToMP3File);
ISoundOut device = new WasapiOut();
device.Initialize(audio);
try
{
device.Play(); // the call causing the crash
Debug.Log("Sound Played!");
}
catch (System.Exception e)
{
Debug.Log("Error playing sound: " + e.Message);
}
}https://stackoverflow.com/questions/36961263
复制相似问题