it.tidalwave.mobile.android.media.AndroidMediaPlayer Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* $Id: AndroidMediaPlayer.java,v a52511a71d18 2010/06/26 12:40:42 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile.android.media;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Map;
import java.io.IOException;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.media.Media;
import static it.tidalwave.mobile.util.Downloadable.Downloadable;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import it.tidalwave.mobile.android.media.impl.AndroidMediaPlayerController;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class AndroidMediaPlayer implements it.tidalwave.mobile.media.MediaPlayer
{
private static final String CLASS = AndroidMediaPlayer.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
private final Map playerMapByMedia = new IdentityHashMap();
@Nonnull
private final Context context = Locator.find(Context.class);
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Nonnull
public Controller play (final @Nonnull Media media)
throws IOException
{
logger.info("play(%s)", media);
MediaPlayer oldPlayer = null;
MediaPlayer newPlayer = null;
synchronized (this)
{
oldPlayer = playerMapByMedia.remove(media);
final Uri uri = Uri.fromFile(media.as(Downloadable).getFile());
newPlayer = MediaPlayer.create(context, uri);
logger.info(">>>> created %s for %s", newPlayer, uri);
if (newPlayer == null) // sometimes it happens
{
dispose(oldPlayer);
throw new IOException("MediaPlayer.create() failed for " + uri);
}
playerMapByMedia.put(media, newPlayer);
}
dispose(oldPlayer);
newPlayer.start(); // prepare() already done by create()
return new AndroidMediaPlayerController(newPlayer);
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
private void stop (final @Nonnull Media media)
{
logger.info("stop(%s)", media);
MediaPlayer oldPlayer = null;
synchronized (this)
{
oldPlayer = playerMapByMedia.remove(media);
}
dispose(oldPlayer);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void stop()
{
logger.info("stop()");
for (final Media media : new ArrayList(playerMapByMedia.keySet()))
{
stop(media);
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private void dispose (final @Nonnull MediaPlayer oldPlayer)
{
if (oldPlayer != null)
{
logger.fine(">>>> stopping %s...", oldPlayer);
try
{
oldPlayer.stop();
oldPlayer.release();
}
catch (IllegalStateException e)
{
logger.warning("While disposing ", e);
logger.throwing("dispose()", CLASS, e);
}
}
}
}