如何显示最近创建的录制音频文件

时间:2016-08-09 10:49:41

标签: android android-studio

我正在制作录音应用程序。 我想显示从我的应用程序完成的最新录音,它显示以前完成的录音,但它不显示最近录制的音频文件。 我正在使用回收站视图来显示来自myfolder的文件。 请帮忙,至少让我知道网上要找的东西。 谢谢

public class MusicLoader extends AsyncTaskLoader<List<Music>> {

    List<Music> mCache;
    MusicObserver mMusicObserver;
    private Context mContext;
    public MusicLoader(Context context) {
        super(context);
    }

    @Override
    public List<Music> loadInBackground() {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();

        String selection = String.format("%s !=%d AND %s LIKE '%s/MyFolder/%%'", MediaStore.Audio.Media.IS_MUSIC, 0, MediaStore.Audio.Media.DATA, path);
        final ContentResolver contentResolver = getContext().getContentResolver();
        String [] projections = {MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM_ID};


        String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
        Cursor cr = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projections, selection, null, sortOrder);

        List<Music> items = new ArrayList<>();

        if(cr != null && cr.moveToFirst()) {
            // Cache the column indexes so we don't have to look them up for every iteration of the do-while loop.

            int idIndex = cr.getColumnIndex(MediaStore.Audio.Media._ID);
            int titleIndex = cr.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int artistIndex = cr.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int albumId =  cr.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
            do {
                if(isLoadInBackgroundCanceled()){
                    return items;
                }
                // Music object to hold the music data.
                Music item = new Music();
                // Retrieve the respective music data from the cursor using the column index.
                item.setId(cr.getLong(idIndex));

                item.setTitle(cr.getString(titleIndex));
                item.setArtist(cr.getString(artistIndex));
                item.setAlbumId(cr.getLong(albumId));

                // Once we've loaded the Music object, store it inside of the arraylist.
                items.add(item);
            }
            while(cr.moveToNext());
            cr.close();

        }
        return items;
    }




    @Override
    protected void onStopLoading() {
       cancelLoad();
    }

    @Override
    protected void onStartLoading() {


        if(mMusicObserver == null) {
            mMusicObserver = new MusicObserver(this, new Handler());
            getContext().getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,true, mMusicObserver);
        }

        if(takeContentChanged() || mCache == null) {
            forceLoad();
        }
    }

    @Override
    protected void onReset() {
        // Close any cursors, web-sockets or database objects
        if(mMusicObserver != null) {
            getContext().getContentResolver().unregisterContentObserver(mMusicObserver);
            mMusicObserver = null;
        }
    }

    /**
     * Simple observer that notifies the loader when it has detected a change.
     */
    private static class MusicObserver extends ContentObserver {

        private android.support.v4.content.Loader mLoader;

        public MusicObserver(android.support.v4.content.Loader loader, Handler handler) {
            super(handler);
            mLoader = loader;
        }

        @Override
        public void onChange(boolean selfChange) {
            // A change has been detectec notify the Loader.
            mLoader.onContentChanged();
        }
    }

}

0 个答案:

没有答案