it.tidalwave.mobile.android.media.impl.AndroidMediaPlayerController 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: AndroidMediaPlayerController.java,v f770a50fc8aa 2010/07/02 00:40:06 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile.android.media.impl;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.mobile.media.MediaPlayer.Controller;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class AndroidMediaPlayerController extends Controller
{
private static final String CLASS = AndroidMediaPlayerController.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
private MediaPlayer player;
private final OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener()
{
public void onCompletion (final @Nonnull MediaPlayer mediaPlayer)
{
mediaPlayer.setOnCompletionListener(null);
setPlaying(false);
}
};
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public AndroidMediaPlayerController (final @Nonnull MediaPlayer player)
{
this.player = player;
setPlaying(true);
player.setOnCompletionListener(onCompletionListener);
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void start()
{
if (player != null)
{
player.start();
setPlaying(true);
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void stop()
{
if (player != null)
{
player.stop();
setPlaying(false);
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void pause()
{
if (player != null)
{
player.pause();
setPlaying(false);
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Nonnegative
public int getDuration()
{
return (player == null) ? 0 : player.getDuration();
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Nonnegative
public int getPosition()
{
return (player == null) ? 0 : player.getCurrentPosition();
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void setPosition (final @Nonnegative int position)
{
if (player != null)
{
player.seekTo(position);
}
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public boolean isPlaying()
{
return (player == null) ? false : player.isPlaying();
}
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void dispose()
{
if (player != null)
{
setPlaying(false);
logger.info("Disposing player %s", player);
player.release();
player.setOnCompletionListener(null);
player = null;
logger.info(">>>> disposed");
}
}
}