Android Open Source - audioplayer Cover Album Getter






From Project

Back to project page audioplayer.

License

The source code is released under:

GNU General Public License

If you think the Android project audioplayer 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

package fr.julien_dumortier.simplemusicplayer.coveralbumeditor;
//  ww w  .ja v a 2 s. co  m
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import fr.julien_dumortier.simplemusicplayer.item.Album;
import fr.julien_dumortier.simplemusicplayer.item.Item;
import fr.julien_dumortier.simplemusicplayer.item.Track;

public class CoverAlbumGetter {
  
  public interface IOnLinksGettedListener {
    public void onLinkGetted(List<String> links);
    public void error();
  }

  public void startGetCoversLinksByItem(final Item item, final IOnLinksGettedListener listener) {
    new Thread("get cover from google thread") { 
      public void run() {
        try {
          String formatedRequest = formatRequestByItem(item);
          JSONObject jso = getJsonResponse(formatedRequest, 0);
          List<String> links = getAllCoverLinkFromJson(jso);
          jso = getJsonResponse(formatedRequest, links.size());
          links.addAll(getAllCoverLinkFromJson(jso));
          listener.onLinkGetted(links);
        } catch(Exception e) {
          try {
            String formatedRequest = formatRequestByItem(item);
            JSONObject jso = getJsonResponse(formatedRequest, 0);
            List<String> links = getAllCoverLinkFromJson(jso);
            jso = getJsonResponse(formatedRequest, links.size());
            links.addAll(getAllCoverLinkFromJson(jso));
            listener.onLinkGetted(links);
          } catch(Exception ee) {
            try {
              String formatedRequest = formatRequestByItem(item);
              JSONObject jso = getJsonResponse(formatedRequest, 0);
              List<String> links = getAllCoverLinkFromJson(jso);
              jso = getJsonResponse(formatedRequest, links.size());
              links.addAll(getAllCoverLinkFromJson(jso));
              listener.onLinkGetted(links);
            } catch(Exception eee) {
              eee.printStackTrace();
              listener.error();
            }
          }
        }
      }
    }.start();
  }
  
  private static String formatRequestByItem(Item item) throws Exception  {
    String formatedRequest = "";
    if(item instanceof Album) {
      String albumName = ((Album)item).getAlbumName();
      String artistName = ((Album)item).getArtistName();
      formatedRequest = artistName+" "+albumName;
      formatedRequest = java.net.URLEncoder.encode(formatedRequest,"UTF-8");
    }
    else if(item instanceof Track) {
      String albumName = ((Track)item).getAlbumName();
      String artistName = ((Track)item).getArtistName();
      String trackName = ((Track)item).getTrackName();
      formatedRequest = artistName+" "+albumName+" "+trackName;
    } else
      throw new Exception("type de l'item non pris en charge");
    return formatedRequest.replaceAll(" ", "%20");
  }
  
  private static JSONObject getJsonResponse(String formatedRequest, int offset) throws Exception {
    if(offset>0)
      formatedRequest+="&start="+Integer.toString(offset);
    URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
                "v=1.0&q=cover%20cd%20"+formatedRequest+"&tbs=isz:m");
    URLConnection connection = url.openConnection();
    connection.addRequestProperty("Referer", "mywebsite");
    String line;
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    while((line = reader.readLine()) != null) {
      builder.append(line);
    }
    
    return new JSONObject(builder.toString());
  }
  
  private static List<String> getAllCoverLinkFromJson(JSONObject jso) throws Exception {
    List<String> links = new ArrayList<String>();
    JSONArray jsa = jso.getJSONObject("responseData").getJSONArray("results");
    for(int i=0; i<jsa.length(); i++) {
      links.add(jsa.getJSONObject(i).getString("url"));
    }
    return links;
  }
}




Java Source Code List

fr.julien_dumortier.simplemusicplayer.DevInfo.java
fr.julien_dumortier.simplemusicplayer.DrawerListAdapter.java
fr.julien_dumortier.simplemusicplayer.IMusicMediaControllerGetter.java
fr.julien_dumortier.simplemusicplayer.MainActivity.java
fr.julien_dumortier.simplemusicplayer.browsemanager.BrowseCover.java
fr.julien_dumortier.simplemusicplayer.browsemanager.BrowseManager.java
fr.julien_dumortier.simplemusicplayer.coveralbumeditor.CoverAlbumEditor.java
fr.julien_dumortier.simplemusicplayer.coveralbumeditor.CoverAlbumGetter.java
fr.julien_dumortier.simplemusicplayer.coveralbumeditor.PromptCovers.java
fr.julien_dumortier.simplemusicplayer.equalizer.EqualizerActivity.java
fr.julien_dumortier.simplemusicplayer.equalizer.EqualizerConfigurationDataBase.java
fr.julien_dumortier.simplemusicplayer.equalizer.VerticalSeekBar.java
fr.julien_dumortier.simplemusicplayer.equalizer.preset.Preset.java
fr.julien_dumortier.simplemusicplayer.item.Album.java
fr.julien_dumortier.simplemusicplayer.item.Artist.java
fr.julien_dumortier.simplemusicplayer.item.Genre.java
fr.julien_dumortier.simplemusicplayer.item.Item.java
fr.julien_dumortier.simplemusicplayer.item.Playlist.java
fr.julien_dumortier.simplemusicplayer.item.Track.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.AlbumAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.ArtistAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.CurrentPlayAnimationManager.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.GenreAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.ItemAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.ItemClicManager.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.OrganisableTrackAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.PlayListAdapter.java
fr.julien_dumortier.simplemusicplayer.item.itemadapter.TrackAdapter.java
fr.julien_dumortier.simplemusicplayer.notifynews.NotifyNews.java
fr.julien_dumortier.simplemusicplayer.playerview.FullScreenPlayerViewController.java
fr.julien_dumortier.simplemusicplayer.playerview.IPlayerViewController.java
fr.julien_dumortier.simplemusicplayer.playerview.MiniPlayerViewController.java
fr.julien_dumortier.simplemusicplayer.playerview.TimeFormater.java
fr.julien_dumortier.simplemusicplayer.playlistview.MultiSelectManager.java
fr.julien_dumortier.simplemusicplayer.playlistview.PlayListManagerActivity.java
fr.julien_dumortier.simplemusicplayer.playlistview.SaveManagerView.java
fr.julien_dumortier.simplemusicplayer.playlistview.SavedPlaylistManager.java
fr.julien_dumortier.simplemusicplayer.service.BackgroundBinder.java
fr.julien_dumortier.simplemusicplayer.service.MusicPlayerService.java
fr.julien_dumortier.simplemusicplayer.service.bluetoohactionmanager.BluetoothReceiver.java
fr.julien_dumortier.simplemusicplayer.service.notificationplayer.NotificationManagerService.java
fr.julien_dumortier.simplemusicplayer.service.notificationplayer.NotificationPlayer.java
fr.julien_dumortier.simplemusicplayer.service.phonestatemanager.PhoneCallStateManager.java
fr.julien_dumortier.simplemusicplayer.service.playlist.EndOfPlayListException.java
fr.julien_dumortier.simplemusicplayer.service.playlist.IMediaPlayerController.java
fr.julien_dumortier.simplemusicplayer.service.playlist.MusicMediaPlayer.java
fr.julien_dumortier.simplemusicplayer.service.playlist.PlayListManager.java
fr.julien_dumortier.simplemusicplayer.thememanager.ColorPreferences.java
fr.julien_dumortier.simplemusicplayer.thememanager.DisplayManager2.java
fr.julien_dumortier.simplemusicplayer.thememanager.ManageDisplayView.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptActionByType.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptActionPlaylist.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptConfirmAddInNew.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptConfirmDeletePlaylist.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptConfirmExit.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptConfirmReplacePlaylist.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptDefineCoverAlbum.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptGetTextName.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptMoveTrack.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptPlaylistName.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptShowText.java
fr.julien_dumortier.simplemusicplayer.uiprompt.PromptSuppressConfirm.java
fr.julien_dumortier.simplemusicplayer.widget.AudioPlayerApplication.java
fr.julien_dumortier.simplemusicplayer.widget.AudioPlayerWidgetProvider.java
fr.julien_dumortier.simplemusicplayer.widget.AudioPlayerWidgetViewController.java
fr.julien_dumortier.simplemusicplayer.widget.WidgetView.java