0

please help to optimised my code for 1) I want to work my media meta data in background so it wont crash my app. 2) on slower internet connectivity like 3g it take more than 10 sec to start streaming and it stuck on that press event until it plays the music .. so sometime i got force close dialog which having wait or ok button to force close app so i dont want to happen that too.

I want to make my application which uses less cpu and less memory ...

So please take a look at my code and please give me example link because i am new to development.

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

1 Answers1

0

use

mp.prepareAsync();

instead of

mp.prepare(); // runs on main thread
pcu
  • 2,745
  • 4
  • 18
  • 22
  • ok what about metadata threading like i want to do it in background without crashing main activity so How can i achieve this –  Sep 02 '12 at 20:23
  • Anybody please suggests some solution –  Sep 03 '12 at 08:03
  • Use AsyncTask for this. doInBackground method runs on different thread and when finished then use postExecute to update UI. It runs on main/UI thread. – pcu Sep 03 '12 at 10:27
  • i am planing to implement service do you think its good idea and one more thing is that on 3g it takes time to buffer so some times android show dialog of force close so how can i solve this probmem –  Sep 03 '12 at 11:10
  • As I wrote above you need to do all processing where is possibility that it can take longer (IO, Networking, ...) on separate thread. For this purpose I usually use AsyncTask. In documentation there is nice example how to use it. – pcu Sep 03 '12 at 12:27