-3

In the delphi source code i see for exemple this procedure :

procedure TAndroidVideo.RetreiveVideoSize;
var
  MediaPlayer: JMediaPlayer;
begin
  MediaPlayer := TJMediaPlayer.JavaClass.init;
  MediaPlayer.setDataSource(StringToJString(FileName));
  MediaPlayer.prepare;
  FVideoSize := TSize.Create(MediaPlayer.getVideoWidth, MediaPlayer.getVideoHeight);
  MediaPlayer := nil;
end;

that can be run from the main thread. but i think it's a mistake and this procedure must be run from the android UI thread no ? or i miss something ?

zeus
  • 12,173
  • 9
  • 63
  • 184
  • In android the main thread is the same as the ui thread – ashkhn Feb 05 '17 at 20:01
  • @akash93 actually, it is not always the same thread, and that does cause problems sometimes. Embarcadero is planning on addressing that in a future version of Delphi. – Remy Lebeau Feb 06 '17 at 10:41
  • @RemyLebeau i don't know if it's good to addressing that in a futur version of delphi because now their is lot of code made that depend of this "wrong" behavior. if they do so, i hope they will highly test it before to release ... – zeus Feb 06 '17 at 12:21
  • Is it literally "procedure TAndroidVideo.RetreiveVideoSize", that would be very sad as it contains a typo. – penarthur66 Feb 06 '17 at 12:56
  • @penarthur66 ... aah yes you are right it's contain a typo :( – zeus Feb 06 '17 at 13:33
  • @RemyLebeau I'm assuming you are talking about delphi? I realize they are different in delphi after reading the comments and the answer below. But on android as far as I'm aware they're always the same? – ashkhn Feb 07 '17 at 01:24

1 Answers1

1

According to the docs for MediaPlayer, The call to MediaPlayer.prepare should not be executed on the UI thread. If this code might be running on the UI thread, the call should be changed to MediaPlayer.prepareAsync.

P.S. In Android, the UI thread and the main thread are the same thing. See, for instance, this post.

P.P.S. I didn't realize that in Delphi, main thread was something different. My guess, though, is that it's just as bad an idea to block the main thread in Delphi as it to block the UI/main thread in Android. The code you posted will block whatever thread is executing it until prepare returns.

Regarding your question about using the async methods (prepareAsync, etc.): the call-backs (OnPreparedListener.onPrepared(MediaPlayer), etc), will happen on the UI thread. the thread that called the original asynchronous method, whatever thread that is (which need not be the UI thread but probably needs to be a HandlerThread). This is not bad, because in the call-back handler methods, you typically either call another async method or call a low-latency method (such as MediaPlayer.start).

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thank Ted ! yes on android main and ui thread are the same, but on delphi it's different (on delphi main thread = the main thread of the delphi .so) ... but the remark i have is that even if you run it from a different thread that the android UI thread all event like OnPreparedListener, OnErrorListener, etc will be fire in the android UI thread and not in the thread who call prepare or asyncPrepare :( this why i ask if not everything must not be dore from the UI thread – zeus Feb 05 '17 at 20:07
  • thanks! however i confirm you that the call-backs (OnPreparedListener.onPrepared(MediaPlayer), etc), will NOT happen on the thread that called the original asynchronous method :( i just check and it's different thread :( maybe because the thread who called the original asynchronous method don't have it's own Looper running (main UI thread by default has a Looper running) – zeus Feb 05 '17 at 20:31
  • @loki - Strange. That seems to be different from what I read. Do the call-backs always happen on the main looper thread? If so, that's not bad, because typically you either then make another async call or call a method that doesn't block (such as `start()`). – Ted Hopp Feb 05 '17 at 21:43
  • yes always happen on the main ui thread. no it's not so bad, but this just let me ask if in this way it's not better to do everything from the main ui thread are all event will be fire in any case on the main ui thread. maybe the fact that event are raise on the main ui thread is because to the delphi framework this i don't know – zeus Feb 05 '17 at 21:48