0

我正在 Ubuntu 10.04/12.04 上测试以下代码。我收到一个错误

#include <gtkmm.h>
#include <gstreamermm.h>
#include <iostream>

using namespace std;

class Sound
{
    public:
        Sound();

        void start_playing(double frequency);
        bool stop_playing();

    private:
        Glib::RefPtr<Gst::Pipeline> m_pipeline;
        Glib::RefPtr<Gst::Element> m_source;
        Glib::RefPtr<Gst::Element> m_sink;
};

Sound::Sound()
{
    m_pipeline = Gst::Pipeline::create("note");
    m_source = Gst::ElementFactory::create_element("audiotestsrc",
                                                   "source");
    m_sink = Gst::ElementFactory::create_element("autoaudiosink",
                                                 "output");
    m_pipeline->add(m_source);
    m_pipeline->add(m_sink);
    m_source->link(m_sink);
}

void Sound::start_playing (double frequency)
{
    m_source->set_property("freq", frequency);
    m_pipeline->set_state(Gst::STATE_PLAYING);

    /* stop it after 200ms */
    Glib::signal_timeout().connect(sigc::mem_fun(*this, &Sound::stop_playing),
                                   200);
}

bool Sound::stop_playing()
{
    m_pipeline->set_state(Gst::STATE_NULL);
    return false;
}


class Buttons : public Gtk::Window
{
public:
  Buttons();
  virtual ~Buttons();

protected:
  //Signal handlers:
  //void on_button_clicked();

  Sound sound;
  //void on_button_clicked(double frequency, Sound& sound);
  void on_button_clicked(double frequency, Sound* sound);

  //Child widgets:
  Gtk::Button m_button;
};

Buttons::Buttons()
{
  m_button.add_pixlabel("info.xpm", "cool button");

  set_title("Pixmap'd buttons!");
  set_border_width(10);

  //m_button.signal_clicked().connect( sigc::mem_fun(*this,
  //            &Buttons::on_button_clicked) );
  //m_button.signal_clicked().connect (sigc::bind<double, Sound*>(sigc::ptr_fun(&Buttons::on_button_clicked),
  //                                                  369.23, &sound));
  //m_button.signal_clicked().connect( sigc::bind<double, Sound&>( sigc::mem_fun(*this, &Buttons::on_button_clicked), 369.23, sound) ); 


  m_button.signal_clicked().connect( sigc::bind<double, Sound*>( sigc::mem_fun(*this, &Buttons::on_button_clicked), 369.23, &sound) ); 

  add(m_button);

  show_all_children();
}

Buttons::~Buttons()
{
}

/*
void Buttons::on_button_clicked()
{
  std::cout << "The Button was clicked." << std::endl;
}
*/

/*
void
Buttons::on_button_clicked(double frequency, Sound& sound)
{
    sound.start_playing (frequency);
}
*/

void
Buttons::on_button_clicked(double frequency, Sound* sound)
{
    sound->start_playing (frequency);
}


int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  Buttons buttons;
  //Shows the window and returns when it is closed.
  Gtk::Main::run(buttons);

  return 0;
}

我编译使用g++ gstmm1.cc -o gstmm1 ``pkg-config --cflags --libs gstreamermm-0.10 gtkmm-2.4

terminate called after throwing an instance of 'std::runtime_error'
  what():  Failed to add null element.
Aborted

任何想法可能有什么问题?几乎看起来这可能与以下帖子有关 http://old.nabble.com/gstmm-add-element-to-pipeline-test-td14042055.html

4

1 回答 1

1

这对我有用!我缺少代码 Gst::init(); 在主()

#include <gtkmm.h>
#include <gstreamermm.h>
#include <glibmm/main.h>
#include <iostream>


class Sound
{
    public:
        Sound();

        void start_playing(double frequency);
        bool stop_playing();

    private:
        Glib::RefPtr<Gst::Pipeline> m_pipeline;
        Glib::RefPtr<Gst::Element> m_source;
        Glib::RefPtr<Gst::Element> m_sink;
};

Sound::Sound()
{
    m_pipeline = Gst::Pipeline::create("note");
    m_source = Gst::ElementFactory::create_element("audiotestsrc",
                                               "source");
    m_sink = Gst::ElementFactory::create_element("autoaudiosink",
                                             "output");
    m_pipeline->add(m_source);
    m_pipeline->add(m_sink);
    m_source->link(m_sink);
}

void Sound::start_playing (double frequency)
{
    m_source->set_property("freq", frequency);
    //m_pipeline->set_state(Gst::STATE_NULL);
    //m_pipeline->set_state(Gst::STATE_PAUSED);
    //m_pipeline->set_state(Gst::STATE_READY);
    m_pipeline->set_state(Gst::STATE_PLAYING);

    /* stop it after 200ms */
    Glib::signal_timeout().connect(sigc::mem_fun(*this, &Sound::stop_playing),
                               200);
}

bool Sound::stop_playing()
{
    //m_pipeline->set_state(Gst::STATE_PAUSED);
    m_pipeline->set_state(Gst::STATE_NULL);
    return false;
}

class HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  virtual ~HelloWorld();

protected:
  //Signal handlers:
  void on_button_clicked();

  //Member widgets:
  Gtk::Button m_button;
  Sound sound;
};

HelloWorld::HelloWorld()
: m_button("Hear Sound")   // creates a new button with label "Hello World".
{
  // Sets the border width of the window.
  set_border_width(10);


  // When the button receives the "clicked" signal, it will call the
  // on_button_clicked() method defined below.
  m_button.signal_clicked().connect(sigc::mem_fun(*this,
          &HelloWorld::on_button_clicked));


  // This packs the button into the Window (a container).
  add(m_button);

  // The final step is to display this newly created widget...
  m_button.show();
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::on_button_clicked()
{
  std::cout << "Hello World" << std::endl;

  sound.start_playing(400);
}


int main(int argc, char* argv[]) {

  Gst::init(); 

  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  HelloWorld helloworld;

  //Shows the window and returns when it is closed.
  return app->run(helloworld);

return 0;
}
于 2012-06-14T02:40:50.557 回答