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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:52:48+00:00 2026-05-25T16:52:48+00:00

I am making an app for Android and I have made three buttons to

  • 0

I am making an app for Android and I have made three buttons to play, pause and stop.

My play and pause buttons are set up, so when I click the play button, it will become invisible and the pause button will display, and vice versa.

It works just fine when I click the play button, but after I click on the pause button, it gives me an error.

The code is given below.

 package com.mpIlango;

 import java.io.IOException;
 import java.util.ArrayList;
 import android.app.Activity;
 import android.media.MediaPlayer;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.RadioGroup;
 import android.widget.RadioGroup.OnCheckedChangeListener;

public class MpIlangoActivity extends Activity implements OnCheckedChangeListener {
/** Called when the activity is first created. */


MediaPlayer song1,song2,song3;
int whatsong = 0;

private ArrayList<Integer> songIds;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    RadioGroup rgMusic = (RadioGroup) findViewById(R.id.rgMusic);

    songIds = new ArrayList<Integer>();

    songIds.add(R.raw.fluet);
    songIds.add(R.raw.airtel);
    songIds.add(R.raw.titanic);


    final Button bPlay = (Button) findViewById(R.id.bPlay);
    final Button bStop = (Button) findViewById(R.id.bStop);
    final Button bPause = (Button) findViewById(R.id.bPause);

    bPause.setVisibility(View.GONE);

    rgMusic.setOnCheckedChangeListener(this);

    bPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {           

             if(song1!=null)   {
                   song1.release();
                }

              if(song2!=null)  {
                   song2.release();
                }

              if(song3!=null)  {
                   song3.release();
                }

              switch (whatsong) {

                case 1:

                    try {
                        song1 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(0));
                        song1.prepare();
                    } catch (IllegalStateException e) {                 
                        e.printStackTrace();
                    } catch (IOException e) {                   
                        e.printStackTrace();
                    }
                    song1.start();
                    bPlay.setVisibility(View.GONE);
                    bPause.setVisibility(View.VISIBLE);
                    break;  

                case 2:
                    try {
                        song2 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(1));
                        song2.prepare();
                    } catch (IllegalStateException e) {                 
                        e.printStackTrace();
                    } catch (IOException e) {                   
                        e.printStackTrace();
                    }
                    song2.start();
                    bPlay.setVisibility(View.GONE);
                    bPause.setVisibility(View.VISIBLE);
                    break;

                case 3:
                    try {
                        song3 = MediaPlayer.create(MpIlangoActivity.this, songIds.get(2));
                        song3.prepare();
                    } catch (IllegalStateException e) {                     
                        e.printStackTrace();
                    } catch (IOException e) {               
                        e.printStackTrace();
                    }
                    song3.start();
                    bPlay.setVisibility(View.GONE);
                    bPause.setVisibility(View.VISIBLE);
                    break;
                }
            }               
    });

    bPause.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            bPlay.setVisibility(View.VISIBLE);
            bPause.setVisibility(View.GONE);

            if(song1.isPlaying()){
                song1.pause();          
            }

            if(song2.isPlaying()){
                song2.pause();
            }

            if(song3.isPlaying()){
                song3.pause();
            }               
        }
    });

    bStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

             if(song1!=null){
                   song1.release();
               }

              if(song2!=null){
                   song2.release();
               }

              if(song3!=null){
                   song3.release();
               }                
        }
    });;        
}

@Override
public void onCheckedChanged(RadioGroup group, int rbId) {

    switch (rbId) {

    case R.id.rbMusic1:
        whatsong = 1;           
        break;
    case R.id.rbMusic2:
        whatsong = 2;           
        break;
    case R.id.rbMusic3:
        whatsong = 3;           
        break;

    }       

}

  }
  • 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-05-25T16:52:49+00:00Added an answer on May 25, 2026 at 4:52 pm

    I guess you get a NullPointerException somewhere here!?

    if(song1.isPlaying()){
        song1.pause();          
    }
    
    if(song2.isPlaying()){
        song2.pause();
    }
    
    if(song3.isPlaying()){
        song3.pause();
    }               
    

    it that’s the problem you may use your switch in here too.

    switch (whatsong) {
    
        case 1: 
            if(song1.isPlaying()){
                song1.pause();          
            }
    

    or initialize your songs somewhere else to make sure they will never be null

    I also recommend using only one MediaPlayer.

    MediaPlayer song;
    

    bPlay code:

    if(song!=null)   {
       song.release();
    }
    
    switch (whatsong) {
    
        case 1:
    
            try {
                song = MediaPlayer.create(MpIlangoActivity.this, songIds.get(0));
                song.prepare();
            } catch (IllegalStateException e) {                 
                e.printStackTrace();
            } catch (IOException e) {                   
                e.printStackTrace();
            }
    
    }
    
    song.start();
    bPlay.setVisibility(View.GONE);
    bPause.setVisibility(View.VISIBLE);
    

    bPause code:

    bPlay.setVisibility(View.VISIBLE);
    bPause.setVisibility(View.GONE);
    
    if(song != null && song.isPlaying()){
        song.pause();          
    }
    

    ALL of this Code is untested!

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

Sidebar

Related Questions

i am making a app which takes photo on button click i have camera.java
I am making an android app which will have two services that will keep
I am making an app that will play sound on events but I can't
I'm making an Android app and have a strange problem. I need a ViewFlipper
I'm making an Android app using Processing, and have decided to port my code
Good afternoon. I have been making a small openGL based app for android that
I have started making graphics for my Android app using Adobe Photoshop. But I
I am making an Android app, and have created a custom view to do
I have tried making a listview for a android app like this tutorial .
I am making one app on Android and I have no idea what is

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.