Question

I'm migrating a code of a media player (aacdecoder) from an activity to a service. I wanna put this in service so the users can close the activity and the player still playing and when the user reopen it connects to the service and show his parameters. This player have a listener that receives things like title and genre of the music playing and the player buffer from the streaming. The problem is that my activity used to be an implementation of the listener for the events of this player, it used to receive on listener methods and update the textviews. Now, how will i handle this events while activity is closed and after it is reconnected?

These are the listener methods, according to the example in the player page.

public void playerStarted() {
    uiHandler.post( new Runnable() {
        public void run() {
            txtBufAudio.setEnabled( false );
            txtBufDecode.setEnabled( false );
            btnPlay.setEnabled( false );
            btnStop.setEnabled( true );

            txtStatus.setText( R.string.text_buffering );
            progress.setProgress( 0 );
            progress.setVisibility( View.VISIBLE );

            playerStarted = true;
        }
    });
}


/**
 * This method is called periodically by PCMFeed.
 *
 * @param isPlaying false means that the PCM data are being buffered,
 *          but the audio is not playing yet
 *
 * @param audioBufferSizeMs the buffered audio data expressed in milliseconds of playing
 * @param audioBufferCapacityMs the total capacity of audio buffer expressed in milliseconds of playing
 */
public void playerPCMFeedBuffer( final boolean isPlaying,
                                 final int audioBufferSizeMs, final int audioBufferCapacityMs ) {

    uiHandler.post( new Runnable() {
        public void run() {
            progress.setProgress( audioBufferSizeMs * progress.getMax() / audioBufferCapacityMs );
            if (isPlaying) txtStatus.setText( R.string.text_playing );
        }
    });
}


public void playerStopped( final int perf ) {
    uiHandler.post( new Runnable() {
        public void run() {
            btnPlay.setEnabled( true );
            btnStop.setEnabled( false );
            txtBufAudio.setEnabled( true );
            txtBufDecode.setEnabled( true );
            // txtStatus.setText( R.string.text_stopped );
            txtStatus.setText( "" + perf + " %" );
            progress.setVisibility( View.INVISIBLE );

            playerStarted = false;
        }
    });
}


public void playerException( final Throwable t) {
    uiHandler.post( new Runnable() {
        public void run() {
            new AlertDialog.Builder( AACPlayerActivity.this )
                .setTitle( R.string.text_exception )
                .setMessage( t.toString())
                .setNeutralButton( R.string.button_close,
                    new DialogInterface.OnClickListener() {
                        public void onClick( DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    }
                 )
                .show();

            txtStatus.setText( R.string.text_stopped );

            if (playerStarted) playerStopped( 0 );
        }
    });
}


public void playerMetadata( final String key, final String value ) {
    TextView tv = null;

    if ("StreamTitle".equals( key ) || "icy-name".equals( key ) || "icy-description".equals( key )) {
        tv = txtMetaTitle;
    }
    else if ("StreamUrl".equals( key ) || "icy-url".equals( key )) {
        tv = txtMetaUrl;
    }
    else if ("icy-genre".equals( key )) {
        tv = txtMetaGenre;
    }
    else return;

    final TextView ftv = tv;

    uiHandler.post( new Runnable() {
        public void run() {
            ftv.setText( value );
        }
    });
}

Should i make all this events send a broadcast from service to activity? Cannot this causeless overload the system with broadcasts? Please help me. Thanks in advance.

Was it helpful?

Solution

You can bind to the service and simply register some callbacks (which you define) so that the Service has something to call back to when events happen. Make sure to manage the registration and unregistration in the appropriate lifecycle methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top