Android Open Source - GameEngine Android Music






From Project

Back to project page GameEngine.

License

The source code is released under:

# Copyright 2013 JK Wood <joshuakwood@gmail.com> # This framework is released under the Dog-on-Fire License: # If use of this framework causes your dog to catch on fire, # you agree to send me five d...

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

package com.slaxer.framework.implementation;
/*from w  w w  . j  a v  a2  s  .c  o  m*/
import java.io.IOException;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;

import com.slaxer.framework.Music;

public class AndroidMusic implements Music, OnCompletionListener, OnSeekCompleteListener, OnPreparedListener, OnVideoSizeChangedListener {
  MediaPlayer mediaPlayer;
  boolean isPrepared = false;

  public AndroidMusic(AssetFileDescriptor assetDescriptor) {
    mediaPlayer = new MediaPlayer();
    try{
      mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(), assetDescriptor.getStartOffset(), assetDescriptor.getLength());
      mediaPlayer.prepare();
      isPrepared = true;
      mediaPlayer.setOnCompletionListener(this);
      mediaPlayer.setOnSeekCompleteListener(this);
      mediaPlayer.setOnPreparedListener(this);
      mediaPlayer.setOnVideoSizeChangedListener(this);
    } catch(Exception e){
      throw new RuntimeException("Couldn't load music");
    }
  }
  
  @Override
  public void dispose() {
    if(this.mediaPlayer.isPlaying())
      this.mediaPlayer.stop();
    this.mediaPlayer.release();    
  }
  
  @Override
  public boolean isLooping() {
    return mediaPlayer.isLooping();
  }
  
  @Override
  public boolean isPlaying() {
    return this.mediaPlayer.isPlaying();
  }
  
  @Override
  public boolean isStopped() {
    return !isPrepared;
  }
  
  @Override
  public void pause() {
    if(this.mediaPlayer.isPlaying())
      mediaPlayer.pause();
  }
  
  @Override
  public void play() {
    if(this.mediaPlayer.isPlaying())
      return;
    
    try{
      synchronized(this){
        if(!isPrepared)
          mediaPlayer.prepare();
        mediaPlayer.start();
      }
    } catch(IllegalStateException e){
      e.printStackTrace();
    } catch(IOException e){
      e.printStackTrace();
    }
  }

  @Override
  public void setLooping(boolean isLooping) {
    mediaPlayer.setLooping(isLooping);
  }

  @Override
  public void setVolume(float volume) {
    mediaPlayer.setVolume(volume, volume);    
  }

  @Override
  public void stop() {
    if(this.mediaPlayer.isPlaying()){
      this.mediaPlayer.stop();
      
      synchronized(this){
        isPrepared = false;
      }
    }
    
  }

  @Override
  public void onCompletion(MediaPlayer player) {
    synchronized(this){
      isPrepared = false;
    }
    
  }

  @Override
  public void seekBegin() {
    mediaPlayer.seekTo(0);
  }
  
  @Override
  public void onPrepared(MediaPlayer player){
    synchronized(this){
      isPrepared = true;
    }
  }
  
  @Override
  public void onSeekComplete(MediaPlayer player) {
    // TODO Auto-generated method stub    
  }
  
  @Override
  public void onVideoSizeChanged(MediaPlayer player, int width, int height) {
    // TODO Auto-generated method stub
    
  }

}




Java Source Code List

com.slaxer.framework.Audio.java
com.slaxer.framework.FileIO.java
com.slaxer.framework.Game.java
com.slaxer.framework.Graphics.java
com.slaxer.framework.Image.java
com.slaxer.framework.Input.java
com.slaxer.framework.Music.java
com.slaxer.framework.Pool.java
com.slaxer.framework.Screen.java
com.slaxer.framework.Sound.java
com.slaxer.framework.implementation.AndroidAudio.java
com.slaxer.framework.implementation.AndroidFastRenderView.java
com.slaxer.framework.implementation.AndroidFileIO.java
com.slaxer.framework.implementation.AndroidGame.java
com.slaxer.framework.implementation.AndroidGraphics.java
com.slaxer.framework.implementation.AndroidImage.java
com.slaxer.framework.implementation.AndroidInput.java
com.slaxer.framework.implementation.AndroidMusic.java
com.slaxer.framework.implementation.AndroidSound.java
com.slaxer.framework.implementation.CollRect.java
com.slaxer.framework.implementation.Dimension.java
com.slaxer.framework.implementation.MultiTouchHandler.java
com.slaxer.framework.implementation.Rectangle.java
com.slaxer.framework.implementation.SingleTouchHandler.java
com.slaxer.framework.implementation.TouchHandler.java