I was looking at the Media Player application found in the Android 2.1 platform samples. I gave a link of the URL for the media file i wanted to play. Now it played fine, but the app gave a "taking too long" warning dialog, meaning too much work was been done on the UI. Is there a better approach to prepare the file async, when streaming from the internet, so as not to bug the UI thread more then needed.
Thanks.
PrepareAsync() is performed on another thread (hence the name). I don't think the problem is here.
I use Service to for player. It doesn't require UI so you can be sure it's not blocking ui and it's not killed when you leave activity.
Related
I need to embed simple inline audio playback into a list view, where the user would just click play/pause/stop on the items one by one, in the foreground
The playback back would stop when leaving the screen (activity or fragment).
So the use case is similar to those usual chat apps.
Ideally, I was looking for a simple UI widget I could embed into a list view and pass a mp3/wav/ogg url (local files for a start).
Trying to research for such a thing I found people implementing either an ultra simplistic player with no ui at all or a fully fledged bound Service (which I tried and it works, but it seems like an overkill for a simple foreground playback). When simply embedding a MediaPlayer + MediaController into a fragment, the android monitor was complaining -I think- about doing too much on the UI thread.
So, is there such a player widget to minimise boilerplate code?
Would ExoPlayer be a good choice? ( I see it's powerful but may be even more of an overkill - unless it has some handy utility class, which I missed so far)
What would be the simplest, quickest, safest, most elegant solution?
I'd be grateful for any pointers or enlightenment.
In the meantime I found some potential candidates as audio player widgets:
Uncodin: looks good but not properly packaged, as a gradle dependency https://github.com/Uncodin/Android-Common/blob/master/UncodinCommon-standalone/src/in/uncod/android/media/widget/AudioPlayerView.java
https://github.com/Cleveroad/MusicBobber
and more on android-arsenal.com
I've yet to try these but wouldn't mind hearing suggestions about concrete solutions or how to implement or where to look.
I am doing a streamed musicplayer for android.
I have tried with MediaPlayer
mp.setDataSource("http://www.myServerPage.com/songUrl.mp3");
mp.prepare();
mp.start();
and it works but not good.
The prepare state is very slow and often the application is freezing.
I have searched a lot on forums and tutorials and as far as I can see this is the common way of doing it.
Do anyone know is it is another way of opening up a stream to play music from a server or have any good advices, maybe in NDK or by some other inputstream?
Thanks for the help.
/micke
The Media Playback article is pretty clear about how to deal with remote media sources: call prepareAsync() and set a listener to get notified when the source is ready for playing.
The reason for this is exactly what you're experiencing:
(...) the call to prepare() can take a long time to execute, because it
might involve fetching and decoding media data. So, as is the case
with any method that may take long to execute, you should never call
it from your application's UI thread. Doing that will cause the UI
to hang until the method returns, which is a very bad user experience
and can cause an ANR (Application Not Responding) error.
I suggest you go over the mentioned article for some more information and pointers that may help you with further development.
I apologize up-front if this is a really obvious question since I'm pretty sure I'm over-thinking it.
Anyway, I'm building a music player in Android which basically streams playlists from 8Tracks and, as my understanding is, the best-practice here would be using a foreground service. I've already built everything and it's completely functional sans service, however, I'm a little confused as to how to implement the service.
As is, I have my Player class, which controls the MediaPlayer, extending Service but I'm not quite sure where to progress from there. I've tried binding it to my Engine class which reconciles all the background work with the UI, however I keep getting a ServiceConnectionLeaked exception, and I'm fairly certain this is simply the wrong approach.
I would appreciate any help at all; really a nod in the right direction is all I need. Thanks for your time!
I think that you can directly control MediaPlayer without additional Player class. As I know MediaPlayer is already a service so you can use start and stop methods to play something.
I've recently begun experimenting with a mediaPlayer instance in my Android app. I implemented a couple different beeps for feedback to the user. Now, when I implemented an audioTrack (for a completely different purpose), I discovered that it pretty much sets itself up as a separate thread automatically (as far as I can tell). It certainly appears as a separate thread when I run my code in the debugger:
Thread [<17> AudioTrackThread] (Running)
My question is: Does mediaPlayer do something similar? My first guess is that it does not -- or I would see thread descriptions in the debugger, right??
Anyway, now I've got to questions:
1) Can I set up my mediaPlayers as separate threads and still have them work right?
2) Does it make sense even to try it?
Thanks,
R.
MediaPlayer will still work like its supposed to in a thread, I've done that before so I could still do everything asynchronously without using the callbacks. I wouldn't spawn more than one of them though they use a lot of resources.
Hello
In my android application i am playing live rtsp links.
The issue is at times if the player gets struck somewhere then the videoplayer doesnot exit even if we press back.
The only option will be to close the program from task manager.
Is there any way that i can make it better?
Please share your valuable suggestions.
Thanks in advance :)
If it is your code getting stuck, be sure that you are not doing anything (especially network operations) that can block or take more than a few milliseconds in the UI thread - you need to be doing that in loopers or services.
If it's the underlying android video player engine that is hanging, sorry, no idea.