-1

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();
    }
  1. I want a background sound when the second activity loads after 5 seconds passed in first activity.

  2. 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)

Hasan Kucuk
  • 2,433
  • 6
  • 19
  • 41
  • current question is a bit confusing, it would be nice if you clearly define what are you trying to achieve and what doesn't work as expected – Ewoks Jun 22 '19 at 19:16

2 Answers2

0

Your code seems fine. But have you registered your service in your manifest file? Please check your manifest. Your service is not registered can be possible cause.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • doesn't really makes sense, cause app would crash if service is not registered in manifest. Besides, he said "Sound works in the first activity when the app start." – Ewoks Jun 20 '19 at 16:52
0

You are starting second activity after 5sec delay and immediately, after you queue start intent, you call finish() on first activity which will trigger onDestroy callback of the same activity. In onDestroy() of that 1st activity you implemented stop service, which causes service to stop.

Remove that implementation of onDestroy() in both activities and provide to user a way to stop service explicitly (clicking on a button or whatever) instead of doing it in a activity lifecycle callback.

Ewoks
  • 12,285
  • 8
  • 58
  • 67
  • I can't force the user stop the service because it needs to stop alone when next activity starts automatically after 5 seconds – user3192161 Jun 20 '19 at 17:03
  • I didn't mean to force user. It seems like you need to describe better what is your issue and what are you trying to achieve. From your 2nd sentence in the question and point #1 at the bottom, I understood that you want sound to keep on playing when 2nd activity is started. Android Service exist so it runs independently of GUI (activities), usually (but not necessary) even when no GUI is visible to the user, so it doesn't really make sense to start/stop service based on every activity lifecycle. Please describe your problem better so we can help – Ewoks Jun 20 '19 at 20:56