Back to project page tetris-android.
The source code is released under:
MIT License
If you think the Android project tetris-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.badlogic.androidgames.framework.impl; //from www . j a v a 2s . co m import java.io.IOException; import android.content.res.AssetFileDescriptor; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import com.badlogic.androidgames.framework.Music; public class AndroidMusic implements Music, OnCompletionListener { 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); } 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 arg0) { synchronized (this) { isPrepared = false; } } }