示例#1
0
  /**
   * Function to play a song
   *
   * @param songIndex - index of song
   */
  @SuppressWarnings("deprecation")
  public void playSong(int songIndex) {
    // Play song
    try {
      // Displaying Song title
      String songTitle = CurrentPL.getInstance().getSong(songIndex).getTitle();

      songTitleLabel.setText(songTitle);

      // Changing Button Image to pause image
      btnPlay.setImageResource(R.drawable.btn_pause);

      // set Progress bar values
      songProgressBar.setProgress(0);
      songProgressBar.setMax(100);
      finished = 0;

      // Ponemos la cover ->
      /*
      LongRunningGetIO lg = new LongRunningGetIO();
      lg.execute();
      while(finished != 1);
      finished = 0;*/

      Drawable d = (Drawable) CurrentPL.getInstance().getSong(songIndex).getDcover();
      Bitmap bd = ((BitmapDrawable) d).getBitmap();
      Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      double resize = 0.85;
      int y = (int) (size.y * resize);
      int x = (int) (size.x * resize);
      if (x > y) x = y;
      Bitmap bitmapOrig = Bitmap.createScaledBitmap(bd, x, x, false);

      songImage.setImageDrawable(new BitmapDrawable(bitmapOrig));

      mp.reset();
      // mp.setDataSource("http://galeon.com/miscosasvarias/homer.mp3");
      mp.setDataSource(CurrentPL.getInstance().getSong(songIndex).getUrl());
      mp.prepare();
      mp.start();

      // Updating progress bar
      updateProgressBar();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
示例#2
0
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

    ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

    // get all songs from sdcard
    CurrentPL playlist = CurrentPL.getInstance();

    // looping through playlist
    for (int i = 0; i < playlist.getNumSongs(); i++) {
      // creating new HashMap
      HashMap<String, String> song = new HashMap<String, String>();
      song.put("songTitle", playlist.getSong(i).getTitle());
      song.put("songPath", playlist.getSong(i).getUrl());

      // adding HashList to ArrayList
      songsListData.add(song);
    }

    // Adding menuItems to ListView
    ListAdapter adapter =
        new SimpleAdapter(
            this,
            songsListData,
            R.layout.playlist_item,
            new String[] {"songTitle"},
            new int[] {R.id.songTitle});

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();
    // listening to single listitem click
    lv.setOnItemClickListener(
        new OnItemClickListener() {

          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // getting listitem index
            int songIndex = position;

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), MusicPlayer.class);
            // Sending songIndex to PlayerActivity
            in.putExtra("songIndex", songIndex);
            setResult(100, in);
            // Closing PlayListView
            finish();
          }
        });
  }
示例#3
0
  /**
   * On Song Playing completed if repeat is ON play same song again if shuffle is ON play random
   * song
   */
  public void onCompletion(MediaPlayer arg0) {

    // check for repeat is ON or OFF
    if (isRepeat) {
      // repeat is on play same song again
      playSong(currentSongIndex);
    } else if (isShuffle) {
      // shuffle is on - play a random song
      Random rand = new Random();
      currentSongIndex = rand.nextInt((CurrentPL.getInstance().getNumSongs() - 1) - 0 + 1) + 0;
      playSong(currentSongIndex);
    } else {
      // no repeat or shuffle ON - play next song
      if (currentSongIndex < (CurrentPL.getInstance().getNumSongs() - 1)) {
        playSong(currentSongIndex + 1);
        currentSongIndex = currentSongIndex + 1;
      } else {
        // play first song
        playSong(0);
        currentSongIndex = 0;
      }
    }
  }