0

I, myself, am quite new to Android. I've done a soundboard once but now I'm making a better one. I have 4 topics(activities). When I switch between them for a while, it gives me an OutOfMemoryError. I tried reducing the resolution of the background, the Image Buttons and it's still throwing me that error. I kill the previous activity as soon as a button is pressed. I also used unbindDrawables but that doesn't seem to help either.

My error log

Much help is appreciated.

My start screen activity with 4 links to activities:

public class Startscreen extends Activity {

ImageButton imageButton1;
ImageButton imageButton2;
ImageButton imageButton3;
ImageButton imageButton4;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startscreen);
    addListenerOnButton();

}

private void unbindDrawables(View LinearLayout) {
    if (LinearLayout.getBackground() != null) {
        LinearLayout.getBackground().setCallback(null);
    }
    if (LinearLayout instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) LinearLayout).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) LinearLayout).getChildAt(i));
        }
        ((ViewGroup) LinearLayout).removeAllViews();

    }

}
public void addListenerOnButton() {



    imageButton1 = (ImageButton) findViewById(R.id.imageButton1);

    imageButton1.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {

            Intent intent = new Intent(Startscreen.this, Airlines.class);
            startActivity(intent);
            overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
            unbindDrawables(findViewById(R.id.scrollView));
            finish();

        }
    });


    imageButton2 = (ImageButton) findViewById(R.id.imageButton2);

    imageButton2.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            Intent intent = new Intent(Startscreen.this, Cabdriver.class);
            startActivity(intent);
            overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
            unbindDrawables(findViewById(R.id.scrollView));
            finish();

        }
    });

    imageButton3 = (ImageButton) findViewById(R.id.imageButton4);

    imageButton3.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            Intent intent = new Intent(Startscreen.this, CakeSoup.class);
            startActivity(intent);
            overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
            unbindDrawables(findViewById(R.id.scrollView));
            finish();

        }
    });

    imageButton4 = (ImageButton) findViewById(R.id.imageButton3);

    imageButton4.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            Intent intent = new Intent(Startscreen.this, BorisLife.class);
            startActivity(intent);
            overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
            unbindDrawables(findViewById(R.id.scrollView));
            finish();

        }

    });


    ImageView img = (ImageView) findViewById(R.id.imageView1);
    img.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("https://www.youtube.com/channel/UCS5tt2z_DFvG7-39J3aE-bQ"));
            startActivity(intent);
        }
    });


}

;






@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();


}

My first activity, all 4 of them are almost identical except the sounds' names.

public class Airlines extends Activity {



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

    ImageButton button1 = (ImageButton) this.findViewById(R.id.Button1);
    final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.airplanetime);
    button1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp1.start();

        }
    });

    ImageButton button2 = (ImageButton) this.findViewById(R.id.Button2);
    final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.masterofskies);
    button2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp2.start();
        }
    });

    ImageButton button3 = (ImageButton) this.findViewById(R.id.Button3);
    final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.oohh);
    button3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp3.start();
        }
    });

    ImageButton button4 = (ImageButton) this.findViewById(R.id.Button4);
    final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.friendyouasshole);


    button4.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp4.start();
        }
    });

    ImageButton button5 = (ImageButton) this.findViewById(R.id.Button5);
    final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.fuckyou);


    button5.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp5.start();
        }
    });

    ImageButton button6 = (ImageButton) this.findViewById(R.id.Button6);
    final MediaPlayer mp6 = MediaPlayer.create(this, R.raw.hopeyouenjoy);


    button6.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp6.start();
        }
    });

    ImageButton button7 = (ImageButton) this.findViewById(R.id.Button7);
    final MediaPlayer mp7 = MediaPlayer.create(this, R.raw.oyblin);


    button7.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp7.start();
        }
    });

    ImageButton button8 = (ImageButton) this.findViewById(R.id.Button8);
    final MediaPlayer mp8 = MediaPlayer.create(this, R.raw.whatthefuck);


    button8.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp8.start();
        }
    });

    ImageButton button9 = (ImageButton) this.findViewById(R.id.Button9);
    final MediaPlayer mp9 = MediaPlayer.create(this, R.raw.muchbetter);


    button9.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp9.start();
        }
    });

    ImageButton button10 = (ImageButton) this.findViewById(R.id.Button10);
    final MediaPlayer mp10 = MediaPlayer.create(this, R.raw.naturaltalent);


    button10.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp10.start();
        }
    });

    ImageButton button11 = (ImageButton) this.findViewById(R.id.Button11);
    final MediaPlayer mp11 = MediaPlayer.create(this, R.raw.neewpff);


    button11.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            mp11.start();
        }
    });


    ImageView img = (ImageView) findViewById(R.id.imageView1);
    img.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);
            intent.setData(Uri.parse("https://www.youtube.com/channel/UCS5tt2z_DFvG7-39J3aE-bQ"));
            startActivity(intent);
        }
    });


}


private void unbindDrawables(View LinearLayout) {
    if (LinearLayout.getBackground() != null) {
        LinearLayout.getBackground().setCallback(null);
    }
    if (LinearLayout instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) LinearLayout).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) LinearLayout).getChildAt(i));
        }
        ((ViewGroup) LinearLayout).removeAllViews();

    }

}



@Override
public void onBackPressed() {
    super.onBackPressed();
    unbindDrawables(findViewById(R.id.linearLeiout));
    Intent intent = new Intent(Airlines.this, LoadingScreenActivity.class);
    startActivity(intent);
    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    this.finish();

}

}

1 Answers1

0

I can see a few potential issues here that could be causing your problems.

  1. If you read the description of MediaPlayer.create() it says:

When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception.

and you don't appear to be calling release(). You might be getting a memory leak caused by not releasing the MediaPlayers.

  1. In your Airlines class, you're creating 11 MediaPlayer objects regardless of whether they'll be used. It isn't clear to me whether you want it to be possible to play multiple sounds at the same time as each other. If you do it may be necessary to have multiple MediaPlayers, but you could consider creating them in the onClick() methods so they only get created if they're needed if it's fast enough to load the sounds only after a click. If you don't need to play sounds simultaneously you might be better with a single MediaPlayer.

  2. It might be better to use an application Context by using getApplicationContext() instead of the Activity's Context in the calls to MediaPlayer.create() although if you release the MediaPlayer objects hopefully you won't leak any Activitys anyway.

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
  • I am going to have a look tomorrow, but no, I do not want to play the sounds at the same time. I tried using SoundPool but that just gave me a headache. A noob in Android can mess up just about anything. :) –  Aug 12 '15 at 15:15
  • EDIT: I used two arrays and a bunch of code to make it work with one MediaPlayer. The sounds will not play at the same time anymore. Good answer! :) –  Aug 13 '15 at 15:25
  • So, if I have opened up 15 activities in the last 20 seconds(user is going through very fast), could that be the problem? What line of code should I add to clear the activity after it has displayed? I get an `outOfMemory` error. THanks! – Ruchir Baronia Jan 04 '16 at 06:07