playback/player: Update Android bindings to the latest API

This commit is contained in:
Sebastian Dröge 2015-03-01 14:20:17 +01:00
parent b7a1198170
commit 5035124050
2 changed files with 77 additions and 21 deletions

View file

@ -55,6 +55,8 @@ static JavaVM *java_vm;
static jfieldID native_player_field_id;
static jmethodID on_position_updated_method_id;
static jmethodID on_duration_changed_method_id;
static jmethodID on_state_changed_method_id;
static jmethodID on_buffering_method_id;
static jmethodID on_end_of_stream_method_id;
static jmethodID on_error_method_id;
static jmethodID on_video_dimensions_changed_method_id;
@ -130,6 +132,32 @@ on_duration_changed (GstPlayer * unused, GstClockTime duration, Player * player)
}
}
static void
on_state_changed (GstPlayer * unused, GstPlayerState state, Player * player)
{
JNIEnv *env = get_jni_env ();
(*env)->CallVoidMethod (env, player->java_player,
on_state_changed_method_id, state);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
}
}
static void
on_buffering (GstPlayer * unused, gint percent, Player * player)
{
JNIEnv *env = get_jni_env ();
(*env)->CallVoidMethod (env, player->java_player,
on_buffering_method_id, percent);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
}
}
static void
on_end_of_stream (GstPlayer * unused, Player * player)
{
@ -187,6 +215,10 @@ native_new (JNIEnv * env, jobject thiz)
G_CALLBACK (on_position_updated), player);
g_signal_connect (player->player, "duration-changed",
G_CALLBACK (on_duration_changed), player);
g_signal_connect (player->player, "state-changed",
G_CALLBACK (on_state_changed), player);
g_signal_connect (player->player, "buffering",
G_CALLBACK (on_buffering), player);
g_signal_connect (player->player, "end-of-stream",
G_CALLBACK (on_end_of_stream), player);
g_signal_connect (player->player, "error", G_CALLBACK (on_error), player);
@ -283,20 +315,6 @@ native_get_uri (JNIEnv * env, jobject thiz)
return uri;
}
static jboolean
native_is_playing (JNIEnv * env, jobject thiz)
{
Player *player = GET_CUSTOM_DATA (env, thiz, native_player_field_id);
jboolean is_playing;
if (!player)
return FALSE;
g_object_get (player->player, "is-playing", &is_playing, NULL);
return is_playing;
}
static jlong
native_get_position (JNIEnv * env, jobject thiz)
{
@ -406,6 +424,10 @@ native_class_init (JNIEnv * env, jclass klass)
(*env)->GetMethodID (env, klass, "onPositionUpdated", "(J)V");
on_duration_changed_method_id =
(*env)->GetMethodID (env, klass, "onDurationChanged", "(J)V");
on_state_changed_method_id =
(*env)->GetMethodID (env, klass, "onStateChanged", "(I)V");
on_buffering_method_id =
(*env)->GetMethodID (env, klass, "onBuffering", "(I)V");
on_end_of_stream_method_id =
(*env)->GetMethodID (env, klass, "onEndOfStream", "()V");
on_error_method_id =
@ -415,6 +437,7 @@ native_class_init (JNIEnv * env, jclass klass)
if (!native_player_field_id ||
!on_position_updated_method_id || !on_duration_changed_method_id ||
!on_state_changed_method_id || !on_buffering_method_id ||
!on_end_of_stream_method_id ||
!on_error_method_id || !on_video_dimensions_changed_method_id) {
static const gchar *message =
@ -437,7 +460,6 @@ static JNINativeMethod native_methods[] = {
{"nativeFree", "()V", (void *) native_free},
{"nativeGetUri", "()Ljava/lang/String;", (void *) native_get_uri},
{"nativeSetUri", "(Ljava/lang/String;)V", (void *) native_set_uri},
{"nativeIsPlaying", "()Z", (void *) native_is_playing},
{"nativeGetPosition", "()J", (void *) native_get_position},
{"nativeGetDuration", "()J", (void *) native_get_duration},
{"nativeGetVolume", "()D", (void *) native_get_volume},

View file

@ -1,6 +1,6 @@
/* GStreamer
*
* Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
* Copyright (C) 2014-2015 Sebastian Dröge <sebastian@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -77,11 +77,6 @@ public class Player implements Closeable {
nativeSetUri(uri);
}
private native boolean nativeIsPlaying();
public boolean isPlaying() {
return nativeIsPlaying();
}
private native long nativeGetPosition();
public long getPosition() {
return nativeGetPosition();
@ -153,6 +148,45 @@ public class Player implements Closeable {
}
}
private static final State[] stateMap = {State.STOPPED, State.BUFFERING, State.PAUSED, State.PLAYING};
public enum State {
STOPPED,
BUFFERING,
PAUSED,
PLAYING
}
public static interface StateChangedListener {
abstract void stateChanged(Player player, State state);
}
private StateChangedListener stateChangedListener;
public void setStateChangedListener(StateChangedListener listener) {
stateChangedListener = listener;
}
private void onStateChanged(int stateIdx) {
if (stateChangedListener != null) {
State state = stateMap[stateIdx];
stateChangedListener.stateChanged(this, state);
}
}
public static interface BufferingListener {
abstract void buffering(Player player, int percent);
}
private BufferingListener bufferingListener;
public void setBufferingListener(BufferingListener listener) {
bufferingListener = listener;
}
private void onBuffering(int percent) {
if (bufferingListener != null) {
bufferingListener.buffering(this, percent);
}
}
public static interface EndOfStreamListener {
abstract void endOfStream(Player player);
}