用于停止和启动Android媒体播放器的案例和切换方法

时间:2013-03-11 01:17:34

标签: android uri spinner android-mediaplayer media

我有一个微调器,你可以选择你想要流的质量,选择后你必须按停止按钮停止播放器,然后选择你的流,然后你必须选择播放按钮调到你的流选择。我可以自动执行此操作吗?这是我的代码。

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

int position = spinner.getSelectedItemPosition();
switch (position){
case 0:
    path = ("URL TO STREAM");
    break;

case 1:
    path = ("URL TO STREAM");
    break;

}


      }

      @Override
       public void onNothingSelected(AdapterView<?> arg0) {
   // TODO Auto-generated method stub

      }
     }

当我使用此代码时

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

int position = spinner.getSelectedItemPosition();
switch (position){
case 0:
            mp.stop();
    path = ("URL TO STREAM");
    mp.start();
            break;

case 1:
            mp.stop();
    path = ("URL TO STREAM");
    mp.start();
            break;

}


      }

      @Override
       public void onNothingSelected(AdapterView<?> arg0) {
   // TODO Auto-generated method stub

      }
     }

我的应用程序崩溃并且不确定为什么上面的代码不起作用,我不会在Eclipse中出现错误,实际上我正在尝试停止媒体播放器,切换URI并启动媒体播放器,当用户选择高或旋转器质量低。

这是我的logcat:

03-10 18:54:51.041: D/AndroidRuntime(284): >>>>>>>>>>>>>> AndroidRuntime START          
03-10 18:54:51.041: D/AndroidRuntime(284): CheckJNI is ON
03-10 18:54:51.201: D/AndroidRuntime(284): --- registering native functions ---
03-10 18:54:51.851: D/AndroidRuntime(284): Shutting down VM
03-10 18:54:51.861: D/dalvikvm(284): Debugger has detached; object registry had 1      entries
03-10 18:54:51.871: I/AndroidRuntime(284): NOTE: attach of thread 'Binder Thread #3'    failed
03-10 18:54:52.251: D/AndroidRuntime(292): >>>>>>>>>>>>>> AndroidRuntime START     <<<<<<<<<<<<<<
03-10 18:54:52.251: D/AndroidRuntime(292): CheckJNI is ON
03-10 18:54:52.421: D/AndroidRuntime(292): --- registering native functions ---
03-10 18:54:53.071: I/ActivityManager(58): Force stopping package com.slaviccenter.radio    uid=10037
03-10 18:54:53.081: I/Process(58): Sending signal. PID: 273 SIG: 9
03-10 18:54:53.165: I/WindowManager(58): WIN DEATH: Window{4606c8b0     com.slaviccenter.radio/com.slaviccenter.radio.MainActivity paused=false}
03-10 18:54:53.181: I/UsageStats(58): Unexpected resume of com.android.launcher while   already resumed in com.slaviccenter.radio
03-10 18:54:53.211: W/InputManagerService(58): Got RemoteException sending    setActive(false) notification to pid 273 uid 10037
03-10 18:54:53.271: I/ActivityManager(58): Starting activity: Intent {    act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000    cmp=com.slaviccenter.radio/.MainActivity }
03-10 18:54:53.271: D/AndroidRuntime(292): Shutting down VM
03-10 18:54:53.291: D/dalvikvm(292): Debugger has detached; object registry had 1 entries
03-10 18:54:53.321: I/dalvikvm(292): JNI: AttachCurrentThread (from ???.???)
03-10 18:54:53.321: I/AndroidRuntime(292): NOTE: attach of thread 'Binder Thread #3' failed
03-10 18:54:53.491: I/ActivityManager(58): Start proc com.slaviccenter.radio for activity com.slaviccenter.radio/.MainActivity: pid=299 uid=10037 gids={3003}
03-10 18:54:53.851: W/ActivityThread(299): Application com.slaviccenter.radio is waiting for the debugger on port 8100...
03-10 18:54:53.901: I/System.out(299): Sending WAIT chunk
03-10 18:54:53.921: I/dalvikvm(299): Debugger is active
03-10 18:54:54.112: I/System.out(299): Debugger has connected
03-10 18:54:54.112: I/System.out(299): waiting for debugger to settle...
03-10 18:54:54.311: I/System.out(299): waiting for debugger to settle...
03-10 18:54:54.511: I/System.out(299): waiting for debugger to settle...
03-10 18:54:54.711: I/System.out(299): waiting for debugger to settle...
03-10 18:54:54.921: I/System.out(299): waiting for debugger to settle...
03-10 18:54:55.127: I/System.out(299): waiting for debugger to settle...
03-10 18:54:55.322: I/System.out(299): waiting for debugger to settle...
03-10 18:54:55.521: I/System.out(299): waiting for debugger to settle...
03-10 18:54:55.732: I/System.out(299): waiting for debugger to settle...
03-10 18:54:55.933: I/System.out(299): debugger has settled (1369)
03-10 18:54:56.271: W/WindowManager(58): No window to dispatch pointer action 0
03-10 18:54:56.421: W/WindowManager(58): No window to dispatch pointer action 1
03-10 18:55:03.275: W/ActivityManager(58): Launch timeout has expired, giving up wake lock!
03-10 18:55:03.751: W/ActivityManager(58): Activity idle timeout for HistoryRecord{460b4c78 com.slaviccenter.radio/.MainActivity}

在最后一行之后,“Activity idle timeout”调试器将我带到MainActivity.java

    int position = spinner.getSelectedItemPosition();
switch (position){
case 0:
            mp.stop(); <---------DEBUGGER HIGHLIGHTS THIS GREEN
    path = ("URL TO STREAM");
    mp.start();
            break;

case 1:
            mp.stop();
    path = ("URL TO STREAM");
    mp.start();
            break;

----新LOGCAT --------

03-10 20:45:54.440: W/ActivityThread(375): Application com.slaviccenter.radio is waiting for the debugger on port 8100...
03-10 20:45:54.460: I/System.out(375): Sending WAIT chunk
03-10 20:45:54.522: I/dalvikvm(375): Debugger is active
03-10 20:45:54.661: I/System.out(375): Debugger has connected
03-10 20:45:54.661: I/System.out(375): waiting for debugger to settle...
03-10 20:45:54.871: I/System.out(375): waiting for debugger to settle...
03-10 20:45:55.071: I/System.out(375): waiting for debugger to settle...
03-10 20:45:55.270: I/System.out(375): waiting for debugger to settle...
03-10 20:45:55.471: I/System.out(375): waiting for debugger to settle...
03-10 20:45:55.683: I/System.out(375): waiting for debugger to settle...
03-10 20:45:55.881: I/System.out(375): waiting for debugger to settle...
03-10 20:45:56.081: I/System.out(375): waiting for debugger to settle...
03-10 20:45:56.288: I/System.out(375): waiting for debugger to settle...
03-10 20:45:56.490: I/System.out(375): waiting for debugger to settle...
03-10 20:45:56.692: I/System.out(375): debugger has settled (1448)
03-10 20:46:06.240: D/AndroidRuntime(375): Shutting down VM
03-10 20:46:06.240: W/dalvikvm(375): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-10 20:46:06.321: E/AndroidRuntime(375): FATAL EXCEPTION: main
03-10 20:46:06.321: E/AndroidRuntime(375): java.lang.NullPointerException
03-10 20:46:06.321: E/AndroidRuntime(375):  at com.slaviccenter.radio.MainActivity.onItemSelected(MainActivity.java:296)
03-10 20:46:06.321: E/AndroidRuntime(375):  at  android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
03-10 20:46:06.321: E/AndroidRuntime(375):  at android.widget.AdapterView.access$200(AdapterView.java:42)
03-10 20:46:06.321: E/AndroidRuntime(375):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
03-10 20:46:06.321: E/AndroidRuntime(375):  at android.os.Handler.handleCallback(Handler.java:587)
03-10 20:46:06.321: E/AndroidRuntime(375):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-10 20:46:06.321: E/AndroidRuntime(375):  at android.os.Looper.loop(Looper.java:123)
03-10 20:46:06.321: E/AndroidRuntime(375):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-10 20:46:06.321: E/AndroidRuntime(375):  at java.lang.reflect.Method.invokeNative(Native Method)
03-10 20:46:06.321: E/AndroidRuntime(375):  at java.lang.reflect.Method.invoke(Method.java:521)
03-10 20:46:06.321: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-10 20:46:06.321: E/AndroidRuntime(375):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-10 20:46:06.321: E/AndroidRuntime(375):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

在停止之前检查mp是否正在播放

    @Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//    check mp
if(mp !=null){
   int position = spinner.getSelectedItemPosition();
   switch (position){
   case 0:
         if(mp.isPlaying())   mp.stop();
    path = ("URL TO STREAM");
    mp.start();
            break;

   case 1:
         if(mp.isPlaying())   mp.stop();
       path = ("URL TO STREAM");
       mp.start();
            break;

}
}

希望这有助于你。