Android Open Source - PlayerHater Media Player Pool






From Project

Back to project page PlayerHater.

License

The source code is released under:

Apache License

If you think the Android project PlayerHater 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 2013 Chris Rhoden, Rebecca Nesson, Public Radio Exchange
 */*from   www  . j av  a 2  s  .co m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.prx.playerhater.mediaplayer;

import android.content.Context;
import android.net.Uri;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.prx.playerhater.util.Log;

public class MediaPlayerPool<P extends SynchronousPlayer> {

  private final Map<Uri, P> mMediaPlayers = new HashMap<Uri, P>();
  private final Set<P> mIdlePlayers = new HashSet<P>();
  private final List<Uri> mRequests = new ArrayList<Uri>();
  private final Class<? extends P> mClass;

  public static <SynchronousPlayerClass extends SynchronousPlayer> MediaPlayerPool<SynchronousPlayerClass> getInstance(Context context,
      Class<SynchronousPlayerClass> klass) {
    return new MediaPlayerPool<SynchronousPlayerClass>(context, klass);
  }

  public MediaPlayerPool(Context context, Class<P> mediaPlayerClass) {
    this(context, mediaPlayerClass, 3);
  }

  public MediaPlayerPool(Context context, Class<P> mediaPlayerClass, int size) {
    mClass = mediaPlayerClass;
    for (int i = 0; i < size; i++) {
      try {
        mIdlePlayers.add(mClass.getConstructor(Context.class).newInstance(context));
      } catch (InstantiationException e) {
        throw new IllegalArgumentException(e);
      } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(e);
      } catch (NoSuchMethodException e) {
                throw new IllegalArgumentException(e);
            } catch (InvocationTargetException e) {
                throw new IllegalArgumentException(e);
            }
        }
  }

  public synchronized void release() {
    for (SynchronousPlayer player : mIdlePlayers) {
      player.release();
    }
    mIdlePlayers.clear();
    for (Uri uri : mRequests) {
      mMediaPlayers.remove(uri).release();
    }
    mRequests.clear();
    for (SynchronousPlayer player : mMediaPlayers.values()) {
      player.release();
    }
    mMediaPlayers.clear();
  }

  public synchronized void prepare(Context context, Uri uri) {
    if (uri == null) {
      throw new IllegalArgumentException(
          "can't prepare a player for a null uri!");
    }
    if (!mMediaPlayers.containsKey(uri)) {
      P player = getPlayer();
      Log.d("Preparing " + player + " for " + uri);
      player.prepare(context, uri);
      addPlayer(player, uri);
    }
  }

  public synchronized P getPlayer(Context context, Uri uri) {
    Log.d("Getting player for " + uri);
    if (mMediaPlayers.containsKey(uri)) {
      mRequests.remove(uri);
      Log.d("Found one (" + mMediaPlayers.get(uri) + ")");
      return mMediaPlayers.remove(uri);
    } else {
      P player = getPlayer();
      player.prepare(context, uri);
      return player;
    }
  }

  public synchronized void recycle(P player) {
    if (player != null && player.getState() != StatelyPlayer.END) {
      player.reset();
      mIdlePlayers.add(player);
    }
  }

  @SuppressWarnings("unchecked")
  private synchronized P getPlayer() {
    if (mIdlePlayers.size() > 0) {
      P player = (P) mIdlePlayers.toArray()[0];
      mIdlePlayers.remove(player);
      Log.d("Getting idle player (" + player + ")");
      return player;
    } else if (mRequests.size() > 0) {
      Uri leastRecentReq = mRequests.remove(mRequests.size() - 1);
      Log.d("Recycling the player that is prepared for " + leastRecentReq);
      P player = mMediaPlayers.remove(leastRecentReq);
      Log.d("Player: " + player);
      player.reset();
      return player;
    } else {
      throw new IllegalStateException(
          "MediaPlayer resources exhausted. Are you sure you're #recycle()ing on time?");
    }
  }

  private synchronized void addPlayer(P player, Uri uri) {
    mRequests.remove(uri);
    mRequests.add(0, uri);
    mMediaPlayers.put(uri, player);
  }
}




Java Source Code List

.AbstractPlugin.java
.AudioFocusPlugin.java
.BackgroundedPlugin.java
.BoundPlayerHater.java
.BroadcastReceiver.java
.ClientPlugin.java
.Config.java
.ExpandableNotificationPlugin.java
.HeadphoneButtonGestureHelper.java
.IPlayerHater.java
.LockScreenControlsPlugin.java
.Log.java
.MediaPlayerPool.java
.NotificationPlugin.java
.OnAudioFocusChangedListener.java
.PebblePlugin.java
.PlaybackService.java
.PlayerHaterClient.java
.PlayerHaterListenerPlugin.java
.PlayerHaterListener.java
.PlayerHaterPlugin.java
.PlayerHaterServer.java
.PlayerHaterService.java
.PlayerHater.java
.PlayerStateWatcher.java
.Player.java
.PlaylistParser.java
.PlaylistSupportingPlayer.java
.PluginCollection.java
.Receiver.java
.RemoteControlButtonReceiver.java
.RemoteSong.java
.ServerPlayerHater.java
.ServicePlayerHater.java
.SongHost.java
.SongQueue.java
.Song.java
.Songs.java
.StatelyPlayer.java
.SynchronousPlayer.java
.ThreadsafePlayerHater.java
.ThreadsafeServicePlayerHater.java
.TouchableNotificationPlugin.java
org.prx.playerhater.BroadcastReceiver.java
org.prx.playerhater.PlaybackService.java
org.prx.playerhater.PlayerHaterListener.java
org.prx.playerhater.PlayerHaterPlugin.java
org.prx.playerhater.PlayerHater.java
org.prx.playerhater.Song.java
org.prx.playerhater.broadcast.HeadphoneButtonGestureHelper.java
org.prx.playerhater.broadcast.OnAudioFocusChangedListener.java
org.prx.playerhater.broadcast.Receiver.java
org.prx.playerhater.broadcast.RemoteControlButtonReceiver.java
org.prx.playerhater.ipc.ClientPlugin.java
org.prx.playerhater.ipc.PlayerHaterClient.java
org.prx.playerhater.ipc.PlayerHaterServer.java
org.prx.playerhater.ipc.ServerPlayerHater.java
org.prx.playerhater.mediaplayer.MediaPlayerPool.java
org.prx.playerhater.mediaplayer.Player.java
org.prx.playerhater.mediaplayer.PlaylistSupportingPlayer.java
org.prx.playerhater.mediaplayer.StatelyPlayer.java
org.prx.playerhater.mediaplayer.SynchronousPlayer.java
org.prx.playerhater.plugins.AbstractPlugin.java
org.prx.playerhater.plugins.AudioFocusPlugin.java
org.prx.playerhater.plugins.BackgroundedPlugin.java
org.prx.playerhater.plugins.ExpandableNotificationPlugin.java
org.prx.playerhater.plugins.LockScreenControlsPlugin.java
org.prx.playerhater.plugins.NotificationPlugin.java
org.prx.playerhater.plugins.PebblePlugin.java
org.prx.playerhater.plugins.PlayerHaterListenerPlugin.java
org.prx.playerhater.plugins.PluginCollection.java
org.prx.playerhater.plugins.ScrubbableLockScreenControlsPlugin.java
org.prx.playerhater.plugins.TouchableNotificationPlugin.java
org.prx.playerhater.service.PlayerHaterService.java
org.prx.playerhater.service.PlayerStateWatcher.java
org.prx.playerhater.songs.RemoteSong.java
org.prx.playerhater.songs.SongHost.java
org.prx.playerhater.songs.SongQueue.java
org.prx.playerhater.songs.Songs.java
org.prx.playerhater.util.Config.java
org.prx.playerhater.util.IPlayerHater.java
org.prx.playerhater.util.Log.java
org.prx.playerhater.util.PlaylistParser.java
org.prx.playerhater.wrappers.BoundPlayerHater.java
org.prx.playerhater.wrappers.ServicePlayerHater.java
org.prx.playerhater.wrappers.ThreadsafePlayerHater.java
org.prx.playerhater.wrappers.ThreadsafeServicePlayerHater.java