package com.example.client;

import android.R.string;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

public class BgMusic {

    private static BgMusic _instance = null;

    private BgMusic() {}
    
    public static BgMusic getInstance() {
        if (null == _instance) {
            _instance = new BgMusic();
        }
        
        return _instance;
    }
    
    //////////////////////////////////////////////////////////////////////////
    private MyMediaplayer mBgPlayer;
    private boolean mIsFirst = true;
    private int mMusicIndex;
    
    public void start(String musicpath) {
        mBgPlayer = new MyMediaplayer(musicpath);
        mBgPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // 继续播放当前音乐
                mp.start();        
            }
         }); 
        
        mMusicIndex = 0;
        mIsFirst = false;
    }
    
    public boolean isFirst() {
        return mIsFirst;
    }
    
    
    public void togglePlay() {
        mBgPlayer.togglePlay();
    }
    
    
    public void stop() {
        mBgPlayer.playStop();
    }
    
    
    public void pause() {
        mBgPlayer.pause();
    }
    
    
    public boolean isPlaying() {
        return mBgPlayer.isPlaying();
    }
    
    
    public int getMusicIndex() {
        return mMusicIndex;
    }
    
    
    public void changeMusicEnv(String musicpath, int musicIndex)
    {
        mBgPlayer.release();
        mBgPlayer = new MyMediaplayer(musicpath);
        mBgPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // 继续播放当前环境音乐
                mp.start();
            }
         });
        
        mMusicIndex = musicIndex;
    }
}

