Android 如何显示最近创建的录制音频文件
我正在制作一个录音应用程序。 我想显示从我的应用程序中录制的最新录音,它显示以前录制的录音,但不显示最近录制的音频文件。 我正在使用recycler视图显示myfolder中的文件。 请帮忙,至少让我知道在网上找什么东西。 多谢各位Android 如何显示最近创建的录制音频文件,android,android-studio,Android,Android Studio,我正在制作一个录音应用程序。 我想显示从我的应用程序中录制的最新录音,它显示以前录制的录音,但不显示最近录制的音频文件。 我正在使用recycler视图显示myfolder中的文件。 请帮忙,至少让我知道在网上找什么东西。 多谢各位 public class MusicLoader extends AsyncTaskLoader<List<Music>> { List<Music> mCache; MusicObserver mMusicOb
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();
}
}
}
公共类MusicLoader扩展了AsyncTaskLoader{
罗列麦卡什;
音乐服务器mMusicObserver;
私有上下文;
公共音乐阅读器(上下文){
超级(上下文);
}
@凌驾
公共列表加载背景(){
字符串路径=Environment.getExternalStorageDirectory().getAbsolutePath();
字符串选择=字符串.格式(“%s!=%d和%s,如“%s/MyFolder/%%”,MediaStore.Audio.Media.IS_MUSIC,0,MediaStore.Audio.Media.DATA,路径);
final ContentResolver ContentResolver=getContext().getContentResolver();
字符串[]投影={MediaStore.Audio.Media.\u ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.ALBUM\u ID};
字符串排序器=MediaStore.Audio.Media.TITLE+“ASC”;
游标cr=contentResolver.query(MediaStore.Audio.Media.EXTERNAL\u CONTENT\u URI、投影、选择、null、排序器);
列表项=新建ArrayList();
如果(cr!=null&&cr.moveToFirst()){
//缓存列索引,这样我们就不必在do while循环的每次迭代中都查找它们。
int-idIndex=cr.getColumnIndex(MediaStore.Audio.Media.\u-ID);
int titleIndex=cr.getColumnIndex(MediaStore.Audio.Media.TITLE);
int artistinex=cr.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int albumId=cr.getColumnIndex(MediaStore.Audio.Media.ALBUM\u ID);
做{
如果(IsLoadInBackgroundCancelled()){
退货项目;
}
//用于保存音乐数据的音乐对象。
音乐项=新音乐();
//使用列索引从光标检索相应的音乐数据。
setId(cr.getLong(ididex));
item.setTitle(cr.getString(titleIndex));
item.setArtist(cr.getString(artistinex));
item.setAlbumId(cr.getLong(albumId));
//加载音乐对象后,将其存储在arraylist中。
项目。添加(项目);
}
while(cr.moveToNext());
cr.close();
}
退货项目;
}
@凌驾
受保护的无效onStopLoading(){
取消加载();
}
@凌驾
开始加载时受保护的void(){
如果(mMusicObserver==null){
mMusicObserver=new MusicObserver(这个,new Handler());
getContext().getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL\u CONTENT\u URI,true,mMusicObserver);
}
if(takeContentChanged()| | mCache==null){
力载荷();
}
}
@凌驾
受保护的void onReset(){
//关闭所有游标、web套接字或数据库对象
如果(mMusicObserver!=null){
getContext().getContentResolver().unregisterContentObserver(mMusicObserver);
mMusicObserver=null;
}
}
/**
*当加载器检测到更改时通知加载器的简单观察者。
*/
私有静态类MusicObserver扩展ContentObserver{
私有android.support.v4.content.Loader-mLoader;
公共音乐服务器(android.support.v4.content.Loader、Handler){
超级(处理器);
装载机=装载机;
}
@凌驾
公共void onChange(布尔自更改){
//已检测到更改并通知加载程序。
mLoader.onContentChanged();
}
}
}