2012-09-02 2 views
0

에 대한 내 코드를 최적화하는 데 도움주세요. 1) 내 미디어 메타 데이터를 백그라운드에서 작업하여 내 앱을 크래시하지 않아야합니다. 2) 3g와 같은 느린 인터넷 연결에서 스트리밍을 시작하려면 10 초 이상 걸리고 음악을 재생할 때까지는 해당 프레스 이벤트에 머물러야합니다. 언젠가 강제로 닫기 대화 상자가 표시됩니다. 나는 그 일을 너무 원하지 않는다.백그라운드 작업 및 메타 데이터 작업을위한 코드를 최적화하는 몇 가지 팁

내가 적은 CPU와 메모리를 적게 사용하여 내 응용 프로그램을 만들고 싶어 ...

그래서 내 코드를 살펴 보시기 바랍니다 그리고 난 개발에 새로운이기 때문에 나에게 예를 들어 링크를 제공하시기 바랍니다.

 super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
     Button b1 = (Button) findViewById(R.id.button1); 
     Timer timer = new Timer(); 
     timer.schedule(new TimerTask() 
     { 
      public void run() 
      { 

    // this is to take metadata from stream so i want do this part in separate task or in background 
       try 
       { 
        IcyStreamMeta icy = new IcyStreamMeta(new URL("http://84.15.135.51:80/mp3/adw")); 
        data = icy.getArtist() + " - " + icy.getTitle(); 
        String temp = null; 


        runOnUiThread(new Runnable() 
        { 
         public void run() 
         { 
          tv.setText(data); 


         } 
        }); 


       } 
       catch (MalformedURLException e) 
       { 
        e.printStackTrace(); 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     },0,20000); 


     mynotification(); // i called notification function to show notification 


    // i have used Vitamio jar and plugin to play ogg stream 

    try { 
     mp = new MediaPlayer(this); 
    } catch (VitamioNotCompatibleException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (VitamioNotFoundException e1) { 

     System.out.println("Install vitamio on your device"); 
     Intent i = new Intent(this,mp3.class); 
     startActivity(i); 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 



    // i want to play media streaming on background and in different task or service so it wont stuck during play . 


    b1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      swap(); 




      try { 

        Boolean flag=mp.isPlaying(); 
        if(flag==true) 
        { 
         mp.reset(); 



        } 

       mp.setDataSource(link1);      mp.setWakeMode(getApplicationContext(),  
        PowerManager.SCREEN_DIM_WAKE_LOCK); 

       mp.prepare(); 
       mp.setOnPreparedListener(new OnPreparedListener() { 

        @Override 
        public void onPrepared(MediaPlayer arg0) { 
         // TODO Auto-generated method stub 
         progressDialog.dismiss(); 
         mp.start(); 

        } 
       }); 




      } catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } 
    });@Override 
public void onBackPressed() 
{ 
Log.d("CDA", "onBackPressed Called"); 
Intent setIntent = new Intent(Intent.ACTION_MAIN); 
setIntent.addCategory(Intent.CATEGORY_HOME); 
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(setIntent); 

return; 
} 
    // I am taking metadata data like title of track and show on notification.i want to show continuous notification until app exit 

@SuppressWarnings("deprecation") 
void mynotification() 
    { 
nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
Intent intent = new Intent(this,MainActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);  
PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0); 
String title = "radio"; 
Notification n = new Notification(R.drawable.noti, data, System.currentTimeMillis()); 
n.setLatestEventInfo(this , title, data, pi); 
n.defaults=Notification.DEFAULT_ALL; 
nm.notify(uniid,n); 
    } 

답변

0

사용

mp.prepareAsync(); 

대신

mp.prepare(); // runs on main thread 
+0

확인 어떤 메타 데이터 내가 제안하십시오이 –

+0

누구를 달성 할 수있는 방법 때문에 주요 활동을 충돌없이 백그라운드에서 그것을하고 싶은처럼 스레딩에 대한 some solution –

+0

AsyncTask를 사용하십시오. doInBackground 메서드는 다른 스레드에서 실행되며 완료되면 postExecute를 사용하여 UI를 업데이트합니다. 메인/UI 스레드에서 실행됩니다. – pcu