android - Android 媒体播放器上用于搜索栏的线程

标签 android

如何创建一个线程,每秒检查歌曲位置并根据歌曲位置移动搜索栏。我使用了打击代码,它只播放两首歌..然后它就从我的应用程序中消失了

public class setp implements MediaPlayer.OnPreparedListener  
         {

            public void onPrepared(MediaPlayer mps) {
                // TODO Auto-generated method stub
                seekbar.setMax(mp.getDuration());
                 new Thread(new Runnable() {

                  public void run() {
                          while(mp!=null && mp.getCurrentPosition()<mp.getDuration())
                          {
                              seekbar.setProgress(mp.getCurrentPosition());
                              Message msg=new Message();
                              int millis = mp.getCurrentPosition();

                              msg.obj=millis/1000;

                               try {
                                   Thread.sleep(100);
                               } 
                               catch (InterruptedException e) {
                                  e.printStackTrace();
                               }
                          }
                  }
          }).start();
            }

最佳答案

下面的代码工作正常

public class setp implements MediaPlayer.OnPreparedListener {

    public void onPrepared(MediaPlayer mps) {
        // TODO Auto-generated method stub
        seekbar.setMax(mp.getDuration());
        System.out.println("curpos" + mp.getCurrentPosition());
        new Thread(new Runnable() {
            public void run() {
                try {
                    while (mp != null && mp.getCurrentPosition() < mp.getDuration()) {
                        seekbar.setProgress(mp.getCurrentPosition());
                        Message msg = new Message();
                        int millis = mp.getCurrentPosition();

                        msg.obj = millis / 1000;

                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            System.out.println("interrupt exeption" + e);
                        }
                    } // end while
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("my Exception" + e);
                }
            }
        }).start();
    }
}

关于android - Android 媒体播放器上用于搜索栏的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9702613/

相关文章:

android - 插桩测试是否有可能为库模块创建覆盖率?

java - Android 用户界面停止响应

android - 使用自定义SurfaceView和线程进行Android游戏编程(示例)

java - 来自 RecyclerView 的 Cloud Firestore 文档的句柄 ID

java - 如何有效管理Android应用程序的内存(堆)

android - 如何让带有渐变的矢量可绘制对象在 < API 24 中工作?

android - 如何从我的 Android 应用程序访问用户安装的 key 和证书?

android - 我可以在 Ad 关闭/onAdDismissed - AdMob 时调用 loadMethod 吗?

java - 如何根据屏幕尺寸动态改变高度?

android - 为什么 9 补丁在我的应用程序中根本不起作用?