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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:03:20+00:00 2026-05-13T09:03:20+00:00

I am currently developing my first Android application by reading Dev Documentation at Android

  • 0

I am currently developing my first Android application by reading Dev Documentation at Android official website. What I am trying to accomplish is to play some ring sounds. A section from my code is:

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class PlayRingSounds extends Activity {

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

public void PlayRingFile(View view) {       
  switch (view.getId()) {
    case R.id.Button01:
      MediaPlayer mp1 = MediaPlayer.create(this.getApplicationContext(), R.raw.aaa);
      mp1.start();
      break;
    case R.id.Button02:
      MediaPlayer mp2 = MediaPlayer.create(this.getApplicationContext(), R.raw.bbb);
      mp2.start();
      break;        
  }
}   
}

The problem is when I click the 2nd button while “aaa” (sound file from 1st button) is playing, “bbb” also starts playing at the same time. Is there a way to stop “aaa” before “bbb” plays, or is there a way to stop all media players?

Update 12/30/2009 – New code is:

   case R.id.Button01:
       if (mp2.isPlaying())
       {
           mp2.stop();
           mp2.release();
       }
       mp1.reset();
       mp1.prepare();
       mp1.start();
       break;
   case R.id.Button02:
       if (mp1.isPlaying())
       {
           mp1.stop();
           mp1.release();
       }
       mp2.reset();
       mp2.prepare();
       mp2.start();
       break;

mp1.prepare() and mp2.prepare() give IOException error.

  • 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-13T09:03:21+00:00Added an answer on May 13, 2026 at 9:03 am

    Please note: This is not the best way to do this, this is very sloppy, this is just an idea

    public void PlayRingFile(View view) {       
      switch (view.getId()) {
       case R.id.Button01:
        if (mp2.isPlaying()) {
           mp2.stop();    // stops the object from playing
           mp2.release(); // always a good practice to release the resource when done
         }
        MediaPlayer mp1 = MediaPlayer.create(this.getApplicationContext(), R.raw.aaa);
        mp1.start();
        break;
       case R.id.Button02:
        if (mp1.isPlaying()) {
           mp1.stop();    // stops the object from playing
           mp1.release(); // always a good practice to release the resource
         }
        MediaPlayer mp2 = MediaPlayer.create(this.getApplicationContext(), R.raw.bbb);
        mp2.start();
        break;        
     }
    }
    

    As I said, this isn’t the best solution especially if you add more buttons then you would have to check every instance of MediaPlayer and there must be better ways of doing this.

    My suggestions are to try to find a way to loop through all MediaPlayer‘s to see if they are open and if so, release the resource and stop playing or maybe a way to stop all MediaPlayer‘s from playing in general?

    I will continue to look for other ways in the meantime, hope this points you in the right direction.

    EDIT (12/30/09):

    case R.id.Button01:
       if (mp2.isPlaying())  {
           mp2.stop();
           mp2.release();
       }
       mp1.reset();
       createMPlayer1(); // used to re-initialze the mediaplayer for reuse since resources were released.
       mp1.prepare();
       mp1.start();
       break;
    case R.id.Button02:
       if (mp1.isPlaying()) {
           mp1.stop();
           mp1.release();
       }
       mp2.reset();
       createMPlayer2();
       mp2.prepare();
       mp2.start();
       break;
    
    public void createMPlayer1() {
       MediaPlayer mp1 = MediaPlayer.create(this.getApplicationContext(), R.raw.aaa);
    }
    
    public void createMPlayer2() {
       MediaPlayer mp2 = MediaPlayer.create(this.getApplicationContext(), R.raw.bbb);
    }
    

    I think the IOException could be called when trying to access the file again after we have renoved the resouce. I added two methods to create the separate raw files since I believe the exception occurs when the resources are released. You can either re-initialized the MediaPlayer‘s or try to discard releasing the resource.

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

Sidebar

Ask A Question

Stats

  • Questions 425k
  • Answers 425k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I understand correctly your problem is that your code… May 15, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer As Juan Manuel pointed out, this is your browser protecting… May 15, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer Try to change your ' to " - php interprets… May 15, 2026 at 12:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.