Android Open Source - app-gkplayer-android Music Retriever






From Project

Back to project page app-gkplayer-android.

License

The source code is released under:

Apache License

If you think the Android project app-gkplayer-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*   
 * Copyright (C) 2011 The Android Open Source Project
 */*from  w w w .  ja  va 2s . co  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.roamtouch.gesturekit.gkplayer.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * Retrieves and organizes media to play. Before being used, you must call {@link #prepare()},
 * which will retrieve all of the music on the user's device (by performing a query on a content
 * resolver). After that, it's ready to retrieve a random song, with its title and URI, upon
 * request.
 */
public class MusicRetriever {
    final String TAG = "MusicRetriever";

    ContentResolver mContentResolver;

    // the items (songs) we have queried
    List<Song> mItems = new ArrayList<Song>();

    Random mRandom = new Random();

    public MusicRetriever(ContentResolver cr) {
        mContentResolver = cr;
    }

    /**
     * Loads music data. This method may take long, so be sure to call it asynchronously without
     * blocking the main thread.
     */
    public void prepare() {
        Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Log.i(TAG, "Querying media...");
        Log.i(TAG, "URI: " + uri.toString());

        // Perform a query on the content resolver. The URI we're passing specifies that we
        // want to query for all audio media on external storage (e.g. SD card)
        Cursor cur = mContentResolver.query(uri, null, null, null, null);
        //Cursor cur = mContentResolver.query(uri, null, android.provider.MediaStore.Audio.Artists.ARTIST+"=?", new String[]{"U2"}, null);
        Log.i(TAG, "Query finished. " + (cur == null ? "Returned NULL." : "Returned a cursor."));

        if (cur == null) {
            // Query failed...
            Log.e(TAG, "Failed to retrieve music: cursor is null :-(");
            return;
        }
        if (!cur.moveToFirst()) {
            // Nothing to query. There is no music on the device. How boring.
            Log.e(TAG, "Failed to move cursor to first row (no query results).");
            return;
        }

        Log.i(TAG, "Listing...");

        // retrieve the indices of the columns where the ID and title of the song are
        int titleColumn = cur.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = cur.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = cur.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
        int albumColumn = cur.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM);
        int coverColumn = cur.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM_ID);
        int songOrdercolumn = cur.getColumnIndex(android.provider.MediaStore.Audio.Media.TRACK);

        Log.i(TAG, "Title column index: " + String.valueOf(titleColumn));
        Log.i(TAG, "ID column index: " + String.valueOf(titleColumn));

        // add each song to mItems
        do {
            //Log.i(TAG, "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn));
            mItems.add(new Song(cur.getLong(idColumn), cur.getString(titleColumn), cur.getString(artistColumn), cur.getInt(coverColumn), cur.getString(albumColumn), cur.getString(songOrdercolumn)));
        } while (cur.moveToNext());

        Log.i(TAG, "Done querying media. MusicRetriever is ready.");
    }

    public ContentResolver getContentResolver() {
        return mContentResolver;
    }

    /** Returns a random Item. If there are no items available, returns null. */
    public Song getRandomItem() {
        if (mItems.size() <= 0) return null;
        return mItems.get(mRandom.nextInt(mItems.size()));
    }
}




Java Source Code List

com.roamtouch.gesturekit.gkplayer.GKPlayer.java
com.roamtouch.gesturekit.gkplayer.PlayerLayout.java
com.roamtouch.gesturekit.gkplayer.graphics.Blur.java
com.roamtouch.gesturekit.gkplayer.graphics.ProgressView.java
com.roamtouch.gesturekit.gkplayer.graphics.Utils.java
com.roamtouch.gesturekit.gkplayer.graphics.VerticalTextView.java
com.roamtouch.gesturekit.gkplayer.graphics.VolumeControl.java
com.roamtouch.gesturekit.gkplayer.service.AudioFocusHelper.java
com.roamtouch.gesturekit.gkplayer.service.AudioFocus.java
com.roamtouch.gesturekit.gkplayer.service.ContentListener.java
com.roamtouch.gesturekit.gkplayer.service.MusicBinder.java
com.roamtouch.gesturekit.gkplayer.service.MusicFocusable.java
com.roamtouch.gesturekit.gkplayer.service.MusicIntentReceiver.java
com.roamtouch.gesturekit.gkplayer.service.MusicRetriever.java
com.roamtouch.gesturekit.gkplayer.service.MusicService.java
com.roamtouch.gesturekit.gkplayer.service.PrepareMusicRetrieverTask.java
com.roamtouch.gesturekit.gkplayer.service.Song.java
com.roamtouch.gesturekit.gkplayer.service.State.java