Search code examples
androidmultithreadingandroid-asynctaskmp3splash-screen

Android splash activity with mp3 tone


In my android application I want to play mp3 tone while it loads the splash activity.I have already finished my animated splash activity.here is my code .please someone help me to playing mp3 tone .

package com.bni.www;

 import android.app.Activity;
 import android.content.Intent;
 import android.graphics.PixelFormat;
 import android.os.Bundle;
 import android.os.CountDownTimer;
 import android.view.Window;
 import android.view.WindowManager;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.widget.ImageView;
 import android.widget.LinearLayout;


public class Main extends Activity {



 public void onAttachedToWindow() {
     super.onAttachedToWindow();
     Window window = getWindow();
     window.setFormat(PixelFormat.RGBA_8888);
    }


   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

 setContentView(R.layout.spalsh);
 CountDown _tik;
 _tik=new CountDown(12000,100,this,Login.class);
 //Intent intent=new Intent(Main.this,Login.class);   
 _tik.start();
 StartAnimations();
 //startActivity(intent);
}
  private void StartAnimations() {
 Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
 anim.reset();
 LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
 l.clearAnimation();
 l.startAnimation(anim);

 anim = AnimationUtils.loadAnimation(this, R.anim.translate);
 anim.reset();
 ImageView iv = (ImageView) findViewById(R.id.logo);
 iv.clearAnimation();
 iv.startAnimation(anim);

   }



 }


 class CountDown extends CountDownTimer{
 private Activity _act;
 private Class _cls;
 public CountDown(long millisInFuture, long countDownInterval,Activity act,Class cls) {
 super(millisInFuture, countDownInterval);
 _act=act;
 _cls=cls;
 }
 @Override
 public void onFinish() {
 _act.startActivity(new Intent(_act,_cls));
 _act.finish();
 }
@Override
public void onTick(long millisUntilFinished) {

 }
}

Solution

  • Use this code to play audio.

    mp = MediaPlayer.create(yourclassname.this, R.raw.mysound);
                            mp.setOnCompletionListener(new OnCompletionListener() {
    
                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    // TODO Auto-generated method stub
                                    mp.release();
                                }
    
                            });   
                            mp.start();