【问题标题】:C# CSCore no sound from mp3 fileC# CSCore 没有来自 mp3 文件的声音
【发布时间】:2018-06-05 20:38:39
【问题描述】:

使用CSCore库,我在一个名为BGM.cs的单独文件中编写了用于播放类BGM中的mp3文件的代码,播放方法为BGM.Play("file directory");,在Form中调用。但不知何故,我无法从中获得任何声音。我已经检查了音量、编解码器和输出,我想不出还有什么可能导致这个问题。

这是类文件的代码:

public class BGM
{
    public static void Play(string file)
    {
        using (IWaveSource soundSource = GetSoundSource(file))
        {
            using (ISoundOut soundOut = GetSoundOut())
            {
                soundOut.Initialize(soundSource);
                soundOut.Volume = 0.8f;
                soundOut.Play();
            }
        }
    }
    private static ISoundOut GetSoundOut()
    {
        if (WasapiOut.IsSupportedOnCurrentPlatform)
            return new WasapiOut();
        else
            return new DirectSoundOut();
    }
    private static IWaveSource GetSoundSource(string file)
    {
        return CodecFactory.Instance.GetCodec(file);
    }

【问题讨论】:

    标签: c# .net cscore


    【解决方案1】:

    实际上有几个原因导致您的 mp3 无法播放。

    第一个原因是您没有指定播放声音的设备。下面的代码获得了第一个可以渲染声音的设备,但如果用户有多个设备连接到他们的计算机,这并不总是正确的。你必须妥善处理。设备必须设置在WasapiOut 对象上。

    第二个原因是您在Play 方法中使用了using 语句。虽然清理实现IDisposable 的对象总是一个好主意,但您不能总是立即这样做。在这种情况下,soundOut.Play() 不是阻塞方法,这意味着控件会立即退出该方法,从而导致在soundOutsoundSource 上调用Dispose()。这意味着声音实际上永远不会被播放(也许它会开始一小会儿,但不足以真正听到它)。从本质上讲,您需要保留引用,并且只有在播放完成后才将其处理掉。

    查看AudioPlayerSample,了解如何实施完整的解决方案。我的代码应该可以帮助您入门。

    void Main()
    {
        using(var player = new BGM(@"D:\Test.mp3"))
        {
            player.Play();
    
            // TODO: Need to wait here in order for playback to complete
            // Otherwise, you need to hold onto the player reference and dispose of it later
            Console.ReadLine();
        }
    }
    
    public class BGM : IDisposable
    {
        private bool _isDisposed = false;
        private ISoundOut _soundOut;
        private IWaveSource _soundSource;
    
        public BGM(string file)
        {
            _soundSource = CodecFactory.Instance.GetCodec(file);
            _soundOut = GetSoundOut();
            _soundOut.Initialize(_soundSource);
        }
    
        public void Play()
        {
            if(_soundOut != null)
            {
                _soundOut.Volume = 0.8f;
                _soundOut.Play();
            }
        }
    
        public void Stop()
        {
            if(_soundOut != null)
            {
                _soundOut.Stop();
            }
        }
    
        private static ISoundOut GetSoundOut()
        {
            if (WasapiOut.IsSupportedOnCurrentPlatform)
            {
                return new WasapiOut
                {
                    Device = GetDevice()
                };
            }
    
            return new DirectSoundOut();
        }
    
        private static IWaveSource GetSoundSource(string file)
        {
            return CodecFactory.Instance.GetCodec(file);
        }
    
        public static MMDevice GetDevice()
        {
            using(var mmdeviceEnumerator = new MMDeviceEnumerator())
            {
                using(var mmdeviceCollection = mmdeviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active))
                {
                    // This uses the first device, but that isn't what you necessarily want
                    return mmdeviceCollection.First();
                }
            }
        }
    
        protected virtual void Dispose(bool disposing)
        {
            if (!_isDisposed)
            {
                if (disposing)
                {
                    if(_soundOut != null)
                    {
                        _soundOut.Dispose();
                    }
    
                    if(_soundSource != null)
                    {
                        _soundSource.Dispose();
                    }
                }
    
                _isDisposed = true;
            }
        }
    
        public void Dispose()
        {
            Dispose(true);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      相关资源
      最近更新 更多