Android Open Source - AndroidPlayer Three D Service






From Project

Back to project page AndroidPlayer.

License

The source code is released under:

Copyright 2012 Michael Marner (michael@20papercups.net) all rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following...

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

// Copyright 2012 Michael Marner (michael@20papercups.net) all rights reserved.
///*  w w  w  .j  av  a2s  .c o  m*/
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
//    1. Redistributions of source code must retain the above copyright notice, this list of
//       conditions and the following disclaimer.
//
//    2. Redistributions in binary form must reproduce the above copyright notice, this list
//       of conditions and the following disclaimer in the documentation and/or other materials
//       provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of the authors.


package com.threedradio.player;



import java.io.IOException;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;


/**
 * This class is the service that is actually responsible for streaming the music.
 * 
 * @author Michael Marner (michael@20papercups.net)
 *
 */
public class ThreeDService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {

  /**
   * A media player for actually streaming the audio!
   */
  MediaPlayer player;
  
  
  private final IBinder mBinder = new LocalBinder();
  
  /**
   * While playing, we will use a wifi lock to prevent network dropping out.
   */
  WifiLock wifiLock;

  
  // I hate magic numbers and strings, but this seems to be the way its done!
  public static String ACTION_PLAY = "com.threedradio.action.PLAY";
  public static int NOTIFICATION_ID = 234894875;
  
  
  /**
   * Initialising the service simply means creating the media player and getting a wifi lock.
   */
  public void onCreate() {
    Log.d("ThreeD", "Service created");
    player = new MediaPlayer();
    wifiLock = ((WifiManager) getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    
  }
  
  
  /**
   * When the service is destroyed we need to clean up the resources we're using.
   * Mostly this is just freeing the media player.
   */
  public void onDestroy() {
         if (player != null) player.release();
     }
  
  
  /**
   * When the service is started, initialise the media player, and add the notification thing
   * indicating the service is running.
   */
  public int onStartCommand(Intent intent, int flags, int startID) {
    Log.d("ThreeD", "service started");
    if (intent != null) {
      if (intent.getAction().equals(ACTION_PLAY)) {      
        try {
          player.reset();
          player.setDataSource(getApplicationContext(), Uri.parse("http://public-radio1.internode.on.net:8002/137"));
          player.setOnPreparedListener(this);
          player.setOnErrorListener(this);
          player.setOnCompletionListener(this);
          player.setAudioStreamType(AudioManager.STREAM_MUSIC);
          
          player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
          wifiLock.acquire();
          player.prepareAsync();
        }
        catch (IOException e) {
          // TODO We should probably do something nice here!
          e.printStackTrace();
        }
      }
    }
    
    /*
     * All this next crap is just setting up the notification.
     */
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
        new Intent(getApplicationContext(), MainActivity.class),
        PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification();
    notification.tickerText = "Three D Radio";
    notification.icon = R.drawable.notification;
    
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(getApplicationContext(), "Three D Radio",
                "Streaming live", pi);
    startForeground(NOTIFICATION_ID, notification);
    
    return START_STICKY;
  }
  
  
  /**
   * Test whether music is playing.
   * @return True if playing, otherwise false.
   */
  public boolean isPlaying() {
    return player.isPlaying();
  }
  
  /**
   * Stop streaming music and the service.
   */
  public void stop() {
    player.stop();
    wifiLock.release();
    stopForeground(true);
  }
  

  /**
   * Called if the media player experiences an error.
   * I don't know exactly what errors cause this.
   * However, we should do something more graceful in here.
   */
  public boolean onError(MediaPlayer mp, int what, int extra) {
    Log.d("ThreeD", "Media player error");
    wifiLock.release();
    stopForeground(true);
    this.stopSelf();
    return true;
  }

  
  /**
   * This method is called when the media player finishes. Believe it or not
   * this is also called if the network drops out. We should do something more
   * graceful in here.
   */
  public void onCompletion(MediaPlayer mp) {
    Log.d("ThreeD", "Media player completed");
    wifiLock.release();
    stopForeground(true);
    this.stopSelf();
  }
  
  
  @Override
  public IBinder onBind(Intent arg0) {
    return mBinder;
  }
  public void onPrepared(MediaPlayer mp) {
    player.start();
  }
  public class LocalBinder extends Binder {
        ThreeDService getService() {
            return ThreeDService.this;
        }
    }

}




Java Source Code List

com.threedradio.player.MainActivity.java
com.threedradio.player.ThreeDService.java