如何知道播放的媒体播放次数并在android中恢复?

时间:2018-04-12 17:52:11

标签: android android-mediaplayer

我想重复播放x次音频。我想控制播放2个按钮暂停和恢复。 我的问题是如何才能获得简历时间。 我从转换textView的文本(计数器)获得恢复时间并将其发送到start方法,但是当playBtn点击resume_int得到错误的值并且启动方法做错了。 我怎么解决呢?

public class CountActivity extends AppCompatActivity  implements View.OnClickListener{
private TextView counter;
private int loop_num;
private String loop_num_txt=null;
private MediaPlayer mediaPlayer;
public static String audioPath;
public int show_time;
private ImageView imageView;
private AnimationDrawable animationDrawable;
private Button pause,play;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    imageView = (ImageView) findViewById(R.id.tasbih);
    animationDrawable = new AnimationDrawable();

    final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Far_Homa.ttf");
    counter= (TextView) findViewById(R.id.show_counts);  counter.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/Far_Homa.ttf"));
    loop_num_txt= Step2Fragment.times;
    loop_num=Integer.parseInt(loop_num_txt);

    mediaPlayer=new MediaPlayer();
    try {
        mediaPlayer.setDataSource(audioPath);
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this,"فایل صوتی ضبط شده در دسترس نیست.",Toast.LENGTH_LONG).show();
    }

    pause= (Button) findViewById(R.id.pauseBTN); pause.setOnClickListener(this);
    play= (Button) findViewById(R.id.playBTN); play.setOnClickListener(this);

    start(loop_num);
}//oncreate

public int start(final int i){
    show_time = i;
    mediaPlayer.start();

    int duration = mediaPlayer.getDuration();
    animationDrawable.addFrame(getResources().getDrawable(R.drawable.t1),duration);
    animationDrawable.addFrame(getResources().getDrawable(R.drawable.t2),200);
    imageView.setBackgroundDrawable(animationDrawable);
    animationDrawable.start();

    counter.setText(Integer.toString(show_time));

    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            show_time--;
            if (show_time !=0){
                start(show_time);
            }else {
                counter.setText("0");
                imageView.setBackgroundResource(R.drawable.t2);
            }
        }
    });

return show_time;
}


@Override
public void onClick(View view) {
    String resume_txt = counter.getText().toString();
    int resume_int=Integer.parseInt(resume_txt);

    switch (view.getId()){
        case R.id.pauseBTN:
            Toast.makeText(CountActivity.this,resume_txt,Toast.LENGTH_LONG).show();
            mediaPlayer.stop();
            animationDrawable.stop();
            play.setVisibility(View.VISIBLE); pause.setVisibility(View.GONE);
            break;

        case R.id.playBTN:
            start(resume_int);
            play.setVisibility(View.GONE); pause.setVisibility(View.VISIBLE);
            break;
    }


}

}

1 个答案:

答案 0 :(得分:0)

不需要为start方法的输出设置int值。只需要这个!