1. Download our Official Android App: Forums for Android!

Apps MediaPlayer array causing nullpointer

Discussion in 'Android Development' started by itzjonno, Aug 7, 2011.

  1. itzjonno

    itzjonno Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    6
    Joined:
    Aug 5, 2011

    Aug 5, 2011
    6
    0
    5
    **SOLVED***

    used: MediaPlayer[] media=new MediaPlayer[5];

    Hi, can anyone see why I would be getting a nullpointerexception with this code? The code is basically to create a MediaPlayer array so that I can loop through it and stop/start all of the mediaplayers at once. They are also linked to seekbars to control volume.

    note: it works manually typing out using loads of mediaplayer variables, just not in this array format

    Code (Text):
    1.  private MediaPlayer[] media;
    2.    
    3.        
    4.     /** Called when the activity is first created. */
    5.     @Override
    6.     public void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.main);
    9.          
    10.                
    11.         //media players
    12.        
    13.         media[0] = MediaPlayer.create(this, R.raw.drums);
    14.        
    15.        
    16.        
    17.         media[1] = MediaPlayer.create(this, R.raw.bass);
    18.        
    19.              
    20.         media[2] = MediaPlayer.create(this, R.raw.synth);
    21.        
    22.        
    23.         media[3] = MediaPlayer.create(this, R.raw.snare);
    24.        
    25.        
    26.         media[4]  = MediaPlayer.create(this, R.raw.wobble);
    27.        
    28.        
    29.         for(int i=0;i<media.length;i++){
    30.              media[i].start();
    31.              media[i].setLooping(true);
    32.              media[i].setVolume(0,0);
    33.         }
    34.        
    35.                
    36.        //drums seekbar
    37.        
    38.         final SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
    39.         volControl.setMax(maxVolume);      
    40.         volControl.setProgress(0);
    41.         volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    42.                    
    43.            
    44.            
    45.             @Override
    46.             public void onStopTrackingTouch(SeekBar arg0) {
    47.                 // TODO Auto-generated method stub
    48.                
    49.             }
    50.                        
    51.            
    52.             @Override
    53.             public void onStartTrackingTouch(SeekBar arg0) {
    54.                 // TODO Auto-generated method stub
    55.             }
    56.            
    57.             @Override
    58.             public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
    59.                 // TODO Auto-generated method stub
    60.            
    61.                 media[0].setVolume(arg1, arg1);
    62.                
    63.             }                      
    64.            
    65.         });      
     

    Advertisement

  2. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    What was the solution?

    Also dont be afraid of ArrayLists<T>() :)
     

Share This Page

Loading...