libsaria: created Audio() class
This class will be used to control gstreamer functions. So far, it can load a song, play, and pause.
This commit is contained in:
parent
c716f62aaa
commit
3e2f374bf8
|
@ -1,10 +1,34 @@
|
|||
#ifndef LIBSARIA_AUDIO_H
|
||||
#define LIBSARIA_AUDIO_H
|
||||
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
extern "C" {
|
||||
#include <gst/gst.h>
|
||||
}
|
||||
|
||||
class Audio
|
||||
{
|
||||
private:
|
||||
GstElement *player;
|
||||
string cur_file;
|
||||
|
||||
void reset();
|
||||
void change_state(GstState);
|
||||
|
||||
public:
|
||||
Audio();
|
||||
~Audio();
|
||||
|
||||
void init(int, char **);
|
||||
void load(string);
|
||||
|
||||
/* Control functions */
|
||||
void play();
|
||||
void pause();
|
||||
};
|
||||
|
||||
void audio_init(int, char **);
|
||||
void gst_play();
|
||||
void gst_pause();
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
#ifndef LIBSARIA_H
|
||||
#define LIBSARIA_H
|
||||
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/print.h>
|
||||
|
||||
class Libsaria
|
||||
{
|
||||
private:
|
||||
Audio audio;
|
||||
|
||||
public:
|
||||
Libsaria(int, char**);
|
||||
~Libsaria();
|
||||
|
@ -12,7 +16,6 @@ class Libsaria
|
|||
/* Control functions */
|
||||
void play();
|
||||
void pause();
|
||||
|
||||
};
|
||||
|
||||
class Libsaria *libsaria_get();
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/libsaria.h>
|
||||
|
||||
static bool initialized = false;
|
||||
static void init_gstreamer(int argc, char **argv)
|
||||
{
|
||||
if (initialized == true)
|
||||
return;
|
||||
gst_init(&argc, &argv);
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
Audio::Audio()
|
||||
{
|
||||
print("Creating audio driver");
|
||||
}
|
||||
|
||||
Audio::~Audio()
|
||||
{
|
||||
}
|
||||
|
||||
void Audio::init(int argc, char **argv)
|
||||
{
|
||||
print("Initializing gstreamer");
|
||||
init_gstreamer(argc, argv);
|
||||
player = gst_element_factory_make("playbin2", "player");
|
||||
}
|
||||
|
||||
void Audio::reset()
|
||||
{
|
||||
cur_file = "";
|
||||
change_state(GST_STATE_NULL);
|
||||
}
|
||||
|
||||
void Audio::load(string file)
|
||||
{
|
||||
string uri = "file://" + file;
|
||||
reset();
|
||||
cur_file = file;
|
||||
g_object_set(G_OBJECT(player), "uri", uri.c_str(), NULL);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
|
||||
#include <libsaria/audio.h>
|
||||
|
||||
void Audio::change_state(GstState new_state)
|
||||
{
|
||||
gst_element_set_state(GST_ELEMENT(player), new_state);
|
||||
}
|
||||
|
||||
void Audio::play()
|
||||
{
|
||||
change_state(GST_STATE_PLAYING);
|
||||
}
|
||||
|
||||
void Audio::pause()
|
||||
{
|
||||
change_state(GST_STATE_PAUSED);
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/libsaria.h>
|
||||
|
||||
static GstElement *player;
|
||||
static string cur_file;
|
||||
|
||||
void change_state(GstState new_state)
|
||||
{
|
||||
gst_element_set_state(GST_ELEMENT(player), new_state);
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
cur_file = "";
|
||||
change_state(GST_STATE_NULL);
|
||||
}
|
||||
|
||||
void load_file(string file)
|
||||
{
|
||||
string uri = "file://" + file;
|
||||
cur_file = file;
|
||||
g_object_set(G_OBJECT(player), "uri", uri.c_str(), NULL);
|
||||
}
|
||||
|
||||
void gst_play()
|
||||
{
|
||||
print("Playing!");
|
||||
change_state(GST_STATE_PLAYING);
|
||||
}
|
||||
|
||||
void gst_pause()
|
||||
{
|
||||
print("Paused!");
|
||||
change_state(GST_STATE_PAUSED);
|
||||
}
|
||||
|
||||
void audio_init(int argc, char **argv)
|
||||
{
|
||||
print("Initializing audio...");
|
||||
gst_init(&argc, &argv);
|
||||
|
||||
player = gst_element_factory_make("playbin2", "player");
|
||||
if (argc > 1)
|
||||
load_file(argv[1]);
|
||||
gst_play();
|
||||
}
|
|
@ -4,10 +4,10 @@
|
|||
|
||||
void Libsaria::play()
|
||||
{
|
||||
gst_play();
|
||||
audio.play();
|
||||
}
|
||||
|
||||
void Libsaria::pause()
|
||||
{
|
||||
gst_pause();
|
||||
audio.pause();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/libsaria.h>
|
||||
|
||||
static class Libsaria *libsaria;
|
||||
|
@ -16,7 +12,6 @@ void libsaria_init(int argc, char **argv)
|
|||
{
|
||||
print("Initializing libsaria");
|
||||
libsaria = new Libsaria(argc, argv);
|
||||
audio_init(argc, argv);
|
||||
}
|
||||
|
||||
void libsaria_quit()
|
||||
|
|
|
@ -3,7 +3,11 @@
|
|||
|
||||
Libsaria::Libsaria(int argc, char **argv)
|
||||
{
|
||||
|
||||
audio.init(argc, argv);
|
||||
if (argc > 1) {
|
||||
audio.load(argv[1]);
|
||||
audio.play();
|
||||
}
|
||||
}
|
||||
|
||||
Libsaria::~Libsaria()
|
||||
|
|
Loading…
Reference in New Issue