All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.taxonomy.factsheet.impl.sound.PlayController Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile
 * SCM: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.taxonomy.factsheet.impl.sound;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.inject.Provider;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.media.Media;
import it.tidalwave.mobile.media.MediaPlayer;
import it.tidalwave.mobile.media.MediaPlayer.Controller;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.sound.TaxonSoundFactSheetView;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.sound.DefaultTaxonSoundFactSheetViewController;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @stereotype Controller
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@Slf4j @RequiredArgsConstructor
public class PlayController 
  {
    protected final DefaultTaxonSoundFactSheetViewController controller;
    
    protected final TaxonSoundFactSheetView view;
    
    @CheckForNull
    public Controller mediaPlayerController;

    @Nonnull
    private final Provider mediaPlayer = Locator.createProviderFor(MediaPlayer.class);

    @CheckForNull
    public Media playingMedia;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    /* package */ final PropertyChangeListener mediaChangeListener = new PropertyChangeListener()
      {
        public void propertyChange (final @Nonnull PropertyChangeEvent event)
          {
            log.info("mediaChangeListener.propertyChange({})", event);
            controller.notifyStatusChanged();
          }
      };

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void play (final @Nonnull Media media) 
      throws IOException 
      {
        log.info("play({})", media);
        
        if (mediaPlayerController != null)
          {
            stop();
          }
        
        playingMedia = media;
        mediaPlayerController = mediaPlayer.get().play(media);
        mediaPlayerController.addPropertyChangeListener(mediaChangeListener);
        view.notifyMediaPlaying(mediaPlayerController);
        controller.notifyStatusChanged();
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public boolean isPlaying (final @Nonnull Media media) 
      {
//        return isStatus(DOWNLOADED, OBSOLETE) && ...
        return (mediaPlayerController != null) && mediaPlayerController.isPlaying() && (media == playingMedia);
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void stop()
      {
        log.info("stop()");
        
        if (mediaPlayerController != null)
          {
            mediaPlayerController.removePropertyChangeListener(mediaChangeListener);
            mediaPlayerController.dispose();
            mediaPlayerController = null;
            controller.notifyStatusChanged();
          }    
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy