11package com .befovy .fijkplayer ;
22
33import android .content .Context ;
4- import android .content .res .AssetFileDescriptor ;
54import android .content .res .AssetManager ;
65import android .graphics .SurfaceTexture ;
76import android .net .Uri ;
1211import androidx .annotation .NonNull ;
1312
1413import java .io .File ;
14+ import java .io .FileNotFoundException ;
1515import java .io .IOException ;
16+ import java .io .InputStream ;
1617import java .util .HashMap ;
1718import java .util .Map ;
1819import java .util .concurrent .atomic .AtomicInteger ;
@@ -30,7 +31,22 @@ public class FijkPlayer implements MethodChannel.MethodCallHandler, IjkEventList
3031
3132 final private static AtomicInteger atomicId = new AtomicInteger (0 );
3233
34+ final private static int idle = 0 ;
35+ final private static int initialized = 1 ;
36+ final private static int asyncPreparing = 2 ;
37+ @ SuppressWarnings ("unused" )
38+ final private static int prepared = 3 ;
39+ @ SuppressWarnings ("unused" )
40+ final private static int started = 4 ;
41+ final private static int paused = 5 ;
42+ final private static int completed = 6 ;
43+ final private static int stopped = 7 ;
44+ @ SuppressWarnings ("unused" )
45+ final private static int error = 8 ;
46+ final private static int end = 9 ;
47+
3348 final private int mPlayerId ;
49+ private int mState ;
3450 final private IjkMediaPlayer mIjkMediaPlayer ;
3551 final private Context mContext ;
3652
@@ -53,6 +69,7 @@ public class FijkPlayer implements MethodChannel.MethodCallHandler, IjkEventList
5369 FijkPlayer (PluginRegistry .Registrar registrar ) {
5470 mRegistrar = registrar ;
5571 mPlayerId = atomicId .incrementAndGet ();
72+ mState = 0 ;
5673 mIjkMediaPlayer = new IjkMediaPlayer ();
5774 mIjkMediaPlayer .addIjkEventListener (this );
5875
@@ -93,9 +110,8 @@ long setupSurface() {
93110 }
94111
95112 void release () {
96- mIjkMediaPlayer . stop ( );
113+ handleEvent ( PLAYBACK_STATE_CHANGED , end , mState , null );
97114 mIjkMediaPlayer .release ();
98-
99115 if (mSurfaceTextureEntry != null ) {
100116 mSurfaceTextureEntry .release ();
101117 mSurfaceTextureEntry = null ;
@@ -121,6 +137,7 @@ private void handleEvent(int what, int arg1, int arg2, Object extra) {
121137 mEventSink .success (event );
122138 break ;
123139 case PLAYBACK_STATE_CHANGED :
140+ mState = arg1 ;
124141 event .put ("event" , "state_change" );
125142 event .put ("new" , arg1 );
126143 event .put ("old" , arg2 );
@@ -151,6 +168,9 @@ private void handleEvent(int what, int arg1, int arg2, Object extra) {
151168 event .put ("height" , arg2 );
152169 mEventSink .success (event );
153170 break ;
171+ case ERROR :
172+ mEventSink .error (String .valueOf (arg1 ), extra .toString (), arg2 );
173+ break ;
154174 default :
155175 // Log.d("FLUTTER", "jonEvent:" + what);
156176 break ;
@@ -166,6 +186,7 @@ public void onEvent(IjkMediaPlayer ijkMediaPlayer, int what, int arg1, int arg2,
166186 case BUFFERING_END :
167187 case BUFFERING_UPDATE :
168188 case VIDEO_SIZE_CHANGED :
189+ case ERROR :
169190 handleEvent (what , arg1 , arg2 , extra );
170191 break ;
171192 default :
@@ -200,6 +221,7 @@ private void applyOptions(Object options) {
200221
201222 @ Override
202223 public void onMethodCall (@ NonNull MethodCall call , @ NonNull MethodChannel .Result result ) {
224+ //noinspection IfCanBeSwitch
203225 if (call .method .equals ("setupSurface" )) {
204226 long viewId = setupSurface ();
205227 result .success (viewId );
@@ -223,7 +245,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
223245 String url = call .argument ("url" );
224246 Uri uri = Uri .parse (url );
225247 boolean openAsset = false ;
226- if ("assets " .equals (uri .getScheme ())) {
248+ if ("asset " .equals (uri .getScheme ())) {
227249 openAsset = true ;
228250 String host = uri .getHost ();
229251 String path = uri .getPath () != null ? uri .getPath ().substring (1 ) : "" ;
@@ -237,9 +259,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
237259 try {
238260 if (openAsset ) {
239261 AssetManager assetManager = mRegistrar .context ().getAssets ();
240- AssetFileDescriptor fd = assetManager .openFd (uri .getPath () != null ? uri .getPath () : "" );
241- //mIjkMediaPlayer.setDataSource(fd.getFileDescriptor());
242- mIjkMediaPlayer .setDataSource (new RawMediaDataSource (fd , mRegistrar .context (), uri .getPath ()));
262+ InputStream is = assetManager .open (uri .getPath () != null ? uri .getPath () : "" , AssetManager .ACCESS_RANDOM );
263+ mIjkMediaPlayer .setDataSource (new RawMediaDataSource (is ));
243264 } else {
244265 if (TextUtils .isEmpty (uri .getScheme ()) || "file" .equals (uri .getScheme ())) {
245266 IMediaDataSource dataSource = new FileMediaDataSource (new File (uri .toString ()));
@@ -248,12 +269,16 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
248269 mIjkMediaPlayer .setDataSource (mContext , uri );
249270 }
250271 }
272+ handleEvent (PLAYBACK_STATE_CHANGED , initialized , -1 , null );
251273 result .success (null );
274+ } catch (FileNotFoundException e ) {
275+ result .error ("-875574348" , "Local File not found:" + e .getMessage (), null );
252276 } catch (IOException e ) {
253- result .error (e .getMessage (), null , null );
277+ result .error ("-1162824012" , "Local IOException:" + e .getMessage (), null );
254278 }
255279 } else if (call .method .equals ("prepareAsync" )) {
256280 mIjkMediaPlayer .prepareAsync ();
281+ handleEvent (PLAYBACK_STATE_CHANGED , asyncPreparing , -1 , null );
257282 result .success (null );
258283 } else if (call .method .equals ("start" )) {
259284 mIjkMediaPlayer .start ();
@@ -263,9 +288,11 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
263288 result .success (null );
264289 } else if (call .method .equals ("stop" )) {
265290 mIjkMediaPlayer .stop ();
291+ handleEvent (PLAYBACK_STATE_CHANGED , stopped , -1 , null );
266292 result .success (null );
267293 } else if (call .method .equals ("reset" )) {
268294 mIjkMediaPlayer .reset ();
295+ handleEvent (PLAYBACK_STATE_CHANGED , idle , -1 , null );
269296 result .success (null );
270297 } else if (call .method .equals ("getCurrentPosition" )) {
271298 long pos = mIjkMediaPlayer .getCurrentPosition ();
@@ -277,12 +304,13 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
277304 result .success (null );
278305 } else if (call .method .equals ("seekTo" )) {
279306 final Integer msec = call .argument ("msec" );
307+ if (mState == completed )
308+ handleEvent (PLAYBACK_STATE_CHANGED , paused , -1 , null );
280309 mIjkMediaPlayer .seekTo (msec != null ? msec .longValue () : 0 );
281310 result .success (null );
282311 } else if (call .method .equals ("setLoop" )) {
283312 final Integer loopCount = call .argument ("loop" );
284- // todo update ijkplayer, add set loop count api
285- mIjkMediaPlayer .setLooping (loopCount != null && loopCount == 0 );
313+ mIjkMediaPlayer .setLoopCount (loopCount != null ? loopCount : 1 );
286314 result .success (null );
287315 } else if (call .method .equals ("setSpeed" )) {
288316 final Double speed = call .argument ("speed" );
0 commit comments