歌曲暂停时搜索栏重置为0

时间:2019-01-31 19:41:10

标签: android

我要创建一个音乐播放器应用程序,每当我暂停歌曲搜索条和其中包含的歌曲重置当前时间为0,但它已暂停在当我按下播放时恢复一个TextView。

这是代码中的我的搜索条和TextView的在MainActivity

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

        new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            seekBar.setProgress(getCurrentPosition());
        }
    },0,1);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            if (fromUser) {
                seekTo(progress);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

public void currentSongTime() {

    int songCurrentTime = getCurrentPosition();

    @SuppressLint("DefaultLocale")
    String currTime = String.format("%2d:%02d",
            TimeUnit.MILLISECONDS.toMinutes(songCurrentTime),
            TimeUnit.MILLISECONDS.toSeconds(songCurrentTime) -
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(songCurrentTime))
    );
    startTime.setText(currTime);
}

和这是我MusicService类

的代码
public class MusicService extends Service implements
    MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
    MediaPlayer.OnCompletionListener {


private MediaPlayer player;
private ArrayList<Song> songs;
private Random rand;
public boolean shuffleOn;
public int songPosn;
NowPlaying nowPlaying;
Song currentSong;

private final IBinder musicBind = new MusicBinder();

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return musicBind;
}

@Override
public boolean onUnbind(Intent intent){

    return false;
}

@Override
public void onCompletion(MediaPlayer mp) {

    if(player.getCurrentPosition()>0){
        mp.reset();
        playNext();
    }
}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {

    mp.reset();
    return false;
}

@Override
public void onPrepared(MediaPlayer mp) {

    mp.start();
    nowPlaying.updateTitleArtistAndPlayButton(currentSong);
}

public void onCreate(){

    super.onCreate();
    songPosn=0;
    player = new MediaPlayer();

    rand = new Random();

    initMusicPlayer();
}

public void initMusicPlayer(){

    player.setWakeMode(getApplicationContext(),
            PowerManager.PARTIAL_WAKE_LOCK);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);

    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnErrorListener(this);

}

public void setList(ArrayList<Song> theSongs){
    songs=theSongs;
}

public class MusicBinder extends Binder {
    public MusicService getService() {
        return MusicService.this;
    }
}

public void playNext(){

    if (shuffleOn) {
        int newSong = songPosn;
        while (newSong == songPosn) {
            newSong = rand.nextInt(songs.size());
        }
        songPosn = newSong;
    } else {
        songPosn++;
        if (songPosn >= songs.size()) songPosn = 0;
    }
    playSong();
}

public void playPrev(){
    songPosn--;
    if(songPosn <0) songPosn=songs.size()-1;
    playSong();
}

public void setShuffle(){
    if (shuffleOn) shuffleOn = true;
    else shuffleOn = false;
}

public void playSong(){

    player.reset();
    Song playSong = songs.get(songPosn);
    currentSong = playSong;
    long currSong = playSong.getID();
    Uri trackUri = ContentUris.withAppendedId(
            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            currSong);
    try{
        player.setDataSource(getApplicationContext(), trackUri);
    }
    catch(Exception e){
        Log.e("MUSIC SERVICE", "Error setting data source", e);
    }
    player.prepareAsync();

}

public void setSong(int songIndex, NowPlaying nowPlaying){
    songPosn=songIndex;
    this.nowPlaying = nowPlaying;
}

public int getPosn(){
    return player.getCurrentPosition();
}

public int getDur(){
    return player.getDuration();
}

public boolean isPng(){
    return player.isPlaying();
}

public void pausePlayer(){
    player.pause();
}

public void seek(int posn){
    player.seekTo(posn);
}

public void go(){
    player.start();
    }
}

谢谢

编辑

@Override
public int getCurrentPosition() {
    if (musicSrv != null && musicBound && musicSrv.isPng())
        return musicSrv.getPosn();
    else return 0;
}

0 个答案:

没有答案