0

在我的 android 应用程序中,我想流式传输一个 url 并播放。我想在单击播放按钮时显示进度条,然后显示暂停按钮。我使用 Progressbar 是因为向用户指示那里正在发生某些事情,因为在 Mediaplayer 中播放时存在延迟。但问题是播放按钮显示 1 秒,然后在一小部分时间内显示进度条并同时更改到暂停按钮。当用户单击 paly 按钮时,如何显示进度条。代码如下

      holder.videoPlayVoiceBtn.setOnClickListener(new OnClickListener() {

                public void onClick(final View v) {
                    // videoInfo element = (videoInfo) videos.get(position);
                    // element.setPlayButton((Button) v);
                    // element.setPlaying(true);


                    LinearLayout relativeLayout = (LinearLayout) v.getParent();
                    RelativeLayout layout = (RelativeLayout) relativeLayout
                            .getParent();
                    final ProgressBar bar = (ProgressBar) layout
                            .findViewById(R.id.progressBarVoice);
                    bar.setVisibility(View.VISIBLE);
                     SeekBar mSeekBar = (SeekBar) relativeLayout
                            .findViewWithTag(position);
                     final SeekBar SeekBar = mSeekBar;


                    final Button playBtn = (Button) relativeLayout
                            .findViewWithTag(position + "b");



                        if (Utilities.message_player != null) {



                            if (Utilities.message_player.isPlaying()) {
                                bar.setVisibility(View.GONE);
                                Utilities.message_player.pause();
                                holder.username.setKeepScreenOn(false);
                                videos.get(position).setCurrentDuration(
                                        Utilities.message_player
                                                .getCurrentPosition());
                                playBtn.setBackgroundResource(R.drawable.play_button_style);
                                pause_check = true;


                            } else if (!Utilities.message_player.isPlaying()) {
                                bar.setVisibility(View.VISIBLE);
                                playBtn.setVisibility(View.GONE);

                                Utilities.message_player.seekTo(videos.get(
                                        position).getCurrentDuration());
                                playBtnPrev
                                        .setBackgroundResource(R.drawable.pause_btn_style);

//                              
                                        playVoice(videos.get(position)
                                                .getVoice_URL(),
                                                videos.get(position)
                                                        .getDuration(),
                                                        SeekBar, duration, playBtn,
                                                position, v, 0, bar);
//                              
                                Utilities.message_player.start();

                                pause_check = true;

                            } else {
                                bar.setVisibility(View.GONE);
                                playBtn.setVisibility(View.VISIBLE);
                                Utilities.message_player.stop();
                                holder.username.setKeepScreenOn(false);
                                // Utilities.message_player.reset();
                                Utilities.message_player = new MediaPlayer();
                                pause_check = false;
                            }

                            playBtnPrev
                                    .setBackgroundResource(R.drawable.play_button_style);

                            isPlayCheck = true;
                        } else {
                            // bar.setVisibility(View.VISIBLE);
                            // playBtn.setVisibility(View.GONE);
                            playBtnPrev
                                    .setBackgroundResource(R.drawable.pause_btn_style);
                            isPlayCheck = false;
                        }
                    }

                    if (pause_check == true) {

                    } else {
                        dummy_voice_button = playBtn;
                        bar.setVisibility(View.VISIBLE);
                        playBtn.setVisibility(View.GONE);
                        if (Utilities.message_player == null)
                            Utilities.message_player = new MediaPlayer();

                                playVoice(videos.get(position).getVoice_URL(),
                                        videos.get(position).getDuration(),
                                        SeekBar, duration, playBtn, position,
                                        v, 1, bar);

                        prevPosition = position;

                    }
                }
            });




   private void playVoice(String url, final int voiceduration,
            final SeekBar mSeekBar, final TextView durationtv,
            final Button playBtn, final int posToPlay, final View view,
            int valInt, final ProgressBar bar) {

        if (Utilities.message_player != null) {
            Utilities.message_player
                    .setOnCompletionListener(new OnCompletionListener() {

                        public void onCompletion(MediaPlayer mp) {
                            Log.e("Play voice", "setOnCompletionListener");

                            if (play_task != null) {
                                prevPosition = -1;
                                playBtn.setBackgroundResource(R.drawable.profile_play_button_style);
                                durationtv.setText(""
                                        + videos.get(posToPlay).getDuration());
                                LinearLayout relativeLayout = (LinearLayout) view
                                        .getParent();
                                if (((SeekBar) relativeLayout
                                        .findViewWithTag(posToPlay)) != null)
                                    ((SeekBar) relativeLayout
                                            .findViewWithTag(posToPlay))
                                            .setProgress(0);

                            }



                        }
                    });
            Utilities.message_player
                    .setOnPreparedListener(new OnPreparedListener() {

                        public void onPrepared(MediaPlayer mp) {
                            playBtn.setVisibility(View.VISIBLE);
                            playBtn.setBackgroundResource(R.drawable.pause_btn_style);
                            bar.setVisibility(View.GONE);

                        }
                    });


                try {
                    Log.e("Play voice", "message_player.isnotPlaying()");
                    Utilities.message_player.reset();
                    Utilities.message_player.setDataSource(url);
                    Utilities.message_player.prepare();

                    Utilities.message_player.start();
                    holder.username.setKeepScreenOn(true);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }
        }
    }
4

0 回答 0