Soundboad Crashes

Put your problem here if it does not fit any of the other categories.

Soundboad Crashes

Postby elomanias » Mon Jan 04, 2010 3:23 pm

Hi Folks
i got a little problem

I rote a little Soundboard and when i hit the first 6 buttons it works ver fine but when i click one of the others the soundboard crashes and i dont know why

here's my java file code

Code: Select all
package com.raddieltopo.soundboard.germanemployee;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class GermanEmployee extends Activity implements OnClickListener {
   private MediaPlayer a,b,c,d,e,f,g,h,i,j,k,l;
   
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       // Native rate is 44.1kHz 16 bit stereo, but
       // to save space we just use MPEG-3 22kHz mono
       MediaPlayer mp = MediaPlayer.create(this, R.raw.a);
       mp.start();
       b = MediaPlayer.create(this, R.raw.b);
       c = MediaPlayer.create(this, R.raw.c);
       d = MediaPlayer.create(this, R.raw.d);
       e = MediaPlayer.create(this, R.raw.e);
       f = MediaPlayer.create(this, R.raw.f);
       g = MediaPlayer.create(this, R.raw.g);
       h = MediaPlayer.create(this, R.raw.h);
       i = MediaPlayer.create(this, R.raw.i);
       j = MediaPlayer.create(this, R.raw.j);
       k = MediaPlayer.create(this, R.raw.k);
       l = MediaPlayer.create(this, R.raw.l);
       
       // Setup click listeners
       setContentView(R.layout.main);
       
       View button0 = this.findViewById(R.id.button0);
       button0.setOnClickListener(this);
       View button1 = this.findViewById(R.id.button1);
       button1.setOnClickListener(this);
       View button2 = this.findViewById(R.id.button2);
       button2.setOnClickListener(this);
       View button3 = this.findViewById(R.id.button3);
       button3.setOnClickListener(this);
       View button4 = this.findViewById(R.id.button4);
       button4.setOnClickListener(this);
       View button5 = this.findViewById(R.id.button5);
       button5.setOnClickListener(this);
       View button6 = this.findViewById(R.id.button6);
       button6.setOnClickListener(this);
       View button7 = this.findViewById(R.id.button7);
       button7.setOnClickListener(this);
       View button8 = this.findViewById(R.id.button8);
       button8.setOnClickListener(this);
       View button9 = this.findViewById(R.id.button9);
       button9.setOnClickListener(this);
       View button10 = this.findViewById(R.id.button10);
       button10.setOnClickListener(this);
       View button11 = this.findViewById(R.id.button11);
       button11.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
      MediaPlayer mp = null;
      switch (v.getId()) {
      
      case R.id.button0:
         mp=a;
         break;
      case R.id.button1:
         mp=b;
         break;
      case R.id.button2:
         mp=c;
         break;
      case R.id.button3:
         mp=d;
         break;
      case R.id.button4:
         mp=e;
         break;
      case R.id.button5:
         mp=f;
         break;
      case R.id.button6:
         mp=g;
         break;
      case R.id.button7:
         mp=h;
         break;
      case R.id.button8:
         mp=i;
         break;
      case R.id.button9:
         mp=j;
         break;
      case R.id.button10:
         mp=k;
         break;
      case R.id.button11:
         mp=l;
         break;         
      
      }
      
      mp.seekTo(0);
      mp.start();
      
   }
   
   // Called only the first time the options menu is displayed.
   // Create the menu entries.
   // Menu adds items in the order shown.
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       super.onCreateOptionsMenu(menu);
      
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.layout.menu, menu);
      
       // Parameters for menu.add are:
       // group -- Not used here.
       // id -- Used only when you want to handle and identify the click yourself.
       // title
       /*
       menu.add("About...");
       menu.add("Quit");
       */
       return true;
   }
   
   // Activity callback that lets your handle the selection in the class.
   // Return true to indicate that you've got it, false to indicate
   // that it should be handled by a declared handler object for that
   // item (handler objects are discouraged for reasons of efficiency).
   @Override
   public boolean onOptionsItemSelected(MenuItem item){
      
      switch(item.getItemId()) {
      
      case R.id.about:
         startActivity(new Intent(this, activity.class));
          return true;
      
      case R.id.quit:
         finish();
         return true;
         
      }
   
      return false;
   
   }
     
}



Hope someone can help me

Thx
elomanias
elomanias
Freshman
Freshman
 
Posts: 4
Joined: Sun Jan 03, 2010 10:30 pm

Top

Postby qlimax » Mon Jan 04, 2010 7:24 pm

try to set a completition listener to each started player instance... like this

mp.setOnCompletionListener(new OnCompletionListener(){

mp.release();

});

maybe it's a heap memory overload problem....
bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby elomanias » Mon Jan 04, 2010 8:59 pm

u mean into the case R.id.buttonxy ??

could u please give me an example??

THX in advance
elomanias
Freshman
Freshman
 
Posts: 4
Joined: Sun Jan 03, 2010 10:30 pm

Postby qlimax » Mon Jan 04, 2010 9:31 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.raddieltopo.soundboard.germanemployee;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.Intent;
  8.  
  9. import android.media.MediaPlayer;
  10.  
  11. import android.os.Bundle;
  12.  
  13. import android.view.View.OnClickListener;
  14.  
  15. import android.view.MenuInflater;
  16.  
  17. import android.view.View;
  18.  
  19. import android.view.Menu;
  20.  
  21. import android.view.MenuItem;
  22.  
  23.  
  24.  
  25. public class GermanEmployee extends Activity implements OnClickListener {
  26.  
  27.    private MediaPlayer a,b,c,d,e,f,g,h,i,j,k,l;
  28.  
  29.    
  30.  
  31.    
  32.  
  33.    @Override
  34.  
  35.    public void onCreate(Bundle savedInstanceState) {
  36.  
  37.        super.onCreate(savedInstanceState);
  38.  
  39.        setContentView(R.layout.main);
  40.  
  41.        // Native rate is 44.1kHz 16 bit stereo, but
  42.  
  43.        // to save space we just use MPEG-3 22kHz mono
  44.  
  45.        MediaPlayer mp = MediaPlayer.create(this, R.raw.a);
  46.  
  47.        mp.start();
  48.  
  49.        b = MediaPlayer.create(this, R.raw.b);
  50.  
  51.        c = MediaPlayer.create(this, R.raw.c);
  52.  
  53.        d = MediaPlayer.create(this, R.raw.d);
  54.  
  55.        e = MediaPlayer.create(this, R.raw.e);
  56.  
  57.        f = MediaPlayer.create(this, R.raw.f);
  58.  
  59.        g = MediaPlayer.create(this, R.raw.g);
  60.  
  61.        h = MediaPlayer.create(this, R.raw.h);
  62.  
  63.        i = MediaPlayer.create(this, R.raw.i);
  64.  
  65.        j = MediaPlayer.create(this, R.raw.j);
  66.  
  67.        k = MediaPlayer.create(this, R.raw.k);
  68.  
  69.        l = MediaPlayer.create(this, R.raw.l);
  70.  
  71.  
  72.  
  73. a.setOnCompletionListener(new OnCompletionListener(){
  74.  
  75.  
  76.  
  77. a.release();
  78.  
  79.  
  80.  
  81. });
  82.  
  83.  
  84.  
  85.  
  86.  
  87. b.setOnCompletionListener(new OnCompletionListener(){
  88.  
  89.  
  90.  
  91. b.release();
  92.  
  93.  
  94.  
  95. });
  96.  
  97.  
  98.  
  99. //and so on...
  100.  
  101.  
  102.  
  103.  
  104.  
  105.        
  106.  
  107.        // Setup click listeners
  108.  
  109.        setContentView(R.layout.main);
  110.  
  111.        
  112.  
  113.        View button0 = this.findViewById(R.id.button0);
  114.  
  115.        button0.setOnClickListener(this);
  116.  
  117.        View button1 = this.findViewById(R.id.button1);
  118.  
  119.        button1.setOnClickListener(this);
  120.  
  121.        View button2 = this.findViewById(R.id.button2);
  122.  
  123.        button2.setOnClickListener(this);
  124.  
  125.        View button3 = this.findViewById(R.id.button3);
  126.  
  127.        button3.setOnClickListener(this);
  128.  
  129.        View button4 = this.findViewById(R.id.button4);
  130.  
  131.        button4.setOnClickListener(this);
  132.  
  133.        View button5 = this.findViewById(R.id.button5);
  134.  
  135.        button5.setOnClickListener(this);
  136.  
  137.        View button6 = this.findViewById(R.id.button6);
  138.  
  139.        button6.setOnClickListener(this);
  140.  
  141.        View button7 = this.findViewById(R.id.button7);
  142.  
  143.        button7.setOnClickListener(this);
  144.  
  145.        View button8 = this.findViewById(R.id.button8);
  146.  
  147.        button8.setOnClickListener(this);
  148.  
  149.        View button9 = this.findViewById(R.id.button9);
  150.  
  151.        button9.setOnClickListener(this);
  152.  
  153.        View button10 = this.findViewById(R.id.button10);
  154.  
  155.        button10.setOnClickListener(this);
  156.  
  157.        View button11 = this.findViewById(R.id.button11);
  158.  
  159.        button11.setOnClickListener(this);
  160.  
  161.    }
  162.  
  163.  
  164.  
  165.    @Override
  166.  
  167.    public void onClick(View v) {
  168.  
  169.       MediaPlayer mp = null;
  170.  
  171.       switch (v.getId()) {
  172.  
  173.      
  174.  
  175.       case R.id.button0:
  176.  
  177.          mp=a;
  178.  
  179.          break;
  180.  
  181.       case R.id.button1:
  182.  
  183.          mp=b;
  184.  
  185.          break;
  186.  
  187.       case R.id.button2:
  188.  
  189.          mp=c;
  190.  
  191.          break;
  192.  
  193.       case R.id.button3:
  194.  
  195.          mp=d;
  196.  
  197.          break;
  198.  
  199.       case R.id.button4:
  200.  
  201.          mp=e;
  202.  
  203.          break;
  204.  
  205.       case R.id.button5:
  206.  
  207.          mp=f;
  208.  
  209.          break;
  210.  
  211.       case R.id.button6:
  212.  
  213.          mp=g;
  214.  
  215.          break;
  216.  
  217.       case R.id.button7:
  218.  
  219.          mp=h;
  220.  
  221.          break;
  222.  
  223.       case R.id.button8:
  224.  
  225.          mp=i;
  226.  
  227.          break;
  228.  
  229.       case R.id.button9:
  230.  
  231.          mp=j;
  232.  
  233.          break;
  234.  
  235.       case R.id.button10:
  236.  
  237.          mp=k;
  238.  
  239.          break;
  240.  
  241.       case R.id.button11:
  242.  
  243.          mp=l;
  244.  
  245.          break;        
  246.  
  247.      
  248.  
  249.       }
  250.  
  251.      
  252.  
  253.       mp.seekTo(0);
  254.  
  255.       mp.start();
  256.  
  257.      
  258.  
  259.    }
  260.  
  261.    
  262.  
  263.    // Called only the first time the options menu is displayed.
  264.  
  265.    // Create the menu entries.
  266.  
  267.    // Menu adds items in the order shown.
  268.  
  269.    @Override
  270.  
  271.    public boolean onCreateOptionsMenu(Menu menu) {
  272.  
  273.        super.onCreateOptionsMenu(menu);
  274.  
  275.      
  276.  
  277.        MenuInflater inflater = getMenuInflater();
  278.  
  279.        inflater.inflate(R.layout.menu, menu);
  280.  
  281.      
  282.  
  283.        // Parameters for menu.add are:
  284.  
  285.        // group -- Not used here.
  286.  
  287.        // id -- Used only when you want to handle and identify the click yourself.
  288.  
  289.        // title
  290.  
  291.        /*
  292.  
  293.        menu.add("About...");
  294.  
  295.        menu.add("Quit");
  296.  
  297.        */
  298.  
  299.        return true;
  300.  
  301.    }
  302.  
  303.    
  304.  
  305.    // Activity callback that lets your handle the selection in the class.
  306.  
  307.    // Return true to indicate that you've got it, false to indicate
  308.  
  309.    // that it should be handled by a declared handler object for that
  310.  
  311.    // item (handler objects are discouraged for reasons of efficiency).
  312.  
  313.    @Override
  314.  
  315.    public boolean onOptionsItemSelected(MenuItem item){
  316.  
  317.      
  318.  
  319.       switch(item.getItemId()) {
  320.  
  321.      
  322.  
  323.       case R.id.about&#058;
  324.  
  325.          startActivity(new Intent(this, activity.class));
  326.  
  327.           return true;
  328.  
  329.      
  330.  
  331.       case R.id.quit:
  332.  
  333.          finish();
  334.  
  335.          return true;
  336.  
  337.          
  338.  
  339.       }
  340.  
  341.    
  342.  
  343.       return false;
  344.  
  345.    
  346.  
  347.    }
  348.  
  349.      
  350.  
  351. }
Parsed in 0.051 seconds, using GeSHi 1.0.8.4


I suggest you to try to recycle the same MediaPlayer object... in this way you have to declare only one oncompletionlistener instead of 10.... and your code will look (in all cases) better.

bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby elomanias » Mon Jan 04, 2010 11:36 pm

i may just got a complete hangover but when i try to insert the changes you provided i get several syntax errors

could u please provide anything i can insert, duplicate and edit to get these erros out of my soundboard??

im sitting here since friday and since saturday i get these crashes and i dont know why

THX in advance
elomanias
Freshman
Freshman
 
Posts: 4
Joined: Sun Jan 03, 2010 10:30 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests