I want to have a method that plays a specific audio file and only returns when it is finished playing.
Asked
Active
Viewed 341 times
0
-
you can implement a callback with interface. [LINK](http://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android) – Samuel Aug 19 '11 at 10:40
1 Answers
1
You shouldn't, it will block a thread and you'll kill CPU for nothing.
Use this: http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html
Here for how to use it: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html

GrandMarquis
- 1,913
- 1
- 18
- 30
-
As to why I need the function to be blocking: Blocking calls are more readable and more easy to code. In my case, to write the code with async calls would be extremely tiring. I would even sacrifice wasting CPU for it. How can I do it? As an alternative, is it possible to execute the code that makes the blocking calls in a sandbox/just-in-time without wasting processing power? Or is there a tool to convert code with sync calls into async code? – Arnulf Aug 19 '11 at 14:10
-
Learn to code well at first, i know it might be more difficult to put a listener than to make it the trash way but you must make the effort to code well. – GrandMarquis Aug 19 '11 at 14:13
-
The part of the code that calls the playing function (many times) is to be read and modified by lay people, so I want to keep it readable. So what are my options? Can I make some scripting interface that offers blocking functions? – Arnulf Aug 19 '11 at 15:40
-
Can I make some scripting interface that offers blocking functions? What does that mean? – GrandMarquis Aug 19 '11 at 16:13
-
I meant to say, is it possible to load parts of the code at runtime from a file/let the user type the code in on the phone (including calling blocking functions), and then execute this user-typed code? – Arnulf Aug 19 '11 at 20:41
-