Owen2014
Owen2014

Reputation: 43

Filtering songs from other music files - Android

So in an app that I am working on, I am trying to incorporate a listview which displays all of the songs on the user's phone. I currently have it set up to show all of the music files (shown in the code below), but I need a way to filter out actual songs from just random media files (i.e. a recording or other audio files used in apps like facebook or google+). I have searched and tried multiple things but none of the methods have proven to be successful. As you can see in my code I was trying something along the line of getting a song's duration and making sure it was at least around one minute long. Any and all help would be much appreciated!

Thanks, Owen

P.S. Just ask me if you need to see more of my code, but I think this should be enough.

  private void init_phone_music_grid() {
    System.gc();
    String[] proj = { MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Video.Media.ARTIST };

    musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            proj, null, null, null);
    count = musiccursor.getCount();
    musiclist = (ListView) findViewById(R.id.lvTrackList);
    musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

    musiclist.setOnItemClickListener(musicgridlistener);
    mMediaPlayer = new MediaPlayer();
}

private OnItemClickListener musicgridlistener = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position,
            long id) {
        System.gc();
        music_column_index = musiccursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        musiccursor.moveToPosition(position);
        String filename = musiccursor.getString(music_column_index);

        try {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.reset();
            }
            mMediaPlayer.setDataSource(filename);
            mMediaPlayer.prepare();
            mMediaPlayer.start();

            if(mMediaPlayer.isPlaying())
                chrono.start();

        } catch (Exception e) {

        }
    }
};

public class MusicAdapter extends BaseAdapter {
    private Context mContext;

    public MusicAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        System.gc();
        TextView tv = new TextView(mContext.getApplicationContext());
        String id = null;
        if (convertView == null) {
            long size = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);

            if(size > 60000) {
            music_column_index = musiccursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);


            musiccursor.moveToPosition(position);
            id = musiccursor.getString(music_column_index);


            music_column_index = musiccursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);

            musiccursor.moveToPosition(position);
            if(!(musiccursor.getString(music_column_index).equalsIgnoreCase("<unknown>")))
                id += " - " + musiccursor.getString(music_column_index);

            tv.setText(id);
            }
        } else
            tv = (TextView) convertView;

        return tv;

    }
}

Upvotes: 1

Views: 1062

Answers (1)

nandeesh
nandeesh

Reputation: 24820

Use below query to get all media with duration more than 1 minute

 musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            proj, MediaStore.Audio.Media.DURATION + ">= 60000", null, null);

Upvotes: 4

Related Questions