[SOLVED] MediaPlayer issues

All your problems with Audio, Video and Images.

[SOLVED] MediaPlayer issues

Postby bewl » Wed Mar 24, 2010 4:30 am

I will start of by saying that I am brand new to this SDK but I have been programming for over 10 years (not java... but VB, c++, c#). I wanted to start out with something trivial, so I decided to create a "Drum Machine". Just a simple 4x2 array of square buttons when pressed play a drum sound (i.e. hi-hat, kick, snare, ect...).

The problem that I am having is that when these buttons are pressed too much, the application crashes. I can post a logcat of the issue, but I would like someone to analyze the code (which is very simplistic) and tell me if there is a better way of doing this.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.drum.nation;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.os.Debug;
  10.  
  11. import android.view.View;
  12.  
  13. import android.view.View.OnClickListener;
  14.  
  15. import android.widget.*;
  16.  
  17. import android.media.MediaPlayer;
  18.  
  19.  
  20.  
  21. public class DrumNation extends Activity {
  22.  
  23.     /** Called when the activity is first created. */
  24.  
  25.  
  26.  
  27.  
  28.  
  29.         MediaPlayer padSound;
  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.        
  42.  
  43.         Button pad1 = (Button) findViewById(R.id.pad1);
  44.  
  45.         Button pad2 = (Button) findViewById(R.id.pad2);
  46.  
  47.         Button pad3 = (Button) findViewById(R.id.pad3);
  48.  
  49.         Button pad4 = (Button) findViewById(R.id.pad4);
  50.  
  51.         Button pad5 = (Button) findViewById(R.id.pad5);
  52.  
  53.         Button pad6 = (Button) findViewById(R.id.pad6);
  54.  
  55.         Button pad7 = (Button) findViewById(R.id.pad7);
  56.  
  57.         Button pad8 = (Button) findViewById(R.id.pad8);
  58.  
  59.        
  60.  
  61.         pad1.setOnClickListener(pad1Click);
  62.  
  63.         pad2.setOnClickListener(pad1Click);
  64.  
  65.         pad3.setOnClickListener(pad1Click);
  66.  
  67.         pad4.setOnClickListener(pad1Click);
  68.  
  69.         pad5.setOnClickListener(pad1Click);
  70.  
  71.         pad6.setOnClickListener(pad1Click);
  72.  
  73.         pad7.setOnClickListener(pad1Click);
  74.  
  75.         pad8.setOnClickListener(pad1Click);
  76.  
  77.        
  78.  
  79.  
  80.  
  81.        
  82.  
  83.     }
  84.  
  85.    
  86.  
  87.        
  88.  
  89.         public OnClickListener pad1Click = new OnClickListener() {             
  90.  
  91.                 public void onClick(View v)
  92.  
  93.                 {
  94.  
  95.                         //try
  96.  
  97.                                 switch (v.getId())
  98.  
  99.                                 {
  100.  
  101.                                         case R.id.pad1:
  102.  
  103.                                                 padSound = MediaPlayer.create(getApplicationContext(), R.raw.analog_clap);
  104.  
  105.                                                 padSound.start();
  106.  
  107.                                                
  108.  
  109.                                                 break;
  110.  
  111.                                         case R.id.pad2:
  112.  
  113.                                                 padSound = MediaPlayer.create(getApplicationContext(), R.raw.analog_kick);
  114.  
  115.                                                 padSound.start();
  116.  
  117.                                                 break;
  118.  
  119.                                                
  120.  
  121.                                 }
  122.  
  123.                 }
  124.  
  125.         };
  126.  
  127. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Last edited by bewl on Thu Mar 25, 2010 9:53 pm, edited 1 time in total.
bewl
Freshman
Freshman
 
Posts: 7
Joined: Wed Mar 24, 2010 4:24 am

Top

Postby mrco » Wed Mar 24, 2010 9:00 am

It is not good idea to use MediaPlayer in drum-machine. MediaPlayer designed to play large files in single thread. Try to use SoundPool or AudioTrack.
mrco
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Jan 15, 2010 11:31 am
Location: 2V Software

Postby bewl » Wed Mar 24, 2010 8:54 pm

mrco wrote:It is not good idea to use MediaPlayer in drum-machine. MediaPlayer designed to play large files in single thread. Try to use SoundPool or AudioTrack.


Using SoundPool did the trick perfectly. I appreciate the direction!
bewl
Freshman
Freshman
 
Posts: 7
Joined: Wed Mar 24, 2010 4:24 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 15 guests