首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误错误码

错误错误码
EN

Stack Overflow用户
提问于 2014-01-31 11:50:12
回答 1查看 481关注 0票数 1

我用portaudio来播放声音。我希望能够通过UI选择输出。我就是这样做到的:

代码语言:javascript
复制
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的工作方式就像一种魅力。

因此:

  • 为什么会失败?
  • 为什么它抛出错误的错误代码?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-31 13:10:10

我假设您使用C++ (尽管代码中有一些奇怪的andor )。

如果您的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()时,这就是它的原因。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21479400

复制
相关文章

相似问题

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