Android Open Source - project2 Game Music






From Project

Back to project page project2.

License

The source code is released under:

MIT License

If you think the Android project project2 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 team2.scdm;
/*from w  w  w . ja va 2 s . co  m*/
import java.io.IOException;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

public class GameMusic implements Media,OnCompletionListener{
  
     MediaPlayer mediaPlayer;
      boolean isPrepared = false;

      public GameMusic(AssetFileDescriptor assetDescriptor) {
          mediaPlayer = new MediaPlayer();
          try {
              mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(),
                      assetDescriptor.getStartOffset(),
                      assetDescriptor.getLength());
              mediaPlayer.prepare();
              isPrepared = true;
              mediaPlayer.setOnCompletionListener(this);
          } catch (Exception e) {
              throw new RuntimeException("Couldn't load music");
          }
      }

      @Override
      public void dispose() {
          if (mediaPlayer.isPlaying())
              mediaPlayer.stop();
          mediaPlayer.release();
      }

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

      @Override
      public boolean isPlaying() {
          return mediaPlayer.isPlaying();
      }

      @Override
      public boolean isStopped() {
          return !isPrepared;
      }

      @Override
      public void pause() {
          if (mediaPlayer.isPlaying())
              mediaPlayer.pause();
      }

      @Override
      public void play() {
          if (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() {
          mediaPlayer.stop();
          synchronized (this) {
              isPrepared = false;
          }
      }

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




Java Source Code List

team2.scdm.AboutActivity.java
team2.scdm.Armor.java
team2.scdm.Assets.java
team2.scdm.Audio.java
team2.scdm.BattleActivity.java
team2.scdm.Enemy.java
team2.scdm.Folk.java
team2.scdm.GameAudio.java
team2.scdm.GameMusic.java
team2.scdm.GameOverActivity.java
team2.scdm.GameSound.java
team2.scdm.GestureListener.java
team2.scdm.Intro1.java
team2.scdm.Intro2.java
team2.scdm.Intro3.java
team2.scdm.Intro4.java
team2.scdm.Intro5.java
team2.scdm.Intro6.java
team2.scdm.InventoryActivity.java
team2.scdm.Item.java
team2.scdm.LocationMapActivity.java
team2.scdm.Media.java
team2.scdm.MenuActivity.java
team2.scdm.NPC.java
team2.scdm.NameActivity.java
team2.scdm.Player.java
team2.scdm.SettingsActivity.java
team2.scdm.Sound.java
team2.scdm.TitleActivity.java
team2.scdm.Weapon.java