服务结构问题的活动

时间:2014-04-29 20:20:19

标签: android service android-activity broadcastreceiver

我正在将媒体播放器的代码(aacdecoder)从活动迁移到服务。我希望将其投入使用,以便用户可以关闭活动并且播放器仍在播放,当用户重新打开它时,它会连接到服务并显示他的参数。该播放器具有收听者,其从流媒体接收诸如音乐播放的标题和类型以及播放器缓冲器之类的内容。问题是我的活动曾经是该玩家事件的监听器实现,它曾用于接收侦听器方法并更新文本视图。现在,我将如何在活动关闭和重新连接后处理此事件?

根据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 );
        }
    });
}

我应该让所有这些事件从服务发送广播到活动吗?这个系统无法通过广播无效过载吗? 请帮我。提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以绑定到服务并简单地注册一些回调(您定义的),以便服务在事件发生时有回调的内容。确保在适当的生命周期方法中管理注册和注销。