0

I am trying to build a splash screen with music in the background, but the music doesn't play.

Here is my code:

    package com.example.thebasicseries;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;  

public class MainActivity extends ActionBarActivity {
    MediaPlayer logoMusic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno );
        Thread logoTimer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                    Intent menuIntent = new Intent("com.example.thebasicseries.MENU");
                    startActivity(menuIntent);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally{
                    finish();
                }
            }

        };
        logoTimer.start();
        logoMusic.release();
    }

There isn't any error message showing before or after running the app.

Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
jass
  • 11
  • You need logoMusic.play() to play music. I only see create and release. – Wildroid Nov 14 '14 at 13:59
  • there is not .play() as i tried. it was working before but i don't understand why it is stop working, maybe because i've been playing around the code. – jass Nov 14 '14 at 14:04
  • I suggest you use AsyncTask for background music playing in its own thread - see this sample [link](http://stackoverflow.com/a/16338215/4123144) – Wildroid Nov 14 '14 at 14:15

3 Answers3

0

LOL, you created the MP :

logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno );

But never play it :

logoMusic.play(); 
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
0

better you cut particular portion for your splash screen from video and use it as like gif image it will work

0

i have got it work!

    package com.example.thebasicseries;


import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {
    MediaPlayer logoMusic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        logoMusic = MediaPlayer.create(MainActivity.this,R.raw.techno );
        logoMusic.start();
        Thread logoTimer = new Thread(){
            public void run(){
                try{
                    sleep(5000);
                    Intent menuIntent = new Intent("com.example.thebasicseries.MENU");
                    startActivity(menuIntent);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally{
                    finish();
                }
            }

        };
        logoTimer.start();

    }
    @Override //so i just create an onPause method and release my song

    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        logoMusic.release(); //while it is onPause i release the song
    }


}
jass
  • 11