English 中文(简体)
选择值和停止介质播放器
原标题:android spinner select value and stop mediaplayer
  • 时间:2012-05-27 12:55:04
  •  标签:
  • java
  • android

我有这个代码:

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;
    }



}

这个代码,就是我从旋转器得到价值的地方:

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

            }

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

它应该暂停媒体播放器, 而不是我得到错误。 这是来自日志 :

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)

那么我做错什么了?当我选择用来暂停媒体玩家的 spinner 值时,是什么导致应用程序崩溃?

最佳回答

您的 Spinner 对象在 onCreate () 方法中声明,该方法完全隐藏在 onItemsects () 方法下,只需在介质Play 对象下方的级别上声明, 如下面,

 private MediaPlayer mediaPlayer;
 private Spinner spinner;
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签