Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a streaming music app using the media player class in a service. I call the service from the main class to start or stop the music. When I press "play" the music doesn't start instantly ( music is streamed, this is why loading animation is needed ).Other then the 5-10 sec wait time the app works fine. I know that it's streaming, and buffering and that takes time so I've decided to implement a loading animation. My problem is: I start the loading animation, but when do I stop it. How do I know that the music is playing? Note that when mediaplayer.start() is called music doesn't play instantly and after that call if I call the method isPlaying() it returns true, but you still can't hear the music.

My code:

private void buttonPlayClick() {
                startScroll();
        buttonPlay.setImageResource(R.drawable.mp_play_hover);
        startService(playbackServiceIntent);
    }

Service:

public int onStartCommand(Intent intent, int flags, int startId) {
    if (!mediaPlayer.isPlaying()) {
        mediaPlayer.start();
        super.onCreate();
    }
    return START_STICKY;
}


onCreate()
{
mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer
                .setDataSource("xyz");
    } catch (IllegalArgumentException e) {

    } catch (IllegalStateException e) {

    } catch (IOException e) {

    }
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.prepare();
    } catch (IllegalStateException e) {

    } catch (IOException e) {

    } // MediaPlayer starts to load data from URL 
    mediaPlayer.setOnCompletionListener(this);
    notifyMe();
 }
Posted
Updated 25-Jul-11 20:14pm
v2
Comments
[no name] 25-Jul-11 10:44am    
Use Code block to format your code (not the inline code block).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900