libsaria: Fix up the audio seek() function

It now seeks forward or backward through a song by dt seconds.
This commit is contained in:
Bryan Schumaker 2011-09-03 23:03:31 -04:00
parent 77a7f84110
commit ded2c559af
3 changed files with 38 additions and 19 deletions

View File

@ -33,8 +33,9 @@ class Audio
bool pause();
/* Functions related to position in the song */
void seek();
bool seek(int);
bool seek_to(double);
bool get_position(gint64 &);
bool get_duration(gint64 &);
};

View File

@ -3,33 +3,51 @@
bool Audio::get_duration(gint64 &duration)
{
GstFormat format;
return gst_element_query_duration(GST_ELEMENT(player),
&format,
&duration);
NULL,
&duration);
}
bool Audio::get_position(gint64 &position)
{
GstFormat fmt = GST_FORMAT_TIME;
return gst_element_query_position(GST_ELEMENT(player),
&fmt,
&position);
}
bool Audio::seek_to(double prcnt)
{
bool ret;
gint64 duration;
ret = get_duration(duration);
gst_element_seek_simple(GST_ELEMENT(player),
GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH,
duration * prcnt);
if (ret == true) {
gst_element_seek_simple(GST_ELEMENT(player),
GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH,
duration * prcnt);
}
return ret;
}
/*
* Right now I just seek to the beginning of the song.
* Eventually I should allow the user to seek anywhere.
*/
void Audio::seek()
/* Use to seek forward or backward in a song */
bool Audio::seek(int dt)
{
gst_element_seek_simple(GST_ELEMENT(player),
GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH,
// Convert seconds to nano-seconds
0 * GST_SECOND);
bool ret;
gint64 cur_pos;
gint64 new_pos;
ret = get_position(cur_pos);
if (ret == true) {
/* Convert seconds to nano-seconds */
new_pos = cur_pos + (dt * GST_SECOND);
if (new_pos < 0)
new_pos = 0;
gst_element_seek_simple(GST_ELEMENT(player),
GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH,
new_pos);
}
return ret;
}

View File

@ -12,7 +12,7 @@ void Libsaria::seek_to(double prcnt)
void Libsaria::seek(int dt)
{
audio.seek();
audio.seek(dt);
}
/*