Quick minor explanation of Android services? - android

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.

Related

simple foreground Android audio playback: shall I use MediaPlayer/Exoplayer in Service?

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.

When should JetCreator or SoundPool be used?

I'm trying to make an app and I would liek to try to implement the optimal solution for multiple, simutaneously playing, programmatically selected sound effects.
I really appreciate the help.
Cheers
Well it depends:
Mediaplayer:
Not good for simultaneous playing (you need multiple MP objects), the response time and especially the prepare() methode take a lot of time.
Therefore synchronizing is quite impossible.
SoundPool:
Good for small MP3.files (size limt ~1mb, ~30sec)
Easy to implement
A lot faster than Mediaplayer. Mostly used for "sounds" and not a for complete "piece of music"
Jet Player/JET Creator
Used for MIDI files, quite difficult to install (you need python, wx python in order to use JET creator ...).
Very fast response, good for simultaneous playing of several tracks!
Any further questions? do not hesitate to ask!
best regards

Playing game sounds

I need to play sound effects in my game such as jump, failed, shot etc.
So for this purpose I have selected SoundPool class. But there were some times when it lagged.
So I wonder what is correct way to use this class. I have tried to use play method in new thread, it seems work better but I don't know if I am correct.
Please advice how to use object of this class correctly for better performance.
Best way to implement Sound / Music into your game is by using Service component. Read this answer here for an example.

Android VPNService - how do I bring up the 'system-managed dialog' from my activity?

According to this tutorial the only way to disconnect the vpn service is to use the 'system-managed dialog'. This can be done manually by dragging down the notification bar and selecting it from there, however this is not a good solution for my problem.
I need to be able to bring this dialog up from within my app when a 'disconnect' button is pressed, however I can't find any documentation on how to do this.
Can anyone help?
Right now the only public and recommended way to disconnect is unfortunately just through the notification. The functions for disconnecting or even bringing up the dialog are otherwise hidden unfortunately.
Of course, that doesn't necessarily mean that they can't be accessed, but that way lies much pain. I haven't tried this in this specific case, but I have done this in the past, way back, with the music player to find out what was playing (for audio scrobbling). So if you absolutely must find a way to do this, no matter how hacky, then you could try this, but keep in mind that 'ere be dragons - this involves accessing a private API that you are not supposed to. That API could change on a whim between any two given builds of android (not just platform versions either), because it is supposed to be private and internal. You would need to make your code very resilient to failure here, and frankly it'll never be anything more than hacky and a PITA.
Right, all that said, see line 171 in this
That's what you want to do. Except you can't see the things you need, right? So you need this AIDL for the service (and possibly a bit of reflection too, I forget - haven't done this in a long time).
This blog post describes something similar, though not in much detail. It's hard to find things talking about, since it is an incredibly discouraged practice.
I think it is fine just to close tun descript and let the Service end. This will effectlivy end the VPN (but the app in question will still hold the permission to open a VPN again). The notification (key symbol) will also go away. If you are trying the VPNService of another app that might be tougher. Perhaps you should ask the author of that app for an API.

Efficient way to update mm:ss from a playing MP3 file

I'm re-inventing the wheel here, but as I'm a beginner programmer I'm curious as to the best way to do this...
As part of an Android app, I'm developing a very simple media player. It plays the file, and I want a m:s timer, so the user can see how far into the file they are (e.g. "04:56").
I'm probably missing the obvious, but what's the best way to do this?
One way would be to generate a new thread which sleeps for 1000ms, then calls MediaPlayer.getCurrentPosition() and updates the UI. However, this seems slightly ridiculous - my thread isn't guaranteed to come back every second, so I'm going to be displaying an inaccurate time. There must be a better way of doing this?
Educate me (or link for me).
Just use Handler#postDelayed() or View#postDelayed() to do your once-a-second updates. This saves you from having to fork a thread, let alone clean up after it.
Here is a project using that specific technique for your desired purpose: updating a time counter based on MediaPlayer progress.
One alternative might be to start an independent timer when the music starts. Have it "sync" by calling MediaPlayer.getCurrentPosition() once every five seconds or so to ensure that the time remains accurate.

Categories

Resources