2017-07-11 5 views
1

J'utilise la bibliothèque de glide pour afficher des pochettes d'album dans mon application de lecteur de musique. Mais je ne sais pas pourquoi ça ne marche pas. Est-ce que quelqu'un peut m'aider ? Je suis un débutant dans le développement d'applications Android. Merci.
Voici le fichier java: PlayListActivity.javaGlisse pour l'art de l'album ne fonctionne pas

package com.example.dell_1.myapp3; 


import android.app.Activity; 
import android.database.Cursor; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
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 java.io.File; 
import java.io.IOException; 

import com.bumptech.glide.Glide; 

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() { 
     ImageView imageView = (ImageView) findViewById(R.id.coverart); 


     File music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); 
     // Tested with music from Windows 7's c:\Users\Public\Music\Sample Music 
     String filePath = new File(music, "I love you.mp3").getAbsolutePath(); 
     Glide 
       .with(this) 
       .load(new AudioCover(filePath)) 
       .placeholder(R.drawable.adele1) 
       .error(R.drawable.adele1) 
       .into(imageView) 
     ; 

    } 
} 

Voici le fichier xml: activity_play_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:divider="#242424" 
     android:dividerHeight="1dp" 
     android:listSelector="@drawable/list_selector" /> 

</LinearLayout> 

activity_android_building_music_player.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <LinearLayout 
     android:id="@+id/player_header_bg" 
     android:layout_width="fill_parent" 
     android:layout_height="60dip" 
     android:layout_alignParentTop="true" 
     android:background="@layout/bg_player_header" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp"> 

     <TextView 
      android:id="@+id/songTitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      android:paddingLeft="50dp" 
      android:text="The Good, The Bad And The Ugly" 
      android:textColor="#04b3d2" 
      android:textSize="16dp" 
      android:textStyle="bold" /> 

     <ImageButton 
      android:id="@+id/btnPlaylist" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:background="@null" 
      android:src="@drawable/btn_playlist" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/songThumbnail" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/player_header_bg" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:gravity="center" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp"> 

     <ImageView 
      android:id="@+id/coverart" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/player_footer_bg" 
     android:layout_width="fill_parent" 
     android:layout_height="100dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:background="@layout/bg_player_footer" 
     android:gravity="center"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_vertical" 
      android:orientation="horizontal" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:weightSum="1"> 
      <RelativeLayout 
       android:layout_width="300dp" 
       android:layout_height="match_parent"> 
       <ImageButton 
        android:id="@+id/btnPrevious" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="2.40" 
        android:layout_alignParentLeft="true" 
        android:paddingLeft="10dp" 
        android:background="@null" 
        android:src="@drawable/btn_previous" /> 

       <ImageButton 
        android:id="@+id/btnPlay1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:background="@null" 
        android:src="@drawable/btn_play" 
        android:onClick="buttonAction1"/> 


       <ImageButton 
        android:id="@+id/btnNext" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingLeft="250dp" 
        android:background="@null" 
        android:src="@drawable/btn_next" /> 
      </RelativeLayout> 
     </LinearLayout> 
    </LinearLayout> 

    <SeekBar 
     android:id="@+id/songProgressBar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/player_footer_bg" 
     android:layout_alignLeft="@+id/timerDisplay" 
     android:layout_alignStart="@+id/timerDisplay" 
     android:layout_marginBottom="10dp" 
     android:paddingLeft="6dp" 
     android:paddingRight="6dp" 
     android:progressDrawable="@drawable/seekbar_progress" 
     android:thumb="@drawable/download8" /> 

    <LinearLayout 
     android:id="@+id/timerDisplay" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/songProgressBar" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp"> 

     <TextView 
      android:id="@+id/songCurrentDurationLabel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="left" 
      android:textColor="#eeeeee" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/songTotalDurationLabel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:textColor="#04cbde" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/timerDisplay" 
     android:gravity="center"> 

     <ImageButton 
      android:id="@+id/btnRepeat" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dp" 
      android:background="@null" 
      android:src="@drawable/btn_repeat" /> 

     <ImageButton 
      android:id="@+id/btnShuffle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:background="@null" 
      android:src="@drawable/btn_shuffle" /> 
    </LinearLayout> 
</RelativeLayout> 

Audio Cover.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 
    } 
} 

Voici le manifeste:

<?xml version="1.0" encoding="utf-8"?> 
     <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.example.dell_1.myapp3"> 

       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
       <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" /> 
       <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
       <uses-permission 
android:name="android.permission.MEDIA_CONTENT_CONTROL" /> 
       <uses-permission android:name="android.permission.INTERNET" /> 

       <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:label="@string/app_name" 
        android:roundIcon="@mipmap/ic_launcher_round" 
        android:supportsRtl="true" 
        android:theme="@style/AppTheme"> 
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
        <uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" /> 
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
        <uses-permission 
        android:name="android.permission.MEDIA_CONTENT_CONTROL" /> 
        <uses-permission android:name="android.permission.INTERNET" /> 

        <activity 
         android:name=".apples" 
         android:label="@string/app_name" 
         android:theme="@style/AppTheme.NoActionBar"> 
         <intent-filter> 
          <action android:name="android.intent.action.MAIN" /> 

          <category android:name="android.intent.category.LAUNCHER" /> 
         </intent-filter> 
        </activity> 
        <activity 
         android:name=".Bacon1" 
         android:label="@string/title_activity_bacon1" /> 
        <activity 
         android:name=".SDCard" 
         android:label="@string/title_activity_sdcard" /> 

        <meta-data android:name="my.app.namespace.utils.GlideConfiguration" 
         android:value="GlideModule"/> 


        <activity 
         android:name=".PlayListActivity" 
         android:label="@string/title_activity_play_list"></activity> 
       </application> 

      </manifest> 

J'ai compris que je reçois l'erreur suivante sur button4 cliquant:

07-11 17:11:04.222 13978-13978/com.example.dell_1.myapp3 E/ExtMediaPlayer-JNI: env->IsInstanceOf fails 
07-11 17:11:04.222 13978-13978/com.example.dell_1.myapp3 E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 
07-11 17:11:04.222 13978-13978/com.example.dell_1.myapp3 E/ExtMediaPlayer-JNI: env->IsInstanceOf fails 
07-11 17:11:04.222 13978-13978/com.example.dell_1.myapp3 E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 
+0

vous devez localiser où il échoue. Donc, si elle échoue sur l'image de récupération, ou sur la définition de l'image, etc. –

+0

Je pensais que logcat m'aiderait à localiser, mais il ne montre rien de pertinent dans le logcat trop –

+0

vous pouvez utiliser le débogage, les points d'arrêt, et le step-over/step-in –

Répondre

0

Avez-vous fourni l'accès nécessaire au AndroidManifest.xml fichier?

+0

oui j'ai certainement. J'ai inclus le fichier manifeste maintenant. Vous pouvez jeter un oeil. Merci –

+0

Quelle est votre API Android? –

+0

API 14: Android 4.0 (IceCreamSandwich) –