android lyric 同步滚动显示

    技术2022-05-20  38

    前面的一篇已经实现了滚动,但是同步的时候还是会有一些问题,就是拖动seekbar的时候线程还在sleep,造成不同步。修改起来也容易,就是将sleep换成wait,在seekbar拖动的时候就notify他就可以了。

    首先定义lock及标志:

    boolean wait = false; private final Object lock = new Object();  

    在seekbar中使用

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { wait = true; if (mLyricThread != null) { synchronized (lock) { // System.out.println(" wait = true;"); lock.notifyAll(); } } } }  

    最好修改更新线程:

    class UIUpdateThread implements Runnable { // long time = 1; // 开始 的时间,不能为零,否则前面几句歌词没有显示出来 public void run() { try { while (musicPlayer != null && musicPlayer.isPlaying()) { long sleeptime = lyricView.updateIndex(musicPlayer .getCurrentPosition() + 10); mHandler.post(mUpdateResults); // System.out.println(" wait = false;"); if (sleeptime != -1) { if (!wait) { wait = false;// 这个标志要放在wait前面防止wait过程中被置位 synchronized (lock) { //System.out.println(" wait = false;"); lock.wait(sleeptime); } } } } } catch (InterruptedException e) { e.printStackTrace(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } return; } } Handler mHandler = new Handler(); Runnable mUpdateResults = new Runnable() { public void run() { lyricView.invalidate(); // 更新视图 } };  

    还有一点需要说明的是        long sleeptime = lyricView.updateIndex(musicPlayer

                                .getCurrentPosition() + 10);

    如果getCurrentPosition(),得到的是0,则前面的几句歌词没有显示出来,因此就加上10ms,同时提前10ms显示也正合适。

    上一篇

    http://blog.csdn.net/piaozhiye/archive/2011/04/13/6320066.aspx


    最新回复(0)