Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- Copyright (c) 2010-2011 cocos2d-x.org
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- package org.cocos2dx.lib;
- import java.io.FileInputStream;
- import java.io.IOException;
- import com.android.vending.expansion.zipfile.APKExpansionSupport;
- import com.android.vending.expansion.zipfile.ZipResourceFile;
- import android.content.Context;
- import android.content.res.AssetFileDescriptor;
- import android.media.MediaPlayer;
- import android.util.Log;
- public class Cocos2dxMusic {
- // ===========================================================
- // Constants
- // ===========================================================
- private static final String TAG = Cocos2dxMusic.class.getSimpleName();
- // ===========================================================
- // Fields
- // ===========================================================
- private final Context mContext;
- private MediaPlayer mBackgroundMediaPlayer;
- private float mLeftVolume;
- private float mRightVolume;
- private boolean mPaused;
- private String mCurrentPath;
- private static ZipResourceFile zip_resource_file = null;
- // ===========================================================
- // Constructors
- // ===========================================================
- public Cocos2dxMusic(final Context pContext) {
- this.mContext = pContext;
- try {
- //Change the second argument to match with your version code
- zip_resource_file = APKExpansionSupport.getAPKExpansionZipFile(pContext, 2, 0);
- } catch ( IOException e ) {
- Log.e( "Cocos2dxMusic" , "Error initialising ZipResourceFile: ", e );
- }
- this.initData();
- }
- // ===========================================================
- // Getter & Setter
- // ===========================================================
- // ===========================================================
- // Methods for/from SuperClass/Interfaces
- // ===========================================================
- // ===========================================================
- // Methods
- // ===========================================================
- public void preloadBackgroundMusic(final String pPath) {
- if ((this.mCurrentPath == null) || (!this.mCurrentPath.equals(pPath))) {
- // preload new background music
- // release old resource and create a new one
- if (this.mBackgroundMediaPlayer != null) {
- this.mBackgroundMediaPlayer.release();
- }
- this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);
- // record the path
- this.mCurrentPath = pPath;
- }
- }
- public void playBackgroundMusic(final String pPath, final boolean isLoop) {
- if (this.mCurrentPath == null) {
- // it is the first time to play background music or end() was called
- this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);
- this.mCurrentPath = pPath;
- } else {
- if (!this.mCurrentPath.equals(pPath)) {
- // play new background music
- // release old resource and create a new one
- if (this.mBackgroundMediaPlayer != null) {
- this.mBackgroundMediaPlayer.release();
- }
- this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);
- // record the path
- this.mCurrentPath = pPath;
- }
- }
- if (this.mBackgroundMediaPlayer == null) {
- Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: background media player is null");
- } else {
- // if the music is playing or paused, stop it
- this.mBackgroundMediaPlayer.stop();
- this.mBackgroundMediaPlayer.setLooping(isLoop);
- try {
- this.mBackgroundMediaPlayer.prepare();
- this.mBackgroundMediaPlayer.seekTo(0);
- this.mBackgroundMediaPlayer.start();
- this.mPaused = false;
- } catch (final Exception e) {
- Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: error state");
- }
- }
- }
- public void stopBackgroundMusic() {
- if (this.mBackgroundMediaPlayer != null) {
- this.mBackgroundMediaPlayer.stop();
- // should set the state, if not, the following sequence will be error
- // play -> pause -> stop -> resume
- this.mPaused = false;
- }
- }
- public void pauseBackgroundMusic() {
- if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) {
- this.mBackgroundMediaPlayer.pause();
- this.mPaused = true;
- }
- }
- public void resumeBackgroundMusic() {
- if (this.mBackgroundMediaPlayer != null && this.mPaused) {
- this.mBackgroundMediaPlayer.start();
- this.mPaused = false;
- }
- }
- public void rewindBackgroundMusic() {
- if (this.mBackgroundMediaPlayer != null) {
- this.mBackgroundMediaPlayer.stop();
- try {
- this.mBackgroundMediaPlayer.prepare();
- this.mBackgroundMediaPlayer.seekTo(0);
- this.mBackgroundMediaPlayer.start();
- this.mPaused = false;
- } catch (final Exception e) {
- Log.e(Cocos2dxMusic.TAG, "rewindBackgroundMusic: error state");
- }
- }
- }
- public boolean isBackgroundMusicPlaying() {
- boolean ret = false;
- if (this.mBackgroundMediaPlayer == null) {
- ret = false;
- } else {
- ret = this.mBackgroundMediaPlayer.isPlaying();
- }
- return ret;
- }
- public void end() {
- if (this.mBackgroundMediaPlayer != null) {
- this.mBackgroundMediaPlayer.release();
- }
- this.initData();
- }
- public float getBackgroundVolume() {
- if (this.mBackgroundMediaPlayer != null) {
- return (this.mLeftVolume + this.mRightVolume) / 2;
- } else {
- return 0.0f;
- }
- }
- public void setBackgroundVolume(float pVolume) {
- if (pVolume < 0.0f) {
- pVolume = 0.0f;
- }
- if (pVolume > 1.0f) {
- pVolume = 1.0f;
- }
- this.mLeftVolume = this.mRightVolume = pVolume;
- if (this.mBackgroundMediaPlayer != null) {
- this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
- }
- }
- private void initData() {
- this.mLeftVolume = 0.5f;
- this.mRightVolume = 0.5f;
- this.mBackgroundMediaPlayer = null;
- this.mPaused = false;
- this.mCurrentPath = null;
- }
- /**
- * create mediaplayer for music
- *
- * @param pPath
- * the pPath relative to assets
- * @return
- */
- private MediaPlayer createMediaplayer(final String pPath) {
- MediaPlayer mediaPlayer = new MediaPlayer();
- try {
- if (pPath.startsWith("/")) {
- final AssetFileDescriptor assetFileDescriptor = zip_resource_file.getAssetFileDescriptor( "assets/" + pPath );
- final FileInputStream fis = assetFileDescriptor.createInputStream();
- mediaPlayer.setDataSource(fis.getFD());
- fis.close();
- } else {
- final AssetFileDescriptor assetFileDescriptor = zip_resource_file.getAssetFileDescriptor( "assets/" + pPath );
- mediaPlayer = new MediaPlayer();
- mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
- }
- mediaPlayer.prepare();
- mediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
- } catch (final IOException e){
- Log.e(Cocos2dxMusic.TAG, "ioerror: " + e.getMessage());
- }
- catch (final Exception e) {
- mediaPlayer = null;
- Log.e(Cocos2dxMusic.TAG, "error: " + e.getMessage(), e);
- }
- return mediaPlayer;
- }
- // ===========================================================
- // Inner and Anonymous Classes
- // ===========================================================
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement