Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:46:42+00:00 2026-06-16T05:46:42+00:00

I’m trying to make a countdown timer that plays a sound when it reaches

  • 0

I’m trying to make a countdown timer that plays a sound when it reaches 0 but whenever it tries to play the sound it force closes. Im pretty sure that my code is right but I can’t figure why it closes. Please help, why does this force close?

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();
}

}

my 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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:46:44+00:00Added an answer on June 16, 2026 at 5:46 am

    try to use

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

    when you set data source to mediaplayer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.