I am making a Music Player for android. Everything works perfect except list-view which have like 300+ songs in it ! The activity having that list-view takes like 5 seconds to load up and I want to reduce that time to user acceptable experience. Basically I am just using song title and artist as list-view format and no image, but experience is still slow. Googled a lot and just end up with lazy loading bitmaps and smooth scrolling docs but nothing about load up time of list-view activity. Here is the code I am using -
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.text1=(TextView) vi.findViewById(R.id.text1);
vi.setTag( holder );
}
else {
holder=(ViewHolder)vi.getTag();
}
if(data.size()<=0)
{
holder.text.setText("No Data");
}
else
{
tempValues=null;
tempValues = ( ListModel ) data.get( position );
holder.text.setText( tempValues.getCompanyName() );
if (tempValues.getUrl()== null){
holder.text1.setText("Unknown Artist");
}
else {
holder.text1.setText( tempValues.getUrl() );
}
vi.setOnClickListener(new OnItemClickListener( position ));
}
return vi;
}
Another thing is when I open list-view activity thrice or more(if lucky) it goes crash(might be memory reason but not sure). Here is the Log Cat
12-26 01:13:59.950: V/MediaPlayer-JNI(6349): getCurrentPosition: 68586 (msec)
12-26 01:13:59.960: V/MediaPlayer-JNI(6349): getCurrentPosition: 68586 (msec)
12-26 01:13:59.960: V/MediaPlayer-JNI(6349): isPlaying: 1
12-26 01:13:59.960: V/MediaPlayer-JNI(6349): getCurrentPosition: 68586 (msec)
12-26 01:14:00.020: D/AbsListView(6349): Get MotionRecognitionManager
12-26 01:14:01.392: D/AndroidRuntime(6349): Shutting down VM
12-26 01:14:01.392: W/dalvikvm(6349): threadid=1: thread exiting with uncaught exception (group=0x419aada0)
12-26 01:14:01.392: E/AndroidRuntime(6349): FATAL EXCEPTION: main
12-26 01:14:01.392: E/AndroidRuntime(6349): Process: com.garaya.musicplayer, PID: 6349
12-26 01:14:01.392: E/AndroidRuntime(6349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.garaya.musicplayer/com.garaya.musicplayer.MainActivity}: java.lang.RuntimeException: setDataSource failed: status = 0x80000000
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.app.ActivityThread.access$900(ActivityThread.java:174)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.os.Handler.dispatchMessage(Handler.java:102)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.os.Looper.loop(Looper.java:146)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.app.ActivityThread.main(ActivityThread.java:5593)
12-26 01:14:01.392: E/AndroidRuntime(6349): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 01:14:01.392: E/AndroidRuntime(6349): at java.lang.reflect.Method.invoke(Method.java:515)
12-26 01:14:01.392: E/AndroidRuntime(6349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-26 01:14:01.392: E/AndroidRuntime(6349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-26 01:14:01.392: E/AndroidRuntime(6349): at dalvik.system.NativeStart.main(Native Method)
12-26 01:14:01.392: E/AndroidRuntime(6349): Caused by: java.lang.RuntimeException: setDataSource failed: status = 0x80000000
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.media.MediaMetadataRetriever.setDataSource(Native Method)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:70)
12-26 01:14:01.392: E/AndroidRuntime(6349): at com.garaya.musicplayer.MainActivity.setListData(MainActivity.java:241)
12-26 01:14:01.392: E/AndroidRuntime(6349): at com.garaya.musicplayer.MainActivity.onCreate(MainActivity.java:186)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.app.Activity.performCreate(Activity.java:5458)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
12-26 01:14:01.392: E/AndroidRuntime(6349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
12-26 01:14:01.392: E/AndroidRuntime(6349): ... 11 more
Here is the Main Activity setListData()
-
public void setListData()
{
for(int i=0; i < files.length; i++)
{
File file = files[i];
final ListModel sched = new ListModel();
MediaMetadataRetriever metaRetriver;
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(file.getPath());
String filename = file.getName().replace(".mp3", "");
filename = filename.replace(".MP3", "");
String TitleFile = metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
String finalName;
if (TitleFile == null || TitleFile == "") {
finalName = filename;
}
else {
finalName = TitleFile;
}
sched.setCompanyName(finalName);
sched.setUrl(metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
CustomListViewValuesArr.add( sched );
}
}
Google is overflooded with Images loading and infinite list-view loading but no well result on load up time of listview. I guess Async might be helpful but I am pretty just beginner in android and its my first android app to learn how to make apps. So guyz, please help me out! I am very clueless. (If any other code is required please let me know!)
Edited setListData() without MetaDataRetriever loads activity in 2 seconds
public void setListData()
{
for(int i=0; i < files.length; i++)
{
File file = files[i];
final ListModel sched = new ListModel();
String filename = file.getName().replace(".mp3", "");
filename = filename.replace(".MP3", "");
sched.setCompanyName(filename);
sched.setUrl("Artist");
CustomListViewValuesArr.add( sched );
}
}
I have solved the issue myself. Here is what I did -
public void setListData()
{
for(int i=0; i < files.length; i++)
{
File file = files[i];
final ListModel sched = new ListModel();
String filename = file.getName().replace(".mp3", "");
filename = filename.replace(".MP3", "");
sched.setCompanyName(filename);
sched.setUrl("Artist"); // set default value and we will get actual value via MetaDataRetriever in getView()
CustomListViewValuesArr.add( sched );
}
}
I have just remove the MetaDataRetriever
from setListData()
and add it in getView()
hence lowers the burden on activity. To understand how it works just think setListData
do work for every row and there are 300+ rows, so lots of work to do, but on other hand getView
deals with rows which are currently on screen. So instead doing work on tones of different rows, now we just need to take care about 7-10 rows which are being displayed! So basic thing is to lower the burden on acticity as much as you ccan while seeting list-data.
Sorry for bad English ! but I hope solution works for others !