I have a start activity which is using services to play a background sound and after 5 seconds another activity is loaded. The problem is in the second activity the sound doesn't load or service doesn't work... i'm not sure what is happening.
Sound works in the first activity when the app start.
here is the first activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//remove window title and make it fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//bind activity
setContentView(R.layout.start_activity);
ButterKnife.bind(this);
Intent intent = new Intent(StartActivity.this, SoundService.class);
intent.putExtra("filename", "audiostart");
//start service and start music
startService(intent);
int TIME_OUT = 5000;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(StartActivity.this, AvatarsActivity.class);
startActivity(i);
finish();
}
}, TIME_OUT);
Log.d(TAG, "APP Started!");
}
@Override
protected void onDestroy() {
//stop service and stop music
stopService(new Intent(StartActivity.this, SoundService.class));
super.onDestroy();
}
and the second activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.avatars_activity);
ButterKnife.bind(this);
Intent intent = new Intent(AvatarsActivity.this, SoundService.class);
intent.putExtra("filename", "audioavatars");
//start service and start music
startService(intent);
}
@Override
protected void onDestroy() {
//stop service and stop music
stopService(new Intent(AvatarsActivity.this, SoundService.class));
super.onDestroy();
}
here is the service:
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
player = MediaPlayer.create(this, R.raw.audio);
player.setLooping(false);
}
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null){
String mFilename = intent.getStringExtra("filename");
Toast toast = Toast.makeText(getApplicationContext(), "Filename: " + mFilename, Toast.LENGTH_SHORT);
toast.show();
}
player.start();
return Service.START_NOT_STICKY;
}
public void onDestroy() {
player.stop();
player.release();
stopSelf();
super.onDestroy();
}
I want a background sound when the second activity loads after 5 seconds passed in first activity.
And a second problem is that i want to pass a variable in onCreate method in service with what sound to play depending on the activity. (This task i think i can do it but doesn't hurt to ask opinions how to do it)