P. Grunder P. Grunder - 6 months ago 71
Android Question

Android Studio add Music to Background over multiple Activities without repeat

so im fairly new to coding and im trying to implement a Soundfile to the Background of my Application, which will play through multiple Activities without Stopping and playing it all over again (like a flow). I found an Answer online by using AsyncTask, but it just tells me that my other Class is "not enclosed" Does anyone have a suggestion how i could use it easier or more efficient? I will include the code here:

public class BackgroundSound extends AsyncTask<Void, Void, Void> {

MediaPlayer mMediaPlayer;

protected void onPreExecute() {
mMediaPlayer = MediaPlayer.create(Splashscreen.this, R.raw.mainmenu);
//Splashscreen.this is "not an enclosing class"
}
protected Void doInBackground(Void... params) {
mMediaPlayer.setLooping(true); // Set looping
mMediaPlayer.setVolume(100,100);
mMediaPlayer.start();
return null;
}
protected void onCancelled(Void v) {
mMediaPlayer.stop();
mMediaPlayer.release();
}


Splashscreen Code:

public class Splashscreen extends AppCompatActivity {

private View mContentView;
BackgroundSound mBackgroundSound;

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

Thread splashTread;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
mContentView = findViewById(R.id.lin_lay);
hide();
StartAnimations();
mBackgroundSound = new BackgroundSound();

}

public void onResume() {
super.onResume();
mBackgroundSound.execute();

}

public void onPause() {
super.onPause();
mBackgroundSound.cancel(true);
}

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.splash);
iv.clearAnimation();
iv.startAnimation(anim);

splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;

while (waited < 3500) {
sleep(100);
waited += 100;
}
Intent intent = new Intent(Splashscreen.this,
MainMenu.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
Splashscreen.this.finish();
} catch (InterruptedException e) {
} finally {
Splashscreen.this.finish();
}

}
};
splashTread.start();

}
private void hide() {
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}


Thanks in advance!

Answer

Use service for the job:

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.your_sound_file);
        player.setLooping(true); 
        player.setVolume(100,100);

    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return 1;
    }

    public void onStart(Intent intent, int startId) {

    }
    public IBinder onUnBind(Intent arg0) {
        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }
}

Start it with intent from your onCreate() method in Splashscreen:

Intent svc = new Intent(this, BackgroundSoundService.class);
startService(svc);
Comments