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 7985961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:42:36+00:00 2026-06-04T11:42:36+00:00

So i have this code: public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener,

  • 0

So i have this code:

public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener, OnItemSelectedListener{
    TextView textMsg, textPrompt;
    ImageView imageView;
    final String textSource = "http://www.sdads.ro/sdsad.php";
    private ImageButton buttonPlayPause;
    protected EditText editTextSongURL;

    private MediaPlayer mediaPlayer;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        Button btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
                System.exit(0);
            }
        });

        imageView = (ImageView) findViewById(R.id.imagine);
        textPrompt = (TextView)findViewById(R.id.textprompt);
        textMsg = (TextView)findViewById(R.id.textmsg);
        textMsg.post( new Runnable(){

            public void run(){


        URL textUrl;
        try {

            textUrl = new URL(textSource);
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
            String StringBuffer;
            String stringText = "";
            while ((StringBuffer = bufferReader.readLine()) != null) {
                stringText += StringBuffer;
            }
            bufferReader.close();
            textMsg.setText(stringText);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        }
        textMsg.postDelayed(this,(1000*10));
            }

        });


        initView();
        spinner.setOnItemSelectedListener(new StreamingMp3Player());
    }




    private void initView() {

        buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
        buttonPlayPause.setOnClickListener(this);


        editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
        editTextSongURL.setText("Romantic FM");

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnBufferingUpdateListener(this);
        mediaPlayer.setOnCompletionListener(this);
    }



    public void onClick(View v) {
        if(v.getId() == R.id.ButtonTestPlayPause){

            try {
                mediaPlayer.setDataSource("http://dfgdgfdgf:8080"); 

                mediaPlayer.prepare(); 
            } catch (Exception e) {
                e.printStackTrace();
            }

            //mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); 

            if(!mediaPlayer.isPlaying()){
                mediaPlayer.start();
                //buttonPlayPause.setBackgroundColor(Color.RED);
                buttonPlayPause.setImageResource(R.drawable.button_pause);
            }else {
                mediaPlayer.pause();
                buttonPlayPause.setImageResource(R.drawable.button_play);
                //System.out.println("Pauza");

            }


        }
    }

     public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
            if(pos == 1) {
                mediaPlayer.pause();
            }

            }

            public void onNothingSelected(AdapterView parent) {
              // Do nothing.
            }

    public void onCompletion(MediaPlayer mp) {

        buttonPlayPause.setImageResource(R.drawable.button_play);

    }


    public void onBufferingUpdate(MediaPlayer arg0, int arg1) {
        // TODO Auto-generated method stub

    }


    public boolean onTouch(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub
        return false;
    }



}

And this code, is where i get the value from the spinner:

public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
            if(pos == 1) {
                mediaPlayer.pause();
            }

            }

            public void onNothingSelected(AdapterView parent) {
              // Do nothing.
            }

It is supposed to pause mediaplayer, instead i get error.This is from logcat:

05-27 12:39:05.554: E/AndroidRuntime(1432): FATAL EXCEPTION: main
05-27 12:39:05.554: E/AndroidRuntime(1432): java.lang.NullPointerException
05-27 12:39:05.554: E/AndroidRuntime(1432):     at com.hrupin.streamingmedia.StreamingMp3Player.onItemSelected(StreamingMp3Player.java:161)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at android.widget.AdapterView.access$200(AdapterView.java:42)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at android.os.Handler.handleCallback(Handler.java:587)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at android.os.Looper.loop(Looper.java:123)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at java.lang.reflect.Method.invokeNative(Native Method)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at java.lang.reflect.Method.invoke(Method.java:507)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-27 12:39:05.554: E/AndroidRuntime(1432):     at dalvik.system.NativeStart.main(Native Method)

So what am i doing wrong? What is causing the app to crash when i select the spinner value which is supposed to pause mediaplayer?

  • 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-06-04T11:42:37+00:00Added an answer on June 4, 2026 at 11:42 am

    Your Spinner object is declared in the OnCreate() method which is totally hidden from the onItemSelected() method, just declare it on class level below the MediaPlayer object, like below,

     private MediaPlayer mediaPlayer;
     private Spinner spinner;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: public class VoiceActivity extends Activity implements OnClickListener { private TextView
I have this code for my soundboard, public class soundboardActivity extends Activity { View
I have this code: public class Chronom extends Activity { Chronometer mChronometer; ProgressBar progre;
I have this code: public class Home extends Activity{ @Override public void onCreate(Bundle savedInstanceState)
I have this code: import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.widget.TextView; public class
I have this code: public class Window extends JFrame { public Window(){ ... JButton
I have this code that describes a service: public class navigation_web extends Service {
I have class where I draw image. This is code: public class CanvasdrawActivity extends
I have this code fragment: public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
I'm a beginner in Android. Here I have this code: public class GPS extends

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.