簡體   English   中英

如果在沒有播放音樂的情況下按下了后退按鈕,則應用程序將關閉

[英]the app closes if back button is pressed without palying the music

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

    }
}

最好的方法是在出現此類問題時放入日志。 但讓我們猜測一下:在您的onPause()方法中,如果尚未播放音樂,則play應該為null。 這就是為什么您應該首先檢查它的原因。 含義:

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

我認為您忘記在AndroidManifest.xml中添加權限

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM