首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当从内部统一调用“CSCore”时,soundOut.Play崩溃

当从内部统一调用“CSCore”时,soundOut.Play崩溃
EN

Stack Overflow用户
提问于 2016-04-30 23:15:41
回答 1查看 884关注 0票数 0

当试图使用加载在CSCore项目中的Unity3D播放声音时,编辑器将终止。

代码语言:javascript
复制
        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全基类库”目标设置进行了编译。这是完整的脚本:

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-05-01 00:32:01

我建议您做的第一件事是将崩溃的Play()函数放在that块中。

代码语言:javascript
复制
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);
    }
}

如果它崩溃,请查看是否有一条消息打印出来。编辑您的问题并发布错误消息。

现在还尝试在其他线程中调用整个函数,以确保这不是问题所在。让我知道在这两种情况下发生了什么。

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36961263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档