Saturday, 1 April 2023

Wierd Result when trying background service

I am trying to make a tic tac toe game on android. It has a few variants also. I have created a Background service class. The code for the same is as under.

package com.sudarshan.tictactoenew;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Service;
import android.util.Log;

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
   // Intent intent;

    MediaPlayer mp;
    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();


    }
    public int onStartCommand(Intent intent, int flags, int startId) {

         mp = new MediaPlayer();

        Bundle extras = intent.getExtras();
        String itemname = (String) extras.get("itemname");
        Log.i("Itemname",itemname);
        if(itemname.equals("menupage"))
        {

            String filename = "android.resource://" + this.getPackageName() + "/raw/got";

            try {
                mp.setDataSource(this, Uri.parse(filename));
                mp.prepareAsync();
                mp.setLooping(true); // Set looping
                mp.setVolume(100,100);
                mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });
                    ;
            } catch(Exception e){
                e.printStackTrace();
            }
            // = MediaPlayer.create(this, R.raw.got);

           // player1.start();
        }
        else if(itemname.equals("classictictactoe"))
        {

            Log.i("Inside","Inside classic tic tac toe");
            String filename = "android.resource://" + this.getPackageName() + "/raw/cof";

            try {
                mp.setDataSource(this, Uri.parse(filename));
                mp.prepareAsync();
                mp.setLooping(true); // Set looping
                mp.setVolume(100,100);
                mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {

                        mp.start();
                    }
                });
                
            } catch(Exception e){
                e.printStackTrace();
            }
            
        }


        return startId;
    }

    public void onStart(Intent intent, int startId) {
        // TO DO
    }
    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {

    }
    public void onPause() {

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


    }

    @Override
    public void onLowMemory() {

    }
}

This code does not get executed Log.i("Inside","Inside classic tic tac toe"); Below is my portion of a log file

2022-10-02 21:44:36.475 13019-13019 Itemname                com.sudarshan.tictactoenew           I  menupage
2022-10-02 21:45:01.080 13019-13019 Itemname                com.sudarshan.tictactoenew           I  classictictactoe
2022-10-02 21:57:39.288 13019-13019 Itemname                com.sudarshan.tictactoenew           I  menupage
2022-10-02 21:57:46.764 13019-13019 Itemname                com.sudarshan.tictactoenew           I  classictictactoe
2022-10-02 21:57:57.114 13019-13019 Itemname                com.sudarshan.tictactoenew           I  menupage
2022-10-02 21:58:03.626 13019-13019 Itemname                com.sudarshan.tictactoenew           I  menupage
2022-10-02 21:58:08.837 13019-13019 Itemname                com.sudarshan.tictactoenew           I  classictictactoe
---------------------------- PROCESS ENDED (13019) for package com.sudarshan.tictactoenew ----------------------------
---------------------------- PROCESS STARTED (13341) for package com.sudarshan.tictactoenew ----------------------------
---------------------------- PROCESS ENDED (13341) for package com.sudarshan.tictactoenew ----------------------------
---------------------------- PROCESS STARTED (13430) for package com.sudarshan.tictactoenew ----------------------------
2022-10-02 21:59:31.086 13430-13430 Itemname                com.sudarshan.tictactoenew           I  menupage
2022-10-02 21:59:36.137 13430-13430 Itemname                com.sudarshan.tictactoenew           I  classictictactoe

Both menupage and classictictactoe are activities. Music should play whenever the activity opens(both menupage and classictictactoe) But no music plays when activity classictictactoe opens,music only plays when menupage activity opens

//Portion of relevant code of classictictactoe activity

Intent  intent = new Intent(ClassicTicToe.this, BackgroundSoundService.class);
        intent.putExtra("itemname","classictictactoe");
        startService(intent);

//Portion of relevant code of menupage activity

i = new Intent(MenuPage.this, BackgroundSoundService.class);
       i.putExtra("itemname","menupage");
        startService(i);

I do not understand why music in both activities is not playing

I have music files cof.mp3 and got.mp3 in the raw folder



from Wierd Result when trying background service

No comments:

Post a Comment