0

the issue流在線廣播,主線程

的工作太多了,我想實現一個簡單的應用程序與遊戲的在線廣播流媒體和暫停按鈕。我在onCreate()方法中做的唯一事情就是在單擊按鈕時啓動和停止服務,並且當我啓動服務並且收音機開始緩衝時應用程序正在拋出一條消息,說「該應用程序無法工作「,選擇」關閉應用程序/等待「,並在logCat中表示該應用程序在其主線程上做了太多工作。我不明白爲什麼,因爲所有的努力工作都在服務中完成,而不是在onCreate()方法中完成。

我的MainActivity:

public class MainActivity extends Activity { 

    ImageButton startButton; 
    static Context context; 
    boolean isPlaying; 
    boolean playPause = false; 
    Intent streamService; 
    SharedPreferences prefs; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     context = this; 
     AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
     audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, 
       AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); 
     startButton = (ImageButton) findViewById(R.id.music_controls); 
     prefs = PreferenceManager.getDefaultSharedPreferences(context); 
     getPrefs(); 
     streamService = new Intent(MainActivity.this, StreamService.class); 

     startButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       if (playPause) { 
        stopService(streamService); 
        startButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_play)); 
        playPause = false; 
        Log.d("Radio: ", "Stoping......"); 
       }else { 
        // TODO Auto-generated method stub 

        Log.d("Radio: ", "Starting......"); 
        startService(streamService); 
        startButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_pause)); 
        playPause = true; 
       } 
      } 
     }); 

    } 

    public void onPrepared (MediaPlayer mp){ 

    } 

    public void getPrefs() { 
     isPlaying = prefs.getBoolean("isPlaying", false); 
     if (isPlaying) playPause = false; 
    } 

} 

我的服務:

public class StreamService extends Service { 
    private static final String TAG = "StreamService"; 
    MediaPlayer mp; 
    boolean isPlaying; 
    SharedPreferences prefs; 
    SharedPreferences.Editor editor; 
    Notification n; 
    NotificationManager notificationManager; 
    // Change this int to some number specifically for this app 
    int notifId = 5315; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.d(TAG, "onCreate"); 

     // Init the SharedPreferences and Editor 
     prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     editor = prefs.edit(); 

     // Set up the buffering notification 
     notificationManager = (NotificationManager) getApplicationContext() 
       .getSystemService(NOTIFICATION_SERVICE); 
     Context context = getApplicationContext(); 

     String notifTitle = context.getResources().getString(R.string.app_name); 
     String notifMessage = context.getResources().getString(R.string.buffering); 

     n = new Notification(); 
     n.icon = R.drawable.ic_launcher; 
     n.tickerText = "A carregar..."; 
     n.when = System.currentTimeMillis(); 

     Intent nIntent = new Intent(context, MainActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0); 

     n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent); 

     notificationManager.notify(notifId, n); 

     // It's very important that you put the IP/URL of your ShoutCast stream here 
     // Otherwise you'll get Webcom Radio 
     String url = "My stream url"; 
     mp = new MediaPlayer(); 
     mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 

     try { 
      mp.setDataSource(url); 
      mp.prepare(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      // TODO Auto-generated catch block 
      Log.e(TAG, "SecurityException"); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      Log.e(TAG, "IllegalStateException"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      Log.e(TAG, "IOException"); 
     } 
    } 

    public int onStartCommand(Intent intent,int flags, int startId) { 
     mp.start(); 
     // Set the isPlaying preference to true 
     editor.putBoolean("isPlaying", true); 
     editor.commit(); 

     Context context = getApplicationContext(); 
     String notifTitle = context.getResources().getString(R.string.app_name); 
     String notifMessage = context.getResources().getString(R.string.now_playing); 

     n.icon = R.drawable.ic_launcher; 
     n.tickerText = notifMessage; 
     n.flags = Notification.FLAG_NO_CLEAR; 
     n.when = System.currentTimeMillis(); 

     Intent nIntent = new Intent(context, MainActivity.class); 
     PendingIntent pIntent = PendingIntent.getActivity(context, 0, nIntent, 0); 

     n.setLatestEventInfo(context, notifTitle, notifMessage, pIntent); 
     // Change 5315 to some nother number 
     notificationManager.notify(notifId, n); 

     return START_STICKY; 
    } 


    @Override 
    public void onDestroy() { 
     Log.d(TAG, "onDestroy"); 
     mp.stop(); 
     mp.release(); 
     mp = null; 
     editor.putBoolean("isPlaying", false); 
     editor.commit(); 
     notificationManager.cancel(notifId); 
    } 

} 
+0

您是使用intentService還是普通服務? – SohailAziz

+0

我認爲這是一個正常的服務,「導入android.app.Service;」 –

+1

在主線程上使用網絡操作是一種不好的做法,可能最終會導致「應用程序關閉」。你必須在Asynctask上做在線流媒體才能讓事情工作變得更順暢。 –

回答