我用portaudio来播放声音。我希望能够通过UI选择输出。我就是这样做到的:
PaError err = Pa_Initialize();
if( err != paNoError )
return false;
qDebug() <<"Port audio succeed initialization !";
int numDevices;
numDevices = Pa_GetDeviceCount();
if( numDevices <= 0 )
{
qDebug() << "ERROR: Pa_CountDevices returned " << numDevices;
return false;
}
const PaDeviceInfo *deviceInfo;
bool isThereOutput = false;
int i = 0;
while(i < numDevices and !isThereOutput)
{
deviceInfo = Pa_GetDeviceInfo( i );
isThereOutput = (deviceInfo->maxOutputChannels > 0);
i++;
}
if(!isThereOutput)
{
qDebug() << "No output device";
return false;
}
PaError errorOpening;
if(outputDevice != "")
{
PaStreamParameters outputDeviceInfo;
int numDevices = Pa_GetDeviceCount();
const PaDeviceInfo *deviceInfo;
for(int i = 0; i<numDevices; i++ )
{
deviceInfo = Pa_GetDeviceInfo( i );
if(deviceInfo->maxOutputChannels > 0 && deviceInfo->name == outputDevice)
{
outputDeviceInfo.device = i;
outputDeviceInfo.channelCount = 1;
outputDeviceInfo.sampleFormat = paInt8;
outputDeviceInfo.suggestedLatency = deviceInfo->defaultLowOutputLatency;
}
}
if(outputDeviceInfo.channelCount > 1)
{
errorOpening = Pa_OpenStream(&stream, NULL, &outputDeviceInfo, SAMPLE_RATE, FRAME_PER_BUFFER, paNoFlag, audioCallback, this);
}
}
if(outputDevice == "" or errorOpening != paNoError)
{
if(errorOpening != paNoError)
qDebug() << "Can't open selected device ("<< outputDevice <<"), switching to the default one. Error : " << Pa_GetErrorText(errorOpening);
errorOpening = Pa_OpenDefaultStream( &stream,
0, /* no input channels */
1, /* mono output */
paInt8, /* 8 bits output */
SAMPLE_RATE,
FRAME_PER_BUFFER, /* frames per buffer, i.e. the number
of sample frames that PortAudio will
request from the callback. Many apps
may want to use
paFramesPerBufferUnspecified, which
tells PortAudio to pick the best,
possibly changing, buffer size.*/
audioCallback, /* this is your callback function */
this ); /*This is a pointer that will be passed to
your callback*/
}
if(errorOpening != paNoError)
return false;
if(Pa_StartStream( stream ) != paNoError)
return false;但它失败了:
无法打开选定的设备( "Sortie intégr“),切换到默认设备。错误:错误代码无效(值大于零)
但是,我无法理解为什么OpenStream会出现奇怪的错误代码,而Pa_OpenDefaultStream的工作方式就像一种魅力。
因此:
发布于 2014-01-31 13:10:10
我假设您使用C++ (尽管代码中有一些奇怪的and和or )。
如果您的for循环没有找到满足eviceInfo->maxOutputChannels > 0 && deviceInfo->name == outputDevice的任何PaDeviceInfo,那么outputDeviceInfo就会离开eviceInfo->maxOutputChannels > 0 && deviceInfo->name == outputDevice这意味着它的channelConnect可以有任何值,包括大的负值。那么un-initialized.就不会调用Pa_OpenStream,您的errorOpening也会被留下我敢打赌,当你把Invalid error code (value greater than zero)输入Pa_GetErrorText()时,这就是它的原因。
https://stackoverflow.com/questions/21479400
复制相似问题