在C#程序中设置声音时出错 [英] Error with setting the sound in C# program
本文介绍了在C#程序中设置声音时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此程序将在我编写的计算机上运行,但不会在其他计算机上运行。其他计算机上出现的错误是设置声音时,这是对dll的调用。任何帮助将不胜感激!
以下是该计划的代码:
This program will run on the computer I wrote it on, but will not run on other computers. The error that comes up on other computers is when the sound is set, which is a call to the dll. Any help would be greatly appreciated!
Here is the code from the program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.DirectX.DirectSound;
using System.Media;
using WaveRecorder;
using System.Windows;
namespace SpellingBee
{
public partial class AddQuizForm : Form
{
//Sound devices
//Sound reference http://aaronamberman.com/tutorials/csaudio.php
private SoundCapture sndCapt;
private SoundDevices sndDevice;
private Playback playBack;
private bool isRecording = false;
}
private void setSound()
{
//int sampling = 96000;
//short bitSample = 16;
//short chnls = 2;
try
{
//Set and reset devices
sndDevice = new SoundDevices(this);
sndCapt = new SoundCapture(96000);
sndCapt.OnWavComplete += new SoundCapture.Complete(sndCapt_OnWavComplete);
//sndCapt = new SoundCapture(SoundCapture._96000_HZ, SoundCapture._16_Bit, SoundCapture._STEREO);
}
catch (Exception ex)
{
MessageBox.Show(String.Concat(ex.StackTrace, ex.Message));
if (ex.InnerException != null)
{
MessageBox.Show("Sound could not be set at setSound()" + Environment.NewLine +
String.Concat(ex.InnerException.StackTrace, ex.InnerException.Message));
}
}
}
这是来自dll的代码:
This is the code from the dll:
public SoundCapture(int samplingRate)
{
if (samplingRate < 11025) samplingRate = 11025;
else if (samplingRate > 11025 && samplingRate < 22050) samplingRate = 22050;
else if (samplingRate > 22050 && samplingRate < 44100) samplingRate = 44100;
else if (samplingRate > 44100 && samplingRate < 96000) samplingRate = 96000;
else if (samplingRate > 96000) samplingRate = 96000;
wvFrmt = new WaveFormat();
wvFrmt.SamplesPerSecond = samplingRate;
if (samplingRate <= 22050) wvFrmt.BitsPerSample = 8;
else if (samplingRate >= 44100) wvFrmt.BitsPerSample = 16;
if (samplingRate <= 22050) wvFrmt.Channels = 1;
else if (samplingRate >= 44100) wvFrmt.Channels = 2;
wvFrmt.FormatTag = WaveFormatTag.Pcm;
wvFrmt.BlockAlign = (short)(wvFrmt.Channels * (wvFrmt.BitsPerSample / 8));
wvFrmt.AverageBytesPerSecond = wvFrmt.SamplesPerSecond * wvFrmt.BlockAlign;
cpturBffrDesc = new CaptureBufferDescription();
cpturBffrDesc.BufferBytes = wvFrmt.AverageBytesPerSecond;
cpturBffrDesc.ControlEffects = false;
cpturBffrDesc.Format = wvFrmt;
cpturBffrDesc.WaveMapped = true;
cptur = new Capture();
cptrBffr = new CaptureBuffer(cpturBffrDesc, cptur);//This is where the 2nd part of the error is listed
rawSoundData = new List<byte[]>();
SetCaptureNotifications();
我在我用它创建的计算机上运行了msi文件运行正常,其他计算机有相同的操作系统和一切但不会运行?
I have run the msi file on the computer I created this with and it runs fine, other computers have same os and everything but won't run?
推荐答案
您的列表中缺少此行:
captureBufferDesc = new CaptureBufferDescription();
You are missing this line in your listing:
captureBufferDesc = new CaptureBufferDescription();
我遇到了同样的问题。但是当我在调试模式下编译程序时它以某种方式被修复了。
请注意,在发布模式下仍然存在问题。
I met the same problem. But it was cured somehow when I compiled the program in Debug mode.
Note that there's still a problem in Release mode.
这篇关于在C#程序中设置声音时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文