Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 732339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:07:49+00:00 2026-05-14T07:07:49+00:00

I have this code: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int

  • 0

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: 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:07:49+00:00Added an answer on May 14, 2026 at 7:07 am

    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();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code which doesnt work: void CAnalyzerIF::SetData(int i_iIters, const CParameterSet* i_CParameterSet) {
I have this code private void writeReport(IReport report, string reportName) { string reportString =
I have this code: foreach(int i in Directions) { if (IsDowner(i)) { while (IsDowner(i))
I have this code : IList<MyObject> myObjects = new List<MyObject>(); if (param != null)
I have this code: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { mouseSwiped = NO; UITouch
I have this code in a single file : public class genIntro { public
I have this code: ImageButton call = new ImageButton(context); call.setId(9001+result.index); call.setBackgroundResource(R.drawable.small_call); LinearLayout.LayoutParams call_params =
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code but it doesn't work! public class Trial { public static
I have this code: Dim Tasks As New List(Of Task) Tasks.Add(Task.Factory.StartNew(Sub() 'Do whatever End

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.