【问题标题】:Pause music when phone receives any incoming calls, home button is pressed and when phone screen goes off(lockscreen)当手机收到任何来电,按下主页按钮以及手机屏幕关闭时暂停音乐(锁屏)
【发布时间】:2016-07-21 05:29:54
【问题描述】:

使用此媒体播放器播放音乐时,每当来电时,按下主页按钮并出现锁屏并在出现上述问题后返回应用程序...应用程序中的播放按钮没有响应...。我想暂停音乐,直到我返回应用程序并从停止的位置继续播放......请帮助......

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) {

    }
    }

【问题讨论】:

标签: java android


【解决方案1】:
@Override
protected void onPause()
{
    super.onPause();
    if (play3!= null)
    {
        play3.pause();
    }
}

@Override
protected void onResume()
{
    super.onResume();
    if ((play3 != null) && (!play3.isPlaying())) {
        but32.setText("Play");
        but32.setOnClickListener(this);
    }
}

这帮助我克服了我的问题.....我可以播放当主页按钮/锁屏/通话出现时暂停的音乐...从它停止的地方开始

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多