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