Android:播放歌曲时Seekbar进度错误?

时间:2016-09-08 09:23:25

标签: android seekbar

在我的Android应用程序中,当我正在播放音乐时,搜索栏会保持不变。音乐的计时器运行良好&音乐从搜索栏的触摸位置播放。

但问题是,在触发搜索栏时,搜索栏仍然没有移动。

任何人帮我找到解决方案..谢谢..

我的活动代码是....

public class Device_AudioPlayerActivity extends Activity  implements Runnable,
        OnClickListener, SeekBar.OnSeekBarChangeListener {

    Button btnBack;
    static Button btnPause;
    private Handler mHandler;
    Button btnNext;
    static Button btnPlay;
    static TextView textNowPlaying;
    static TextView textAlbumArtist;
    static TextView textComposer;
    static LinearLayout linearLayoutPlayer;
    SeekBar progressBar;
    static Context context;

    TextView textBufferDuration, textDuration;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getActionBar().hide();
        setContentView(R.layout.device_audio_player);
        context = this;
        progressBar=(SeekBar)findViewById(R.id.progressBar);
        init();
        progressBar.setMax(Device_SongService.mp.getDuration());
        new Thread().start();
        progressBar.setOnSeekBarChangeListener(this);
        progressBar.setEnabled(true);
        //-=--------------------------------------
    }

    private void init() {
        getViews();
        setListeners();
        progressBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.white), Mode.SRC_IN);
        Device_PlayerConstants.PROGRESSBAR_HANDLER = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Integer i[] = (Integer[]) msg.obj;
                textBufferDuration.setText(Device_UtilFunctions.getDuration(i[0]));
                textDuration.setText(Device_UtilFunctions.getDuration(i[1]));
                progressBar.setProgress(i[2]);

            }
        };
    }

    private void setListeners() {
        btnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Device_Controls.previousControl(getApplicationContext());
            }
        });

        btnPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Device_Controls.pauseControl(getApplicationContext());
            }
        });

        btnPlay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Device_Controls.playControl(getApplicationContext());
            }
        });

        btnNext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Device_Controls.nextControl(getApplicationContext());
            }
        });
    }

    public static void changeUI() {
        updateUI();
        changeButton();
    }

    private void getViews() {
        btnBack = (Button) findViewById(R.id.btnBack);
        btnPause = (Button) findViewById(R.id.btnPause);
        btnNext = (Button) findViewById(R.id.btnNext);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        textNowPlaying = (TextView) findViewById(R.id.textNowPlaying);
        linearLayoutPlayer = (LinearLayout) findViewById(R.id.linearLayoutPlayer);
        textAlbumArtist = (TextView) findViewById(R.id.textAlbumArtist);
        textComposer = (TextView) findViewById(R.id.textComposer);
        progressBar = (SeekBar) findViewById(R.id.progressBar);
        textBufferDuration = (TextView) findViewById(R.id.textBufferDuration);
        textDuration = (TextView) findViewById(R.id.textDuration);
        textNowPlaying.setSelected(true);
        textAlbumArtist.setSelected(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean isServiceRunning = Device_UtilFunctions.isServiceRunning(Device_SongService.class.getName(), getApplicationContext());
        if (isServiceRunning) {
            updateUI();
        }
        changeButton();
    }

    public static void changeButton() {
        if (Device_PlayerConstants.SONG_PAUSED) {
            btnPause.setVisibility(View.GONE);
            btnPlay.setVisibility(View.VISIBLE);
        } else {
            btnPause.setVisibility(View.VISIBLE);
            btnPlay.setVisibility(View.GONE);
        }
    }

    private static void updateUI() {
        try {
            String songName = Device_PlayerConstants.SONGS_LIST.get(Device_PlayerConstants.SONG_NUMBER).getTitle();
            String artist = Device_PlayerConstants.SONGS_LIST.get(Device_PlayerConstants.SONG_NUMBER).getArtist();
            String album = Device_PlayerConstants.SONGS_LIST.get(Device_PlayerConstants.SONG_NUMBER).getAlbum();
            String composer = Device_PlayerConstants.SONGS_LIST.get(Device_PlayerConstants.SONG_NUMBER).getComposer();
            textNowPlaying.setText(songName);
            textAlbumArtist.setText(artist + " - " + album);
            if (composer != null && composer.length() > 0) {
                textComposer.setVisibility(View.VISIBLE);
                textComposer.setText(composer);
            } else {
                textComposer.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            long albumId = Device_PlayerConstants.SONGS_LIST.get(Device_PlayerConstants.SONG_NUMBER).getAlbumId();
            Bitmap albumArt = Device_UtilFunctions.getAlbumart(context, albumId);
            if (albumArt != null) {
                linearLayoutPlayer.setBackgroundDrawable(new BitmapDrawable(albumArt));
            } else {
                linearLayoutPlayer.setBackgroundDrawable(new BitmapDrawable(Device_UtilFunctions.getDefaultAlbumArt(context)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        try {
            if (Device_SongService.mp.isPlaying() || Device_SongService.mp != null) {
                if (b)
                    Device_SongService.mp.seekTo(i);
            } else if (Device_SongService.mp == null) {
                Toast.makeText(getApplicationContext(), "Media is not running",
                        Toast.LENGTH_SHORT).show();
                seekBar.setProgress(0);
            }
        } catch (Exception e) {
            Log.e("seek bar", "" + e);
            seekBar.setEnabled(false);

        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void run() {
        int currentPosition = Device_SongService.mp.getCurrentPosition();
        int total = Device_SongService.mp.getDuration();

        while (Device_SongService.mp != null && currentPosition < total) {
            try {
                Thread.sleep(1000);
                currentPosition = Device_SongService.mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            progressBar.setProgress(currentPosition);
        }

    }
}

2 个答案:

答案 0 :(得分:1)

你必须在onprogresschnaged ....上使用线程。

@Override
public void onProgressChanged(SeekBar arg0, final int progress, boolean arg2) {
Thread thread = new Thread() {
                   @Override
                   public void run() {
                   try {
                        while(true) {
                           //here write your code
                            }                                     
                      } catch (Exception e) {
                       e.printStackTrace();
                      }
                    }
                  };

thread.start();
}

答案 1 :(得分:1)

查看this链接。

尝试使用正在更改的线程。