在android

时间:2015-04-26 12:09:12

标签: android android-mediaplayer

我在做什么:我正在使用网址播放媒体播放器

发生了什么:一开始,屏幕就像没有响应一样,并且自歌曲缓冲发生以来一直挂起。

问题:

  1. 如何克服这个?
  2. 我是否需要在此处使用Async任务而不是处理程序? ......如果 那么如何修改我发布的代码
  3. 或者我应该只需要在此处显示进度对话框。在处理程序? ......有可能吗? ..如果是这样的话?
  4. 代码:

    protected void newValue(TextView txtSrcId, TextView txtDstId) {
                // TODO Auto-generated method stub
    
    
                do {
                    current = mediaPlayer.getCurrentPosition();
                    System.out.println("duration - " + duration + " current- "
                            + current);
                    int dSeconds = (int) (duration / 1000) % 60 ;
                    int dMinutes = (int) ((duration / (1000*60)) % 60);
                    int dHours   = (int) ((duration / (1000*60*60)) % 24);
    
                    int cSeconds = (int) (current / 1000) % 60 ;
                    int cMinutes = (int) ((current / (1000*60)) % 60);
                    int cHours   = (int) ((current / (1000*60*60)) % 24);
    
    
                    if(dHours == 0){
                        txtSrcId.setText(String.format("%02d:%02d", cMinutes, cSeconds));
                        txtDstId.setText(String.format("%02d:%02d", dMinutes, dSeconds));
                        //txtSrcId.setText("jodwjdwudwudguwdgwugduwgduwgduwgd");
                    }else{
                        txtSrcId.setText(String.format("%02d:%02d:%02d",cHours, cMinutes, cSeconds));
                        txtDstId.setText(String.format("%02d:%02d:%02d",dHours, dMinutes, dSeconds));
                        //txtSrcId.setText("jodwjdwudwudguwdgwugduwgduwgduwgd");
                    }
    
                    try{
                        Log.d("Value: ", String.valueOf((int) (current * 100 / duration)));
                        if(seekBarProgress.getProgress() >= 100){
                            break;
                        }
                    }catch (Exception e) {}
                }while (seekBarProgress.getProgress() <= 100);
            }
    
    
            private Runnable onEverySecond = new Runnable() {
                @Override
                public void run(){
                    if(true == running){
                        if(seekBarProgress != null) {
                            seekBarProgress.setProgress(mediaPlayer.getCurrentPosition());
                        }
    
                        if(mediaPlayer.isPlaying()) {
                            seekBarProgress.postDelayed(onEverySecond, 1000);
                            //updateTime();
                            newValue(txtSrcId,txtDstId);
                        }
                    }
                }
            };
    
            public void play() {
                try {
                    mediaPlayer.setDataSource(url);
                    mediaPlayer.prepare();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer.start();
                seekBarProgress.postDelayed(onEverySecond, 1000);
    
            } 
    
    
            @Override
            public void onPrepared(MediaPlayer arg0) {
                // TODO Auto-generated method stub
                duration = mediaPlayer.getDuration();
                seekBarProgress.setMax(duration);
                seekBarProgress.postDelayed(onEverySecond, 1000);
            }
    
    
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                /** Method which updates the SeekBar secondary progress by current song loading from URL position*/
                seekBarProgress.setSecondaryProgress(percent);
            }
    

    注意: 如果需要更多代码,请告诉我我将编辑问题

1 个答案:

答案 0 :(得分:0)

您正在寻找MediaPlayer#prepareAsync

  

以异步方式准备播放器以进行播放。设置数据源和显示表面后,您需要调用prepare()或prepareAsync()。对于流,您应该调用prepareAsync(),它会立即返回,而不是阻塞,直到缓冲了足够的数据。

使用此metod而不是prepare。当媒体源准备好通过MediaPlayer.OnPreparedListener.html播放时,您将收到通知。