I am having a problem (I think) with overlapping threads playing music in my android mp3 player. I have a class SongList.java that lists and plays music when an item is clicked and a ArtistList.java that intents SongList to play songs by that specific artist. Two or more songs will play at the same time if they are not in the same activity.
How do I tell my playSong method to stop all threads but the most recent one?
You should have just one single Thread or service doing playback.
SongList and ArtistList should not start a new service. Rather you should have a PlaybackService you can pass a SongList or ArtistList as parameter / argument. If doing so, you should just have one single Thread doing playback and never run into that kind of trouble.
Have a look here:
http://developer.android.com/guide/topics/media/mediaplayer.html#mpandservices
They implement a Service that uses the build in MediaPlayer for audio playback. Next you can bind your MusicPlaybackService to an activity.
Here is an introduction:
http://developer.android.com/guide/components/bound-services.html
You can specify methods that the activity can invoke on your service with the help of Binder. So you could provide a method like playSongs(SongList songs) or playArtists(ArtistList list) that could be invoked by the activity (i.e. by clicking on a certain button). Since the service runs the playback (by using MediaPlayer) the service is responsible for Threading and playback. So if you call playSongs(SongList songs) of the service, the Service should stop MediaPlayer and restart MediaPlayer with the desired music file (provided by SongList). With this approach your MusicPlaybackService manages the playback and guarantees that only one music file is played simultaneously.
Related
I'm developing an application which works with information broadcasted by the default Android music player.
I have set up an IntentFilter and BroadcastReceiver in order to listen for metadatachanged or playstatechanged events of the Android music player:
IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.metachanged");
iF.addAction("com.android.music.playstatechanged");
Thus I'm able to pick up when the user changes a song or pauses/starts playing a song. However, I'm interested to know when the user seeks forward or backward through a song - I need to know how many seconds are skipped.
I don't think there's an intent action for seeking? Any ideas as to how I should go about detecting this?
Im working on a Music Player app, and all the music handled by the app are from streaming resources.
Everytime the user changes songs, i need to call reset and set a new dataSource. If the user rapidly switches songs, and the MediaPlayer is on the preparing state, the UI from my app freezes.
This only happens if i call reset and the media player is in the preparing state.
How can i prevent my app UI from freezing and getting an ANR?
PS: Im using prepareAsync(), not prepare().
See setOnPreparedListener. You can keep track of the state in your class through various listeners and avoid calling prepareAsync().
You might have better luck also calling stop() before reset().
http://developer.android.com/reference/android/media/MediaPlayer.html#setOnPreparedListener%28android.media.MediaPlayer.OnPreparedListener%29
I am developing an Android application in which I am showing a ListView of mp3 files from the sd card.
Now what I want is that when user clicks on any of the mp3 files it should start playing there on the same Activity. The code which I am using presently is not working for me to play audio files.
My code:
Uri uri = Uri.parse("/sdcard/music/sample.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
But this is something that I want. This is a screen Shot form android gingerbread
see this link here is a good example of getting all the music files and show in list and play when you click any item...
Display MusicList and play on itemClick
Another link
I suppose you should use another approach in case you want audio played in the same activity. I'd better initialize MediaPlayer object in this activity or as a background service.
In case of any questions feel free to ask.
UPDATE 1
In general case as I said you either need the MediaPlayer object in Activity or Music Service.
Now lets talk about controls.
You can simply put into your layout the block with controls and show/hide it when needed. MediaPlayer provides several convenient callbacks, so you will be able to update progressbar.
So, sum up:
Include controls into layout.
Include MediaPlayer into activity or make it work as a Service.
Bond controls to player.
???
PROFIT
I have a music player Activity in my app.
The problem is that, if the user switch to other Activity inside my app, the music keeps playing, but if later he returns to the music player Activity, everything resets(song's title, duration...) except the music itself.
Is there a way to make an activity keep running?(not by using a service).
Take a look here it might help you.
How to mute and unmute it on the onPause and onResume
Use the onPause() and onResume() for that.
Here's my problem: I start a media player, and if I start a new intent, or leave the app and go back to it..
mp.stop;
..wont work (mp is my media player variable).
is there anyway to fix this?
Bonus to whoever can tell me how to link it up to a widget.
You always must call mp.release() in your Activity's onDestroy(). It is also recommended to at least call mp.pause() in on onPause() Activity's method.
If you want playback to continue outside of Activity lifecycle (after onPause()/onStop()/onDestroy()) you have to create Service and play audio content from there.