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

  • SEARCH
  • Home
  • 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 9236667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:21:03+00:00 2026-06-18T07:21:03+00:00

I can touch the toggle button on (once) and off (once), but if I

  • 0

I can touch the toggle button on (once) and off (once), but if I touch it a third time, it turns on but never plays the music. From there, when I touch it again to turn it off it crashes.

public class Main extends Activity {

MediaPlayer mp;
Button startButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    mp = MediaPlayer.create(getBaseContext(), R.raw.songthing);

    ToggleButton toggle = (ToggleButton) findViewById(R.id.ToggleButton);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                mp.start();
            } else {
                mp.stop();
            }
        }
    });

    mp.setOnCompletionListener(new OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });

}

I’m not sure if I should have it within the onCreate method… I’ve tried to move it but I get very confused with all the brackets and it doesn’t work out.

A little more about my code… I’m trying to get the media player to play a song when the toggle button is on and stop it when its off.

EDIT: Logcat says something about java.lang.IllegalStateException

XML:

<ToggleButton
        android:id="@+id/ToggleButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:textOff="Start"
        android:textOn="Stop" />
  • 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-18T07:21:04+00:00Added an answer on June 18, 2026 at 7:21 am

    You can read a bit about the MediaPlayer states here.
    If you call stop(), you have to prepare() the player again before you can start() it.

    Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.

    If preparing the player takes too long, consider using pause() followed by seekTo(0); as an alternative.

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

Sidebar

Related Questions

I know I can get touch events in my iPhone app. But these touch
How can I get input data from the rear touch pad on the Vita
So I'm trying to have a button where users can choose between flash on/off/auto.
I am making a card game where the player can touch and drag around
Can we execute touch event programmatically in android by some method, such as passing
I would like to find out how can I convert touch coordinate that I
Can i create custom touch events for iPhone?? will the device support for creating
Can anyone give me a hand with a touch of regex? I'm reading in
I can't work out how to pass the touch location in touch began into
On a touch device like iPhone/iPad/Android it can be difficult to hit a small

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.