使用C#,wave标头已损坏

时间:2015-07-17 14:54:49

标签: c# media soundplayer

我想在C#中播放一个Wav文件 我的一些文件出现错误" wave标头已损坏。" 我设置了Position = 0,它仍然会发生。

   using (System.Media.SoundPlayer sound = new System.Media.SoundPlayer(soundStream))
            {
                sound.Stream.Position = 0;
                sound.Play();

            }

如果我将Stream保存到磁盘上的文件中,我可以使用媒体播放器播放它,所以我知道它没有损坏。

非常感谢任何建议。

1 个答案:

答案 0 :(得分:1)

Try reloading the stream

class Spec {
    public $a;
    public $b;
    public $c;

    function calcParameter() {
        $this->c = $this->a + $this->b;
    }

    function calcPlot() {
        $this->c = $this->a * $this->b;
    }
}

class Plot {
    public $spec;

    function __construct() {
        $this->spec = new Spec();
    }

    function calcPlot() {
        $this->spec->calcPlot()
    }
}


class Controller {
    public $spec;
    public $plot;

    function __construct() {
        $this->spec = new Spec();
        $this->plot = new Plot();
    }

    function doBusinessLogic() {
        //spec for local
        $this->spec->a = 5;
        $this->spec->b = 5;
        $this->spec->calcParameter();
        print "total is {$this->spec->c}<br>\n";

        //later this format is used by JS to display computational results
        $plotJSON = json_encode($this->spec); 

        //spec for plot
        $this->plot->spec->a = 7;
        $this->plot->spec->b = 143;
        $this->plot->calcPlot();
        print "total is {$this->plot->spec->c}<br>\n";

        //later this format is used by JS to display a plot
        $plotJSON = json_encode($this->plot); 
        print "
        <div id='plot' style='display:none'>$plotJSON</div>
        <script>
            var plot = JSON.parse(document.getElementById('plot').innerHTML);
            document.write('JS says - there are ' + plot.spec.c + ' plot points<br>');
        </script>
        ";
    }  
}

//runs the above
(new Controller())->doBusinessLogic();

可替换地:

soundStream.Position = 0;

using (System.Media.SoundPlayer sound = new System.Media.SoundPlayer(soundStream))
{
    sound.Play();
}