88 lines
1.4 KiB
C++
88 lines
1.4 KiB
C++
|
|
#include <libsaria/audio.h>
|
|
#include <libsaria/callback.h>
|
|
#include <libsaria/print.h>
|
|
#include <libsaria/path.h>
|
|
#include "audio.h"
|
|
|
|
static string cur_file;
|
|
static GstState cur_state = GST_STATE_NULL;
|
|
|
|
static bool change_state(GstState new_state)
|
|
{
|
|
GstStateChangeReturn ret;
|
|
ret = gst_element_set_state(GST_ELEMENT(player), new_state);
|
|
|
|
switch(ret) {
|
|
case GST_STATE_CHANGE_SUCCESS:
|
|
case GST_STATE_CHANGE_ASYNC:
|
|
cur_state = new_state;
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static void reset()
|
|
{
|
|
cur_file = "";
|
|
change_state(GST_STATE_NULL);
|
|
}
|
|
|
|
void load_file(GstElement *playbin, string file)
|
|
{
|
|
if (file == "" || !exists(file))
|
|
return;
|
|
string uri = "file://" + file;
|
|
println("Loading uri: " + uri);
|
|
g_object_set(G_OBJECT(playbin), "uri", uri.c_str(), NULL);
|
|
cur_file = file;
|
|
}
|
|
|
|
namespace libsaria
|
|
{
|
|
|
|
void audio::play()
|
|
{
|
|
if (cur_file == "")
|
|
return;
|
|
if (change_state(GST_STATE_PLAYING))
|
|
trigger_callback(PLAY);
|
|
}
|
|
|
|
void audio::pause()
|
|
{
|
|
if (cur_file == "")
|
|
return;
|
|
if (change_state(GST_STATE_PAUSED))
|
|
trigger_callback(PAUSE);
|
|
}
|
|
|
|
bool audio::is_playing()
|
|
{
|
|
return cur_state == GST_STATE_PLAYING;
|
|
}
|
|
|
|
void audio::stop()
|
|
{
|
|
if (cur_file == "")
|
|
return;
|
|
pause();
|
|
seek_to(0);
|
|
}
|
|
|
|
void audio::load(string file)
|
|
{
|
|
if (file == "")
|
|
return;
|
|
reset();
|
|
load_file(player, file);
|
|
}
|
|
|
|
string audio::get_current_file()
|
|
{
|
|
return cur_file;
|
|
}
|
|
|
|
};
|