• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps MediaPlayer array causing nullpointer

itzjonno

Lurker
**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:
 private MediaPlayer[] media;
	
		
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
          
                
        //media players
        
        media[0] = MediaPlayer.create(this, R.raw.drums);
        
        
        
        media[1] = MediaPlayer.create(this, R.raw.bass);
        
              
        media[2] = MediaPlayer.create(this, R.raw.synth);
        
        
        media[3] = MediaPlayer.create(this, R.raw.snare);
       
        
        media[4]  = MediaPlayer.create(this, R.raw.wobble);
        
        
        for(int i=0;i<media.length;i++){
        	 media[i].start();
        	 media[i].setLooping(true);
        	 media[i].setVolume(0,0);
        }
        
               
       //drums seekbar
        
        final SeekBar volControl = (SeekBar)findViewById(R.id.volbar);
        volControl.setMax(maxVolume);       
        volControl.setProgress(0);
        volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
			      	
        	
        	
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
						
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
			
				media[0].setVolume(arg1, arg1);
				
			}			      		
			
		});
 
Back
Top Bottom