Android Open Source - wristband-android Wristband Player Service






From Project

Back to project page wristband-android.

License

The source code is released under:

The Artistic License 2.0 Copyright (c) 2014 Allan Pichardo Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed...

If you think the Android project wristband-android 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.nimo.wristband.service;
/*from  w w w .  j av  a  2  s .com*/
import java.io.IOException;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.nimo.wristband.MainActivity;
import com.nimo.wristband.R;
import com.nimo.wristband.net.BitmapLruCache;
import com.nimo.wristband.net.Show;

public class WristbandPlayerService extends Service implements OnPreparedListener, 
OnErrorListener,OnAudioFocusChangeListener,OnBufferingUpdateListener,
OnInfoListener,OnCompletionListener,OnSeekCompleteListener{
  
  public static final String ARG_SHOW = "show";
  
  private final IBinder binder = new PlayerBinder();
  
  private MediaPlayer mediaPlayer = null;
  private PlaybackCallbacks listener;
  
  public static final long POLL_INTERVAL = 1000 * 1;
  private Handler positionHandler = new Handler();
  private Runnable positionRunnable = new Runnable() {
    
    @Override
    public void run() {
      if(mediaPlayer != null){
        try{
        if(mediaPlayer.isPlaying()){
          if(listener != null){
            listener.onPositionChange(mediaPlayer.getCurrentPosition(), mediaPlayer.getDuration());
          }
        }
        }catch(IllegalStateException e){
          
        }
      }
      positionHandler.postDelayed(this, POLL_INTERVAL);
    }
  };

  @Override
  public IBinder onBind(Intent intent) {
    init();
    return binder;
  }
  
  public class PlayerBinder extends Binder{
    public WristbandPlayerService getService(){
      return WristbandPlayerService.this;
    }
  }
  
  public void setPlaybackCallbacks(PlaybackCallbacks listener){
    this.listener = listener;
  }
  
  @Override
  public void onDestroy() {
    if(mediaPlayer != null){
      mediaPlayer.release();
      mediaPlayer = null;
    }
  }
  
  @Override
  public void onCreate() {
    super.onCreate();
  }
  
  private void init(){
    
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnInfoListener(this);
    mediaPlayer.setOnSeekCompleteListener(this);
  }
  
  private void showNotification(Show show){
    // assign the song name to songName
    
    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
    intent.putExtra(ARG_SHOW, show.toString());
    
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
    //final Notification notification = new Notification();
    final Notification.Builder builder = new Notification.Builder(this);
    builder.setTicker(show.getBandName()+"-"+show.getTrackTitle());
    //notification.tickerText = show.getBandName()+"-"+show.getTrackTitle();
    builder.setOngoing(true);
    //notification.flags |= Notification.FLAG_ONGOING_EVENT;
    builder.setContentText("Playing: " + show.getTrackTitle());
    builder.setContentTitle(show.getBandName());
    //notification.setLatestEventInfo(getApplicationContext(), "Wristband",
    //               "Playing: " + show.getTrackTitle(), pi);
    builder.setSmallIcon(R.drawable.actionicon);
    builder.setContentIntent(pi);
    ImageLoader il = new ImageLoader(Volley.newRequestQueue(this),new BitmapLruCache());
    il.get(show.getSmallArtUrl(), new ImageListener() {
      
      @Override
      public void onErrorResponse(VolleyError error) {
        startForeground(111,builder.build());
      }
      
      @Override
      public void onResponse(ImageContainer response, boolean isImmediate) {
        if(response.getBitmap() != null){
          builder.setLargeIcon(response.getBitmap());
          startForeground(111,builder.build());
        }
      }
    });
    
  }

  @Override
  public void onPrepared(MediaPlayer mp) {
    if(listener != null){
      listener.onPrepared();
    }
  }
  
  public boolean isPlaying(){
    try{
    return mediaPlayer.isPlaying();
    }catch(NullPointerException npe){
      stopSelf();
      return false;
    }
  }
  
  public boolean isInitialized(){
    return mediaPlayer != null;
  }
  
  public void seekTo(int position){
    mediaPlayer.seekTo(position);
  }
  
  public void cue(Show show){
    if(mediaPlayer.isPlaying()){
      mediaPlayer.stop();
    }
    mediaPlayer.reset();
    try {
      mediaPlayer.setDataSource(show.getTrackStreamingUrl());
    } 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();
    }
    mediaPlayer.prepareAsync();
    positionHandler.post(positionRunnable);
    showNotification(show);
  }
  
  public void stop(){
    if(mediaPlayer.isPlaying()){
      mediaPlayer.stop();
    }
    mediaPlayer.release();
    stopForeground(true);
  }
  
  public void play(){
    mediaPlayer.start();
  }
  
  public void pause(){
    mediaPlayer.pause();
  }
  
  public int getDuration(){
    return mediaPlayer.getDuration();
  }
  
  public interface PlaybackCallbacks{
    public void onPositionChange(int position, int duration);
    public void onPrepared();
    public void onBufferingUpdate(int percent);
    public void onCompleted();
  }

  @Override
  public void onBufferingUpdate(MediaPlayer mp, int percent) {
    if(listener != null){
      listener.onBufferingUpdate(percent);
    }
  }

  @Override
  public void onAudioFocusChange(int focusChange) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public boolean onError(MediaPlayer mp, int what, int extra) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public void onCompletion(MediaPlayer mp) {
    if(listener != null){
      listener.onCompleted();
    }
    positionHandler.removeCallbacks(positionRunnable);
  }

  @Override
  public boolean onInfo(MediaPlayer mp, int what, int extra) {
    // TODO Auto-generated method stub
    return false;
  }

  @Override
  public void onSeekComplete(MediaPlayer mp) {
    // TODO Auto-generated method stub
    
  }

}




Java Source Code List

android.UnusedStub.java
com.android.volley.AuthFailureError.java
com.android.volley.CacheDispatcher.java
com.android.volley.Cache.java
com.android.volley.DefaultRetryPolicy.java
com.android.volley.ExecutorDelivery.java
com.android.volley.NetworkDispatcher.java
com.android.volley.NetworkError.java
com.android.volley.NetworkResponse.java
com.android.volley.Network.java
com.android.volley.NoConnectionError.java
com.android.volley.ParseError.java
com.android.volley.RequestQueue.java
com.android.volley.Request.java
com.android.volley.ResponseDelivery.java
com.android.volley.Response.java
com.android.volley.RetryPolicy.java
com.android.volley.ServerError.java
com.android.volley.TimeoutError.java
com.android.volley.VolleyError.java
com.android.volley.VolleyLog.java
com.android.volley.toolbox.AndroidAuthenticator.java
com.android.volley.toolbox.Authenticator.java
com.android.volley.toolbox.BasicNetwork.java
com.android.volley.toolbox.ByteArrayPool.java
com.android.volley.toolbox.ClearCacheRequest.java
com.android.volley.toolbox.DiskBasedCache.java
com.android.volley.toolbox.HttpClientStack.java
com.android.volley.toolbox.HttpHeaderParser.java
com.android.volley.toolbox.HttpStack.java
com.android.volley.toolbox.HurlStack.java
com.android.volley.toolbox.ImageLoader.java
com.android.volley.toolbox.ImageRequest.java
com.android.volley.toolbox.JsonArrayRequest.java
com.android.volley.toolbox.JsonObjectRequest.java
com.android.volley.toolbox.JsonRequest.java
com.android.volley.toolbox.NetworkImageView.java
com.android.volley.toolbox.NoCache.java
com.android.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.volley.toolbox.RequestFuture.java
com.android.volley.toolbox.StringRequest.java
com.android.volley.toolbox.Volley.java
com.nimo.wristband.DetailActivity.java
com.nimo.wristband.MainActivity.java
com.nimo.wristband.SettingsActivity.java
com.nimo.wristband.adapters.ShowListAdapter.java
com.nimo.wristband.constants.Constants.java
com.nimo.wristband.db.ShowData.java
com.nimo.wristband.db.SimpleStorage.java
com.nimo.wristband.fragments.MusicPlayerFragment.java
com.nimo.wristband.fragments.WristbandMap.java
com.nimo.wristband.net.BitmapLruCache.java
com.nimo.wristband.net.Show.java
com.nimo.wristband.net.WristbandRequest.java
com.nimo.wristband.net.WristbandResponse.java
com.nimo.wristband.service.WristbandPlayerService.java