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 9114061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:06:56+00:00 2026-06-17T04:06:56+00:00

package net.androidbootcamp.guessinggame; import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;

  • 0
package net.androidbootcamp.guessinggame;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class setting extends Activity {
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);

Button s =(Button) findViewById(R.id.btnons);
s.setOnClickListener(new OnClickListener(){

public void onClick(View v) {


MediaPlayer mp = MediaPlayer.create(setting.this,R.raw.meow);
mp.start();





                }

    });

Button ss =(Button) findViewById(R.id.btnoffs);
ss.setOnClickListener(new OnClickListener(){

How can I code the Off Button.This is my code of ON button.I don’t know how to stop the sound when I click the other button(off).

 I have two buttons, the ON and OFF button,a normal game settings of sound.

I don’t know what’s next. I want to stop the stop everytime I click the OFF button.

  • 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-17T04:06:57+00:00Added an answer on June 17, 2026 at 4:06 am

    This should probably be sufficient to handle the MediaPlayer present in your code:

    package net.androidbootcamp.guessinggame;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class setting extends Activity {
        private MediaPlayer mediaPlayer;
    
        @Override
        public void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.settings);
    
            Button s =(Button) findViewById(R.id.btnons);
            s.setOnClickListener(
                new OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        startMediaPlayer();
                    }
                }
            );
    
            Button ss =(Button) findViewById(R.id.btnoffs);
            ss.setOnClickListener(
                new OnClickListener(){ 
                    @Override
                    public void onClick(View v) {
                        stopMediaPlayer();
                    }
                }
            );
        }
    
         private void startMediaPlayer() {
            mediaPlayer = MediaPlayer.create(setting.this,R.raw.meow);
            mediaPlayer.start();
        }
    
        private void stopMediaPlayer() {
            if( mediaPlayer != null ) {
                mp.stop();
                mp.release();
            }
        }
    }
    

    Changes towards your code:
    mp is now a member of the class setting, called mediaPlayer. Previously, you’d have a hard time trying to work on the mp in the scope of the “off button”‘s OnClickListener, but that’s solved now. Also wrapped the code in stopMediaPlayer() with a null check to avoid a NullPointerException.

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

Sidebar

Related Questions

package net.learn2develop.PopularAttractions; import java.io.IOException; import java.util.List; import java.util.Locale; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController;
I have this class package net.omnosis.mazegame.components; import net.omnosis.mazegame.SlicedBitmap; import android.graphics.Bitmap; public class PlayerLayer extends
latitude and longitude change in android and map not shown package net.learn2develop.GoogleMaps; import java.io.IOException;
Here's my code package net.sourceforge.jwebunit.sample; import net.sourceforge.jwebunit.WebTestCase; public class JWebUnitSearchExample extends WebTestCase { public
Android kills my service in the package com.net.myspeechservice under some certain scenarios, for example
Here is the server code package echoserver; import java.net.*; import java.io.*; public class EchoServer
Having a simple Akka HTTP server: package org.package.some import akka.actor.{IOManager, IO, Actor} import java.net.InetSocketAddress
I have the following simple entity: package net.plus.msodb.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity;
package net.example.view; class StatusBar extends View { ... } I use it in my
I have following file upload code: package net.viralpatel.contact.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller;

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.