我正在使用 glide 来显示专辑封面,但不知何故我无法显示它。我想添加侦听器来滑动以定位错误,但它不起作用。它显示错误说:
com.bumptech.glide.DrawableRequestBuilder
中的listener(com.bumptech.glide.request.RequestListener)
不能应用于(anonymous com.bumptech.glide.request.RequestListener)
播放列表活动.java:
package com.example.dell_1.myapp3;
import android.app.Activity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
public class PlayListActivity extends Activity {
private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_list);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.list);
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA}, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
setContentView(R.layout.activity_android_building_music_player);
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
//mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
asd();
}
public void asd(){
File music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
// Tested with music from Windows 7's c:UsersPublicMusicSample Music
String mAudioPath = new File(music, "Maid with the Flaxen Hair.mp3").getAbsolutePath();
ImageView imageView = (ImageView) findViewById(R.id.coverart);
Glide
.with(this)
.load(new AudioCover(mAudioPath))
.placeholder(R.drawable.adele1)
.error(R.drawable.adele1)
.listener(new RequestListener<Uri, GlideDrawable>() {
@Override public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// easy
return false;
// impossible?
}
})
.into(imageView)
;
}
}
音频封面.java :
package com.example.dell_1.myapp3;
import android.content.Context;
import android.media.MediaMetadataRetriever;
import com.bumptech.glide.*;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.stream.StreamModelLoader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class AudioCover {
final String path;
public AudioCover(String path) {
this.path = path;
}
}
class AudioCoverLoader implements StreamModelLoader<AudioCover> {
@Override public DataFetcher<InputStream> getResourceFetcher(AudioCover model, int width, int height) {
return new AudioCoverFetcher(model);
}
static class Factory implements ModelLoaderFactory<AudioCover, InputStream> {
@Override public ModelLoader<AudioCover, InputStream> build(Context context, GenericLoaderFactory factories) {
return new AudioCoverLoader();
}
@Override public void teardown() {
}
}
}
class AudioCoverFetcher implements DataFetcher<InputStream> {
private final AudioCover model;
private FileInputStream stream;
public AudioCoverFetcher(AudioCover model) {
this.model = model;
}
@Override public String getId() {
return model.path;
}
@Override public InputStream loadData(Priority priority) throws Exception {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(model.path);
byte[] picture = retriever.getEmbeddedPicture();
if (picture != null) {
return new ByteArrayInputStream(picture);
} else {
return fallback(model.path);
}
} finally {
retriever.release();
}
}
private static final String[] FALLBACKS = {"cover.jpg", "album.jpg", "folder.jpg"};
private InputStream fallback(String path) throws FileNotFoundException {
File parent = new File(path).getParentFile();
for (String fallback : FALLBACKS) {
// TODO make it smarter by enumerating folder contents and filtering for files
// example algorithm for that: http://askubuntu.com/questions/123612/how-do-i-set-album-artwork
File cover = new File(parent, fallback);
if (cover.exists()) {
return stream = new FileInputStream(cover);
}
}
return null;
}
@Override public void cleanup() {
// already cleaned up in loadData and ByteArrayInputStream will be GC'd
if (stream != null) {
try {
stream.close();
} catch (IOException ignore) {
// can't do much about it
}
}
}
@Override public void cancel() {
// cannot cancel
}
}
RequestListener<T, R>
期望 T 是您在load()
中提供的模型,在您的情况下T = ? super AudioCover
(文档(:
// ...
.listener(new RequestListener<AudioCover, GlideDrawable>() {
@Override
public boolean onException(Exception e, AudioCover model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, AudioCover model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
// ...
请注意,由于版本 4.0.0-RC0RequestListener<T, R>
已被弃用以支持RequestListener<R>
,请在新的 javadoc 中找到更多信息。
添加RequestListner
Glide.with(this)
.load(url)
.listener(new RequestListener < Uri, GlideDrawable > () {
@Override public boolean onException(Exception e, Uri model, Target < GlideDrawable > target, boolean isFirstResource) {
return false;
}
@Override public boolean onResourceReady(GlideDrawable resource, Uri model, Target < GlideDrawable > target, boolean isFromMemoryCache, boolean isFirstResource) {
// easy
return false;
// impossible?
}
})
.into(imageView);