I want to embed sound in the splash screen . And it must be stopped when Activity change

时间:2017-06-15 10:07:59

标签: java android xml

SplashScreen.java-

package com.example.android.nxtgendataingestion;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import gr.net.maroulis.library.EasySplashScreen;

public class SplashScreen extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EasySplashScreen config=new EasySplashScreen(SplashScreen.this)
                .withFullScreen()
                .withTargetActivity(MainActivity.class)
                .withSplashTimeOut(3000)
                .withBackgroundColor(Color.parseColor("#ebedef"))
                .withLogo(R.drawable.nxtgenlogo)
                .withAfterLogoText("NxtGen Grafana Dashboard");


        //Set Text Color

        View view=config.create();
        setContentView(view);


    }
}

I am working on a project i want to use .ogg extension sound clip for this activity and it must be stopped when change to other activity.

1 个答案:

答案 0 :(得分:0)

make raw folder in app : put sound file (here hangout_ringtone) in raw folder `try this :

     EasySplashScreen config=new EasySplashScreen(SplashScreen.this)
                    .withFullScreen()
                    .withTargetActivity(MainActivity.class)
                    .withSplashTimeOut(3000)
                    .withBackgroundColor(Color.parseColor("#ebedef"))
                    .withLogo(R.drawable.nxtgenlogo)
                    .withAfterLogoText("NxtGen Grafana Dashboard");


            //Set Text Color

            View view=config.create();
            setContentView(view);
    final MediaPlayer player = new MediaPlayer();
AssetFileDescriptor afd = this.getResources().openRawResourceFd(R.raw.hangout_ringtone);
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.prepare();
    player.start();
                // wait 3 sec... then stop the player.
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        player.stop();
                    }
                }, 3000);
    `