android spinner selected value stop mediaplayer

All your problems with Audio, Video and Images.

android spinner selected value stop mediaplayer

Postby bizkit1 » Sun May 27, 2012 3:41 pm

So i have this code:

Code: Select all
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:

Code: Select all
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?
bizkit1
Once Poster
Once Poster
 
Posts: 1
Joined: Sun May 27, 2012 3:37 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 13 guests