为什么MediaPlayer强制关闭我的应用程序?

时间:2012-12-13 02:44:56

标签: android android-mediaplayer

我正在尝试制作一个倒数计时器,当它达到0时播放声音,但每当它试图播放声音时它会强制关闭。我很确定我的代码是正确的,但我无法弄清楚它为什么会关闭。请帮忙,为什么这个部队关闭?

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


Button start, stop;
int minCount, secCount;
Chronometer count;
boolean viberate, running;
SharedPreferences getPrefs;
MediaPlayer alarm;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initilize();
    count.start();

    count.setOnChronometerTickListener(new OnChronometerTickListener() {

        @Override
        public void onChronometerTick(Chronometer chronometer) {
            if (running) {
                secCount--;
                updateDisplay();
            }

        }

    });
    start.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            running = true;
            alarm.release();

            if (minCount <= 0 && secCount <= 0) {
                nothing();
            }
        }
    });
    stop.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            running = false;
        }
    });





protected void done() {
    // TODO

    boolean music = getPrefs.getBoolean("Sound", true);
    if (music && alarm != null) {
        alarm.start(); //line 179 this is the line that causes the force close

    }
    running = false;
    Toast.makeText(this, "Times Up!!!", Toast.LENGTH_LONG).show();
}

private void initilize() {

    start = (Button) findViewById(R.id.start);
    stop = (Button) findViewById(R.id.stop);
    second = (TextView) findViewById(R.id.second);
    minute = (TextView) findViewById(R.id.minute);
    minCount = 1;
    secCount = 0;
    count = (Chronometer) findViewById(R.id.chronometer1);                           running = false;
    getPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    alarm = MediaPlayer.create(MainActivity.this, R.raw.finished);
}

private void updateDisplay() {
    if (minCount <= 0 && secCount <= 0) {
        done();

    }
    if (secCount < 0) {
        minCount--;
        secCount = 59;
    }


    if (secCount < 0) {
        secCount = 59;
        second.setText("59");

    } else if (secCount <= 9) {
        second.setText("0" + secCount + "");
    } else {
        second.setText(secCount + "");
    }

    if (minCount < 0) {
        minCount = 59;
        minute.setText("59 :");

    } else if (minCount <= 9) {
        minute.setText("0" + minCount + " :");
    } else {
        minute.setText(minCount + " :");
    }


}


@Override
protected void onStop() {
    super.onStop();
    alarm.release();
}

}

我的logcat

12-12 19:55:50.423: E/AndroidRuntime(25573): FATAL EXCEPTION: main
12-12 19:55:50.423: E/AndroidRuntime(25573): java.lang.IllegalStateException
12-12 19:55:50.423: E/AndroidRuntime(25573):    at android.media.MediaPlayer._start(Native Method)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at android.media.MediaPlayer.start(MediaPlayer.java:1025)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at com.example.timer.MainActivity.done(MainActivity.java:179)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at com.example.timer.MainActivity.updateDisplay(MainActivity.java:209)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at com.example.timer.MainActivity.access$0(MainActivity.java:207)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at com.example.timer.MainActivity$1.onChronometerTick(MainActivity.java:47)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at android.widget.Chronometer.dispatchChronometerTick(Chronometer.java:279)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at android.widget.Chronometer$1.handleMessage(Chronometer.java:271)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at android.os.Looper.loop(Looper.java:137)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at android.app.ActivityThread.main(ActivityThread.java:4978)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at java.lang.reflect.Method.invoke(Method.java:511)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-12 19:55:50.423: E/AndroidRuntime(25573):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

你有一个illegalStateException,但你的代码看起来很好。

alarm = MediaPlayer.create(MainActivity.this, R.raw.finished);

确保您的信息流不是非法格式。

答案 1 :(得分:0)

尝试使用

try {
            alarm.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

将数据源设置为mediaplayer时