Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / Java

Adding Background Music to Android App

Rate me:
Please Sign up or sign in to vote.
3.89/5 (9 votes)
23 Sep 2011CPOL5 min read 243.2K   14.7K   25  
This article explains how to add background music while developing Android Apps, e.g., Games
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class MusicService extends Service  implements MediaPlayer.OnErrorListener{
	
    private final IBinder mBinder = new ServiceBinder(); 
    MediaPlayer mPlayer;
    private int length = 0;
   
    public MusicService() { } 
    
    public class ServiceBinder extends Binder {
     	 MusicService getService()
    	 {
    		return MusicService.this;
    	 }
    }
  
    @Override
    public IBinder onBind(Intent arg0){return mBinder;}

    @Override
    public void onCreate (){
	  super.onCreate(); 
        
       Player = MediaPlayer.create(this, R.raw.jingle);
       mPlayer.setOnErrorListener(this);
        
       if(mPlayer!= null)
        {
        	mPlayer.setLooping(true);
        	mPlayer.setVolume(100,100);
        }
   	

        mPlayer.setOnErrorListener(new OnErrorListener() {

	  public boolean onError(MediaPlayer mp, int what, int       
          extra){
		
			onError(mPlayer, what, extra);
			return true;
		}
    	  });
	}
	

    @Override
	public int onStartCommand (Intent intent, int flags, int startId)

	{
         mPlayer.start();
         return START_STICKY;

	}
	
	public void pauseMusic()
	{
		if(mPlayer.isPlaying())
		{
			mPlayer.pause();
			length=mPlayer.getCurrentPosition(); 

		}
	}
	
	public void resumeMusic()
	{
		if(mPlayer.isPlaying()==false)
		{
			mPlayer.seekTo(length);
			mPlayer.start();
		}
	}
	
	public void stopMusic()
	{
		mPlayer.stop();
		mPlayer.release();
		mPlayer = null;
	}
	

	@Override
	public void onDestroy ()

	{
		super.onDestroy();
		if(mPlayer != null)
		{
		try{
		 mPlayer.stop();
		 mPlayer.release();
			}finally {
				mPlayer = null;
			}
		}

	}

	public boolean onError(MediaPlayer mp, int what, int extra) {
			
		Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show();	
		if(mPlayer != null)
		{
			try{
				mPlayer.stop();
				mPlayer.release();
			}finally {
				mPlayer = null;
			}
		}
		return false;
	}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions