0

ストリーミングする品質を選択できるスピナーがあります。選択したら、停止ボタンを押してプレーヤーを停止し、ストリームを選択してから、選択したストリームに合わせて再生ボタンを選択する必要があります。これを自動的に行うことはできますか? これは私が持っているコードです。

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

----新しいログキャット--------

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)
4

1 に答える 1

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;

}
}

これがお役に立てば幸いです。

于 2013-03-11T03:47:56.090 に答える