views:

180

answers:

2

I have this code:

    hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
     public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
      final MediaPlayer mp2 = MediaPlayer.create(Textbox.this, R.raw.hero);
      mp2.start();
        }
        public void onNothingSelected(AdapterView<?> parentView) {
        }
    });

(The code basically runs when a new item is selected of a spinner and then plays a song, -which later will be a variable based on what was picked, but i'm fine as it is for now)

Problem:

And I want to be able to use 'mp2' out of this public void, (I want a button which pauses it) How can I do this?

Please explain/show...

Thanks alot

James

A: 

I'm not sure what happens when you call MediaPlayer.create(Textbox.this, R.raw.hero), but assuming it has no immediate effects, you can just create the object outside of the listener.


Edit1: OK, then how about this?

MediaPlayer currentPlayer;

methodA()
{
   hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
   {
       public void onItemSelected(AdapterView<?> parentView,
             View selectedItemView, int position, long id)
       {
          MediaPlayer mp2 = MediaPlayer.create(Textbox.this, R.raw.hero);
          mp2.start();
          setPlayer(mp2);
       }
       public void onNothingSelected(AdapterView<?> parentView) {
    }
});


setMediaPlayer(MediaPlayer player)
{
   currentPlayer = player;
}
Chris
Yeah I know, but as I said in future (after fixing this problem) i am going to use 'R.raw.****Variable value from spinner*****' instead -to find the mp3 track. -So it can't go outside, -or can it? :)
James Rattray
I don't know where R.raw comes from unfortunately, I have no Android experience, but it doesn't sound like a local variable, so you could probably put it anywhere you want.
Chris
Never mind then :) Thanks anyway
James Rattray
A: 

Move the variable mp2 to the instance of the parent class. This will keep a running reference to it which you can interact with whenever you please. You will need to remove the final qualifier though if MediaPlayer.create(...) will be called more than once and stored.


edit:

I was referring to something along the lines of this:

class SomeClass {
    private MediaPlayer mp2 = null;

    private void whateverFunctionYouAreIn() {
        hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                SomeClass.this.mp2 = MediaPlayer.create(Textbox.this, R.raw.hero);
                SomeClass.this.mp2.start();
            }
            public void onNothingSelected(AdapterView<?> parentView) {}
        });

        //TODO: put this in an onClickListener:
        if (this.mp2 != null) {
            this.mp2.pause();
        }
    }
}
Jake Wharton
I need it inside... (For reasons put on the comment on Chris's answer)
James Rattray
See my edit. Exactly the same as your code except you are storing the `MediaPlayer` object in the main class instance.
Jake Wharton
So then I just put the (pause) button in the 'Some Class' -but not in the private void? -and it will work?
James Rattray
You can put the pause stuff anywhere you'd like. I assume you are doing all this in the `onCreate` method (if I remember correctly) so when you do `pauseButton.setOnClickListener(new OnClickListener() { ... });` put the `TODO` section in there.
Jake Wharton
Ok yeah I think that is working, except one problem, it doesnt run atall, -no music is ever played so i'm thinking there is a problem with the private function, 'private void whateverFunctionYouAreIn() {' -Does that run with/by itself? or what? -or the class? is that being called -or does it not need too?
James Rattray
No. You should put it in the `onCreate` method (or whichever method you were originally placing your original code snippet).
Jake Wharton