0

我的搜索栏有问题,当我使用媒体播放器播放音频时,搜索栏不会从它开始。我不明白为什么搜索栏不“刷新”进度。

谢谢你的帮助。

下面我发布代码,我不明白我什么时候错了。

MediaPlayer  player=new MediaPlayer();
SeekBar seekBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.mymp3);
        player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
        player.prepare();

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException 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();
    }

    Button pausa=(Button)findViewById(R.id.button2);  
    pausa.setOnClickListener(new OnClickListener(){  
                @Override  
                public void onClick(View arg0) { 
                    player.pause();
                }  
            }); 
   Button vai=(Button)findViewById(R.id.button3);  
    vai.setOnClickListener(new OnClickListener(){  
                @Override  
                public void onClick(View arg0) {
                    player.start();
                }  
            });

    int tutto=player.getDuration();

    seekBar = (SeekBar)findViewById(R.id.seekBar1);

    seekBar.setMax(tutto);
    seekBar.setProgress(player.getCurrentPosition() / 1000);
}
4

2 回答 2

0

因为您设置了进度onCreate()并且只调用了一次 - 您猜对了,当Activity创建时。我不是很熟悉,MediaPlayer所以我不知道是否有某种更新侦听器,但是您需要某种计时器,它每秒左右更新一次进度。

于 2014-03-05T23:52:37.033 回答
0

An easy way you can do it is to create a Timer object that fires every second.

Inside the code of the timer you call

seekBar.setProgress(player.getCurrentPosition() / 1000);

Additionally, you may want to implement setOnCompletionListener to get notified when the playback has reached it's end so you can stop the timer. The timer should be stopped either if the playback reached it's end, the applications finishes, or you stop the music manually (in response to a stop button, for example)

于 2014-03-05T23:56:04.437 回答