Android Open Source - AndroidFramework Android Music






From Project

Back to project page AndroidFramework.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project AndroidFramework 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.mdobbins.implementation.framework;
// w ww . j av  a2 s  .com
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.mdobbins.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() == true){
        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) {
        // TODO Auto-generated method stub
         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.mdobbins.framework.Audio.java
com.mdobbins.framework.Audio.java
com.mdobbins.framework.FileIO.java
com.mdobbins.framework.FileIO.java
com.mdobbins.framework.Game.java
com.mdobbins.framework.Game.java
com.mdobbins.framework.Graphics.java
com.mdobbins.framework.Graphics.java
com.mdobbins.framework.Image.java
com.mdobbins.framework.Image.java
com.mdobbins.framework.Input.java
com.mdobbins.framework.Input.java
com.mdobbins.framework.Music.java
com.mdobbins.framework.Music.java
com.mdobbins.framework.Pool.java
com.mdobbins.framework.Pool.java
com.mdobbins.framework.Screen.java
com.mdobbins.framework.Screen.java
com.mdobbins.framework.Sound.java
com.mdobbins.framework.Sound.java
com.mdobbins.implementation.framework.AndroidAudio.java
com.mdobbins.implementation.framework.AndroidFastRenderView.java
com.mdobbins.implementation.framework.AndroidFileIO.java
com.mdobbins.implementation.framework.AndroidGame.java
com.mdobbins.implementation.framework.AndroidGraphics.java
com.mdobbins.implementation.framework.AndroidImage.java
com.mdobbins.implementation.framework.AndroidInput.java
com.mdobbins.implementation.framework.AndroidMusic.java
com.mdobbins.implementation.framework.AndroidSound.java
com.mdobbins.implementation.framework.MultiTouchHandler.java
com.mdobbins.implementation.framework.SingleTouchHandler.java
com.mdobbins.implementation.framework.TouchHandler.java
com.mdobbins.sample.Assets.java
com.mdobbins.sample.GameScreen.java
com.mdobbins.sample.LoadingScreen.java
com.mdobbins.sample.MainMenuScreen.java
com.mdobbins.sample.SampleGame.java