3 * Copyright (C) 2014-2015 Sebastian Dröge <sebastian@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 package org.freedesktop.gstreamer;
23 import java.io.Closeable;
24 import android.view.Surface;
25 import android.content.Context;
26 import org.freedesktop.gstreamer.GStreamer;
28 public class Player implements Closeable {
29 private static native void nativeClassInit();
30 public static void init(Context context) throws Exception {
31 System.loadLibrary("gstreamer_android");
32 GStreamer.init(context);
34 System.loadLibrary("gstplayer");
38 private long native_player;
39 private native void nativeNew();
44 private native void nativeFree();
50 private native void nativePlay();
55 private native void nativePause();
60 private native void nativeStop();
65 private native void nativeSeek(long position);
66 public void seek(long position) {
70 private native String nativeGetUri();
71 public String getUri() {
72 return nativeGetUri();
75 private native void nativeSetUri(String uri);
76 public void setUri(String uri) {
80 private native long nativeGetPosition();
81 public long getPosition() {
82 return nativeGetPosition();
85 private native long nativeGetDuration();
86 public long getDuration() {
87 return nativeGetDuration();
90 private native double nativeGetVolume();
91 public double getVolume() {
92 return nativeGetVolume();
95 private native void nativeSetVolume(double volume);
96 public void setVolume(double volume) {
97 nativeSetVolume(volume);
100 private native boolean nativeGetMute();
101 public boolean getMute() {
102 return nativeGetMute();
105 private native void nativeSetMute(boolean mute);
106 public void setMute(boolean mute) {
110 private Surface surface;
111 private native void nativeSetSurface(Surface surface);
112 public void setSurface(Surface surface) {
113 this.surface = surface;
114 nativeSetSurface(surface);
117 public Surface getSurface() {
121 public static interface PositionUpdatedListener {
122 abstract void positionUpdated(Player player, long position);
125 private PositionUpdatedListener positionUpdatedListener;
126 public void setPositionUpdatedListener(PositionUpdatedListener listener) {
127 positionUpdatedListener = listener;
130 private void onPositionUpdated(long position) {
131 if (positionUpdatedListener != null) {
132 positionUpdatedListener.positionUpdated(this, position);
136 public static interface DurationChangedListener {
137 abstract void durationChanged(Player player, long duration);
140 private DurationChangedListener durationChangedListener;
141 public void setDurationChangedListener(DurationChangedListener listener) {
142 durationChangedListener = listener;
145 private void onDurationChanged(long duration) {
146 if (durationChangedListener != null) {
147 durationChangedListener.durationChanged(this, duration);
151 private static final State[] stateMap = {State.STOPPED, State.BUFFERING, State.PAUSED, State.PLAYING};
159 public static interface StateChangedListener {
160 abstract void stateChanged(Player player, State state);
163 private StateChangedListener stateChangedListener;
164 public void setStateChangedListener(StateChangedListener listener) {
165 stateChangedListener = listener;
168 private void onStateChanged(int stateIdx) {
169 if (stateChangedListener != null) {
170 State state = stateMap[stateIdx];
171 stateChangedListener.stateChanged(this, state);
175 public static interface BufferingListener {
176 abstract void buffering(Player player, int percent);
179 private BufferingListener bufferingListener;
180 public void setBufferingListener(BufferingListener listener) {
181 bufferingListener = listener;
184 private void onBuffering(int percent) {
185 if (bufferingListener != null) {
186 bufferingListener.buffering(this, percent);
190 public static interface EndOfStreamListener {
191 abstract void endOfStream(Player player);
194 private EndOfStreamListener endOfStreamListener;
195 public void setEndOfStreamListener(EndOfStreamListener listener) {
196 endOfStreamListener = listener;
199 private void onEndOfStream() {
200 if (endOfStreamListener != null) {
201 endOfStreamListener.endOfStream(this);
205 // Keep these in sync with gstplayer.h
206 private static final Error[] errorMap = {Error.FAILED};
211 public static interface ErrorListener {
212 abstract void error(Player player, Error error, String errorMessage);
215 private ErrorListener errorListener;
216 public void setErrorListener(ErrorListener listener) {
217 errorListener = listener;
220 private void onError(int errorCode, String errorMessage) {
221 if (errorListener != null) {
222 Error error = errorMap[errorCode];
223 errorListener.error(this, error, errorMessage);
227 public static interface VideoDimensionsChangedListener {
228 abstract void videoDimensionsChanged(Player player, int width, int height);
231 private VideoDimensionsChangedListener videoDimensionsChangedListener;
232 public void setVideoDimensionsChangedListener(VideoDimensionsChangedListener listener) {
233 videoDimensionsChangedListener = listener;
236 private void onVideoDimensionsChanged(int width, int height) {
237 if (videoDimensionsChangedListener != null) {
238 videoDimensionsChangedListener.videoDimensionsChanged(this, width, height);