Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: MediaPlayer.

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MediaPlayer.

    Hi,

    Why does my MediaPlayer stop when I change the layout?

     
    MediaPlayer mp = MediaPlayer.create(this, R.raw.background_song);
    mp.setLooping(true);
    mp.start();
     
    setContentView(R.layout.menu_screen);

    Thanks in advance


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    18
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: MediaPlayer.

    First set the layout file

    setContentView(R.layout.menu_screen);
    MediaPlayer mp = MediaPlayer.create(this, R.raw.background_song);
    mp.setLooping(true);
    mp.start();

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MediaPlayer.

    I cant because I have set it so that when I click on a button the this is called: setContentView(R.layout.menu_screen);

    I tried making a different activity for the menu_screen.xml layout but when I changed to that one the audio stopped, so i tried staying in the same activity bt again the audio/MediaPlayer stops.

    Basically im trying to get audio to play constantly, and continue playing when i change the screen, how can i do this?

    Thanks

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MediaPlayer.

    Ive created a service which plays the audio, but still when i change the layout on the activity the audio stops?

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MediaPlayer.

    I got it working now, just to let you know why it wasnt working:

    I changed this:

    public class BackgroundService extends Service{
     
    	@Override
    	public IBinder onBind(Intent intent) {
    		// TODO Auto-generated method stub
    		return null;
     
    	}
     
    	public void onCreate(){
     
    		MediaPlayer mp = MediaPlayer.create(this, R.raw.background_song);
    		mp.start();
    		mp.setLooping(true);
    		mp.setVolume(100, 100);
     
    	}
     
     
    }


    To this:

    public class BackgroundService extends Service{
     
    	MediaPlayer mp; 
     
    	@Override
    	public IBinder onBind(Intent intent) {
    		// TODO Auto-generated method stub
    		return null;
     
    	}
     
    	public void onCreate(){
    		mp = MediaPlayer.create(this, R.raw.background_song);
    		mp.start();
    		mp.setLooping(true);
    		mp.setVolume(100, 100);
     
    	}
     
     
    }