Advertisement
VandroiyX

Cocos2dxMusic.java APK Expansion

Sep 11th, 2013
1,555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.32 KB | None | 0 0
  1. /****************************************************************************
  2. Copyright (c) 2010-2011 cocos2d-x.org
  3.  
  4. http://www.cocos2d-x.org
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23.  ****************************************************************************/
  24. package org.cocos2dx.lib;
  25.  
  26. import java.io.FileInputStream;
  27. import java.io.IOException;
  28.  
  29. import com.android.vending.expansion.zipfile.APKExpansionSupport;
  30. import com.android.vending.expansion.zipfile.ZipResourceFile;
  31.  
  32. import android.content.Context;
  33. import android.content.res.AssetFileDescriptor;
  34. import android.media.MediaPlayer;
  35. import android.util.Log;
  36.  
  37. public class Cocos2dxMusic {
  38.     // ===========================================================
  39.     // Constants
  40.     // ===========================================================
  41.  
  42.     private static final String TAG = Cocos2dxMusic.class.getSimpleName();
  43.  
  44.     // ===========================================================
  45.     // Fields
  46.     // ===========================================================
  47.  
  48.     private final Context mContext;
  49.     private MediaPlayer mBackgroundMediaPlayer;
  50.     private float mLeftVolume;
  51.     private float mRightVolume;
  52.     private boolean mPaused;
  53.     private String mCurrentPath;
  54.    
  55.     private static ZipResourceFile zip_resource_file = null;
  56.  
  57.     // ===========================================================
  58.     // Constructors
  59.     // ===========================================================
  60.  
  61.     public Cocos2dxMusic(final Context pContext) {
  62.         this.mContext = pContext;
  63.         try {
  64.             //Change the second argument to match with your version code
  65.             zip_resource_file = APKExpansionSupport.getAPKExpansionZipFile(pContext, 2, 0);
  66.         } catch ( IOException e ) {
  67.             Log.e( "Cocos2dxMusic" ,  "Error initialising ZipResourceFile: ", e );
  68.         }
  69.         this.initData();
  70.     }
  71.  
  72.     // ===========================================================
  73.     // Getter & Setter
  74.     // ===========================================================
  75.  
  76.     // ===========================================================
  77.     // Methods for/from SuperClass/Interfaces
  78.     // ===========================================================
  79.  
  80.     // ===========================================================
  81.     // Methods
  82.     // ===========================================================
  83.  
  84.     public void preloadBackgroundMusic(final String pPath) {
  85.         if ((this.mCurrentPath == null) || (!this.mCurrentPath.equals(pPath))) {
  86.             // preload new background music
  87.  
  88.             // release old resource and create a new one
  89.             if (this.mBackgroundMediaPlayer != null) {
  90.                 this.mBackgroundMediaPlayer.release();
  91.             }
  92.             this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);
  93.  
  94.             // record the path
  95.             this.mCurrentPath = pPath;
  96.         }
  97.     }
  98.  
  99.     public void playBackgroundMusic(final String pPath, final boolean isLoop) {
  100.         if (this.mCurrentPath == null) {
  101.             // it is the first time to play background music or end() was called
  102.             this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);
  103.             this.mCurrentPath = pPath;
  104.         } else {
  105.             if (!this.mCurrentPath.equals(pPath)) {
  106.                 // play new background music
  107.  
  108.                 // release old resource and create a new one
  109.                 if (this.mBackgroundMediaPlayer != null) {
  110.                     this.mBackgroundMediaPlayer.release();
  111.                 }
  112.                 this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);
  113.  
  114.                 // record the path
  115.                 this.mCurrentPath = pPath;
  116.             }
  117.         }
  118.  
  119.         if (this.mBackgroundMediaPlayer == null) {
  120.             Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: background media player is null");
  121.         } else {
  122.             // if the music is playing or paused, stop it
  123.             this.mBackgroundMediaPlayer.stop();
  124.  
  125.             this.mBackgroundMediaPlayer.setLooping(isLoop);
  126.  
  127.             try {
  128.                 this.mBackgroundMediaPlayer.prepare();
  129.                 this.mBackgroundMediaPlayer.seekTo(0);
  130.                 this.mBackgroundMediaPlayer.start();
  131.  
  132.                 this.mPaused = false;
  133.             } catch (final Exception e) {
  134.                 Log.e(Cocos2dxMusic.TAG, "playBackgroundMusic: error state");
  135.             }
  136.         }
  137.     }
  138.  
  139.     public void stopBackgroundMusic() {
  140.         if (this.mBackgroundMediaPlayer != null) {
  141.             this.mBackgroundMediaPlayer.stop();
  142.  
  143.             // should set the state, if not, the following sequence will be error
  144.             // play -> pause -> stop -> resume
  145.             this.mPaused = false;
  146.         }
  147.     }
  148.  
  149.     public void pauseBackgroundMusic() {
  150.         if (this.mBackgroundMediaPlayer != null && this.mBackgroundMediaPlayer.isPlaying()) {
  151.             this.mBackgroundMediaPlayer.pause();
  152.             this.mPaused = true;
  153.         }
  154.     }
  155.  
  156.     public void resumeBackgroundMusic() {
  157.         if (this.mBackgroundMediaPlayer != null && this.mPaused) {
  158.             this.mBackgroundMediaPlayer.start();
  159.             this.mPaused = false;
  160.         }
  161.     }
  162.  
  163.     public void rewindBackgroundMusic() {
  164.         if (this.mBackgroundMediaPlayer != null) {
  165.             this.mBackgroundMediaPlayer.stop();
  166.  
  167.             try {
  168.                 this.mBackgroundMediaPlayer.prepare();
  169.                 this.mBackgroundMediaPlayer.seekTo(0);
  170.                 this.mBackgroundMediaPlayer.start();
  171.  
  172.                 this.mPaused = false;
  173.             } catch (final Exception e) {
  174.                 Log.e(Cocos2dxMusic.TAG, "rewindBackgroundMusic: error state");
  175.             }
  176.         }
  177.     }
  178.  
  179.     public boolean isBackgroundMusicPlaying() {
  180.         boolean ret = false;
  181.  
  182.         if (this.mBackgroundMediaPlayer == null) {
  183.             ret = false;
  184.         } else {
  185.             ret = this.mBackgroundMediaPlayer.isPlaying();
  186.         }
  187.  
  188.         return ret;
  189.     }
  190.  
  191.     public void end() {
  192.         if (this.mBackgroundMediaPlayer != null) {
  193.             this.mBackgroundMediaPlayer.release();
  194.         }
  195.  
  196.         this.initData();
  197.     }
  198.  
  199.     public float getBackgroundVolume() {
  200.         if (this.mBackgroundMediaPlayer != null) {
  201.             return (this.mLeftVolume + this.mRightVolume) / 2;
  202.         } else {
  203.             return 0.0f;
  204.         }
  205.     }
  206.  
  207.     public void setBackgroundVolume(float pVolume) {
  208.         if (pVolume < 0.0f) {
  209.             pVolume = 0.0f;
  210.         }
  211.  
  212.         if (pVolume > 1.0f) {
  213.             pVolume = 1.0f;
  214.         }
  215.  
  216.         this.mLeftVolume = this.mRightVolume = pVolume;
  217.         if (this.mBackgroundMediaPlayer != null) {
  218.             this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
  219.         }
  220.     }
  221.  
  222.     private void initData() {
  223.         this.mLeftVolume = 0.5f;
  224.         this.mRightVolume = 0.5f;
  225.         this.mBackgroundMediaPlayer = null;
  226.         this.mPaused = false;
  227.         this.mCurrentPath = null;
  228.     }
  229.  
  230.     /**
  231.      * create mediaplayer for music
  232.      *
  233.      * @param pPath
  234.      *            the pPath relative to assets
  235.      * @return
  236.      */
  237.     private MediaPlayer createMediaplayer(final String pPath) {
  238.         MediaPlayer mediaPlayer = new MediaPlayer();
  239.  
  240.         try {
  241.             if (pPath.startsWith("/")) {
  242.                 final AssetFileDescriptor assetFileDescriptor = zip_resource_file.getAssetFileDescriptor( "assets/" + pPath );
  243.                 final FileInputStream fis = assetFileDescriptor.createInputStream();
  244.                 mediaPlayer.setDataSource(fis.getFD());
  245.                 fis.close();
  246.             } else {
  247.                 final AssetFileDescriptor assetFileDescriptor = zip_resource_file.getAssetFileDescriptor( "assets/" + pPath );
  248.                
  249.                 mediaPlayer = new MediaPlayer();
  250.                 mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
  251.             }
  252.  
  253.             mediaPlayer.prepare();
  254.  
  255.             mediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
  256.         } catch (final IOException e){
  257.             Log.e(Cocos2dxMusic.TAG, "ioerror: " + e.getMessage());
  258.         }
  259.         catch (final Exception e) {
  260.             mediaPlayer = null;
  261.             Log.e(Cocos2dxMusic.TAG, "error: " + e.getMessage(), e);
  262.         }
  263.  
  264.         return mediaPlayer;
  265.     }
  266.  
  267.     // ===========================================================
  268.     // Inner and Anonymous Classes
  269.     // ===========================================================
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement