Android Open Source - Streamplay Music Player






From Project

Back to project page Streamplay.

License

The source code is released under:

Copyright 2014 A-z-f

If you think the Android project Streamplay 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 com.example.streamplay.libs;
/*ww w .  j  av a  2  s  . co m*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import android.media.AudioManager;
import android.media.MediaPlayer;

public class MusicPlayer {

  public MediaPlayer mediaPlayer = null;
  private ArrayList<HashMap<String, String>> _tracklist = null;
  private Integer _trackPosition = 0;
  private String _node = "";
  private String _baseLink = "";
  private Boolean _play = false;

  public MusicPlayer(ArrayList<HashMap<String, String>> tracklist,
      String baseLink, String node) {
    _tracklist = tracklist;
    _node = node;
    _baseLink = baseLink;
  }

  public boolean Play(Integer trackPosition) {
    if (!_play) {
      _play = true;
      mediaPlayer = new MediaPlayer();
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      _trackPosition = trackPosition;
      String track = _tracklist.get(_trackPosition).get(_node);

      try {
        String url = _baseLink + track;
        mediaPlayer.setDataSource(url);
      } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      try {
        mediaPlayer.prepare();
      } catch (IllegalStateException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } // might take long! (for buffering, etc)

      mediaPlayer.start();
    }
    return true;
  }

  public boolean Pause() {
    if (_play) {
      try {
        mediaPlayer.pause();
        _play = false;
        return true;
      } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
      } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
      } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
      }
    } else {
      return false;
    }
  }

  public boolean Next() {
    if (_play) {
      mediaPlayer.stop();
      _play = false;
    }

    if (_trackPosition < _tracklist.size())
      _trackPosition++;

    this.Play(_trackPosition);

    return true;
  }

  public boolean Prev() {
    if (_play) {
      mediaPlayer.stop();
      _play = false;
    }
    if (_trackPosition > 0)
      _trackPosition--;

    this.Play(_trackPosition);
    return true;
  }
}




Java Source Code List

com.example.streamplay.activities.MainActivity.java
com.example.streamplay.activities.PlayerActivity.java
com.example.streamplay.activities.SplashActivity.java
com.example.streamplay.app.StreamplayApp.java
com.example.streamplay.libs.JSONParser.java
com.example.streamplay.libs.MusicPlayer.java