【问题标题】:How do I add an playable audio file to a TableListBox playlist (JUCE C++)如何将可播放的音频文件添加到 TableListBox 播放列表(JUCE C++)
【发布时间】:2021-09-09 03:02:57
【问题描述】:

我想点击一个按钮来打开文件浏览器,打开一个音频文件(mp3),它被添加到一个表格中。我希望它可以让您在播放列表表中拥有多个可用的音频文件。我已经弄清楚了一些,但是我正在尝试创建一个事件侦听器,在该侦听器中单击一个按钮“添加轨道”。如何使用音频文件填充我的表格或“播放列表”?我已经实现了一个文件选择器来加载要播放的文件和一个播放加载文件的播放按钮,我只是不确定如何将它添加到表中?任何帮助表示赞赏。 到目前为止,我的代码大致如下:

Array<File> trackTitles;

...

void PlaylistComponent::buttonClicked(Button* button)
{

    if (button == &loadButton)
    {
        DBG(" MainComponent::buttonClicked: loadButton");
        FileChooser chooser{ "Select a file" };
        if (chooser.browseForFileToOpen())
        {
            trackTitles.add(File{ chooser.getResult() });
            player->loadURL(File{ chooser.getResult() });
        }


    }
}

【问题讨论】:

  • 您需要提供一些代码。至少一个 sn-p,理想情况下是一个最小的复制案例,可以显示你在哪里。否则真的很难帮你。
  • PlaylistComponent 继承自哪里?
  • JUCE/examples/GUI/WidgetsDemo.h 中有一个演示如何创建表。你可能想看看那个。

标签: c++ file user-interface audio juce


【解决方案1】:

您需要在表格中添加按钮吗?这是一个例子

.h

    class PlaylistComponent : public juce::Component,
                              public TableListBoxModel,
                              public Button::Listener
    {
    .....
    Component *refreshComponentForCell(int rowNumber, int columnId, bool , Component *existingComponentToUpdate) override;
    void paintCell(Graphics &, int rowNumber, int columnId, int width, int height, bool ) override;
    void buttonClicked(Button *button) override;
    void setTracks(Array<File> tracksFile);


    TableListBox tableComponent;

    .....
    }

.cpp

PlaylistComponent::PlaylistComponent() 
{
    // In your constructor, you should add any child components, and
    // initialise any special settings that your component needs.
   
    tableComponent.getHeader().addColumn("", 1, 20);
    tableComponent.getHeader().addColumn("Track Title", 3, 500);
    tableComponent.setModel(this);
    addAndMakeVisible(tableComponent);
}


Component * PlaylistComponent::refreshComponentForCell(int rowNumber, int columnId, bool , Component *existingComponentToUpdate)
{
    
    if (columnId == ) // The ID and Length columns do not have a custom component
    {
        jassert(existingComponentToUpdate == nullptr);
        return nullptr;
    }

    if(columnId == 1)
    {
        // Creating component if does not existst yet

        TextButton * btn = static_cast<TextButton *> (existingComponentToUpdate);

        if (btn == 0)
        {
            btn = new TextButton { "Play" };
        }


        String id{ std::to_string(rowNumber) };
        btn->setComponentID(id);
        btn->addListener(this);
        existingComponentToUpdate = btn;
      
    }
    return existingComponentToUpdate;
   
}
void PlaylistComponent::paintCell(Graphics &g, int rowNumber, int columnId, int width, int height, bool )
{
    if (columnId == 2) {
        g.drawText(trackTitles[rowNumber], 2, 0, width - 4, height, Justification::centredLeft, true);
    }
    
}

void PlaylistComponent::setTracks(Array<File> tracksFile)
{

    for (int i = 0; i < tracksFile.size(); i++)
    {
        trackTitles.push_back(tracksFile[i].getFileName());
      
    }
    tableComponent.updateContent();

}

void PlaylistComponent::buttonClicked(Button *button)
{


   //Array<File> file;
   player->loadURL(file[std::stoi(button->getComponentID().toStdString())]); // your player
   player->start();

   
}

那么在桌子附近应该有一个加载大量音频文件的按钮,你可以将它存储在一个向量中

Array<File> file;
..........
void loadAudioFiles()
{
    juce::String filters = "*.mp3";
    FileChooser chooser("Select an mp3 files..", File::getSpecialLocation(File::userHomeDirectory), filters);

   
    if (chooser.browseForMultipleFilesToOpen())
    {
        file = chooser.getResults();
        playlistComponent.setTracks(file );

    }
}

【讨论】:

  • 不一定要在表中,这个非常有帮助。但是,我现在在 loadAudioFiles() 函数中遇到问题,其中 setTracks 将文件结果作为参数我收到错误“没有合适的用户定义转换'juce::Array...' 到 'std::vector。 ..'”。我假设因为 setTracks 需要一个向量,但 loadAudioFiles 函数中的文件必须是一个数组才能从文件选择器中获取结果?关于如何解决这个问题的任何想法?
  • 对不起,将 void PlaylistComponent::setTracks(std::vector trackingFile) 更改为 void PlaylistComponent::setTracks(Array trackingFile) '
  • void setTracks(std::vector trackingFile) 更改为 void setTracks(Array file);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多