如果按下后退按钮而未连接音乐,应用程序将关闭

时间:2016-07-13 19:00:46

标签: java android

我是Android新手,我想通过搜索栏实现控制音乐的代码。以下是我的活动。它的问题是当按下后退按钮时应用程序强制关闭。但是,当我播放音乐并按回按钮时,应用程序不会导致任何问题....

这是我的代码:

package com.example.acer.aartisangrah;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

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(22);
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);
but19 = (Button) findViewById(R.id.button19);
but19.setOnClickListener(this);
}

public void run() {
int currentPosition= 0;
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.isPlaying()) return;
            if(seek_bar.getProgress() > 0) {
                play.start();
                return;
            }
            play = MediaPlayer.create(ekdanta.this, R.raw.ekadanta);
            play.start();
            seek_bar.setProgress(0);
            seek_bar.setMax(play.getDuration());
            new Thread(this).start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        play.stop();
    }

    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(22);
            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 seekBar, int progress, boolean fromUser) {
        if (fromUser) {
            play.seekTo(progress);
            seekBar.setProgress(progress);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}

2 个答案:

答案 0 :(得分:0)

最好的方法是在遇到此类问题时放入日志。但是让我们猜一下:在你的onPause()方法中,如果没有播放音乐,播放应为null。这就是你应该先检查它的原因。含义:

if (play != null) {
play.stop();
}

答案 1 :(得分:-1)

我想您忘了在AndroidManifest.xml中添加权限

  <uses-permission android:name="android.permission.INTERNET"/>