0

So I have a collection of mp3 files in my raw folder. Each mp3 belongs to a category. From that category, the user can select one of eight mp3s to play.

For example: 1.mp3 2.mp3 3.mp3 4.mp3 5.mp3 6.mp3

mp3 (1-3) Belong to the category smooth jazz while, mp3(3-6) belong to alternative rock.

Reiterate the same method in each class, I'd rather have one method in the main activity, that I can call, from each class.

MediaPlayer mp3;
public void musicPlayer(//Here is where I want to have a variable x){
    mp3 = MediaPlayer.create(this, R.raw.//Here is where variable x goes);
    PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (state == TelephonyManager.CALL_STATE_RINGING) {
                mp3.pause();
            } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                mp3.start();

            } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                mp3.pause();
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };
    TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    if(mgr != null) {
        mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

I want to instantiate variable x when I call the method musicPlayer, from a separate class.

  • Check [this *Stackoverflow*](http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) out. But this is usually not the good way of doing it—depending on how your project is laid out it’s better sending them as arguments to constructors, in order for your project’s implementation not to be as visible. – D. Ataro Jan 23 '17 at 00:01

2 Answers2

0

I see two ways this could be done with very little effort, especially since you only need 8 possible MP3 files.

  1. The "variable x" could be passed in as the fully-qualified 'R' constant. That means the caller has to know which of the 8 items to pass in. Actually, if you change so the user can somehow browse all possible candidates in R.raw, this still works. It means the "x" variable does not quite go where you placed it, however.
  2. The "variable x" could be something completely unknown to the R object, and instead you could have a decoder working to map that to the R.raw.whatever constant. This is also made feasible by having only a few possible values. Unfortunately, using this approach makes things less flexible. In future, were you to add more of these MP3s, a code change (expand mapping) would be required. Again, the "x" variable becomes something different.

Also, since only 8 possibilities are possible, and all are presumably residing in the R.raw directory, controlling what goes into that method is not such a terrible thing.

I hope this helps.

0

For the reusability of methods you should create a separate class MusicPlayer() with x as a class Attribute and musicPlayer as a class method. Then you could create an object e.g. myMusicPlayer of type MusicPlayer in the class containing your main method or in any other class of your Project where you need the musicPlayer method to be called.

e.g.

MusicPlayer.java

public class MusicPlayer{

    MediaPlayer mp3;
    int x;

    //ToDo: constructor(s)
    //get and set methods for the Attributes of an object of type MusicPlayer (optional)

    public void musicPlayer(){

    //ToDo: your code
    }

 }

MainClass.java

                  ...

MusicPlayer myMusicPlayer = new MusicPlayer();

                  ...

doSomethingWith(myMusicPlayer.x);

                  ...

doSomethingWith(myMusicPlayer.mp3);

Good luck!