java - 我想在播放音乐并且按钮显示 "play"后将搜索栏重置为开始位置

标签 java android

我想在播放音乐并且按钮显示“播放”后将搜索栏重置为开始位置......代码的问题是搜索栏在播放完音乐后停留在结束位置并且按钮仍然存在显示“暂停”...

这是我的代码

ekdanta.java

public class ekdanta extends AppCompatActivity implements Runnable, View.OnClickListener,SeekBar.OnSeekBarChangeListener {
   TextView tv4;
   Button b9, b10,but19;
   int count = 0;
   MediaPlayer play;
   SeekBar seek_bar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ekdanta);
    tv4 = (TextView) findViewById(R.id.textView9);
    tv4.setTextSize((float)21.5);
    tv4.setText(Html.fromHtml(getString(R.string.thirteen)));
    b9 = (Button) findViewById(R.id.b9);
    b10 = (Button) findViewById(R.id.b10);
    seek_bar = (SeekBar) findViewById(R.id.seekBar);
    seek_bar.setOnSeekBarChangeListener(this);
    seek_bar.setEnabled(false);
    but19 = (Button) findViewById(R.id.button19);
    but19.setOnClickListener(this);
}

public void run() {
    int currentPosition= play.getCurrentPosition();
    int total = play.getDuration();
    while (play!=null && currentPosition<total) {
        try {
            Thread.sleep(1000);
            currentPosition= play.getCurrentPosition();
        } catch (InterruptedException e) {
            return;
        } catch (Exception e) {
            return;
        }
        seek_bar.setProgress(currentPosition);
    }
}

public void onClick(View v) {
    if (v.equals(but19)) {
        if (play == null) {
            play = MediaPlayer.create(getApplicationContext(), R.raw.ekadanta);
            seek_bar.setEnabled(true);
        }
        if (play.isPlaying()) {
            play.pause();
            but19.setText("Play");
        } else {
            play.start();
            but19.setText("Pause");
            seek_bar.setMax(play.getDuration());
            new Thread(this).start();
        }
    }
}


@Override
protected void onPause() {
    if(play!=null){
        play.stop();
    }
    super.onPause();
}

public void increase(View inc) {
    count++;
    if (count == 1) {
        tv4.setTextSize(25);
    } else if (count == 2) {
        tv4.setTextSize(30);
    } else if (count >= 3) {
        count = 3;
        tv4.setTextSize(40);
    }
}

public void decrease(View dec) {
    count--;
    if (count <= 0) {
        tv4.setTextSize((float)21.5);
        count = 0;
    }
    if (count == 1) {
        tv4.setTextSize(25);
    } else if (count == 2) {
        tv4.setTextSize(30);
    } else if (count == 3) {
        tv4.setTextSize(40);
    }
}

@Override
public void onProgressChanged(SeekBar seek_bar, int progress, boolean fromUser) {
    try{
        if(play.isPlaying()||play!=null){
            if (fromUser)
                play.seekTo(progress);
        }
        else if(play==null){
            Toast.makeText(getApplicationContext(),"First Play", Toast.LENGTH_SHORT).show();
            seek_bar.setProgress(0);
        }
    }
    catch(Exception e){
        Log.e("seek bar",""+e);
        seek_bar.setEnabled(false);
    }
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
}

最佳答案

MediaPlayeronCompletionListener(),你可以使用它。您的带有处理程序的代码不太可靠,最好重构它。

mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        // do whatever you want
    }
});

关于java - 我想在播放音乐并且按钮显示 "play"后将搜索栏重置为开始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38446055/

相关文章:

java - 使用 GRAILS(chargify webhooks)解析 FORM-ENCODED 参数

android - 如何使用 stripDrawable 和间距自定义 PagerTabStrip

android - 如何使用 DiffUtil 更新 RecyclerView 适配器

java - GridLayout 的程序逻辑错误

android - 如何制作气球状的 ImageView?

java - 从列表中删除包含空值的对象

java - 如何使 JCombobox 具有更改其他 JCombobox 上的项目的功能

java - servlet 过滤器如何在请求页面上发送错误消息?

java - 如何将纪元日期转换为 Joda 时间 LocalDate?

java - 如何使用两个并发线程更新 View Android