package com.example.client;

import java.io.File;
import java.io.IOException;


import android.media.MediaPlayer;
import android.os.Environment;
import android.util.Log;

public class MyMediaplayer extends MediaPlayer{

    public MyMediaplayer(String path) {
        super();
        Log.i("MYmusicPlayer","Ini");
        
        try {
            this.setDataSource(this.getSDPath() + path);
            this.prepare();
            this.start();
        } catch (IOException e) {
            // TODO: handle exception
        }
    
        // TODO Auto-generated constructor stub
    }
    
    public boolean togglePlay() {
        if (this.isPlaying()) {
            this.pause();
        } else {
            this.start();
        }
        return true;
    }
    
    public boolean playStop() {
        this.stop();
        try {
            this.prepare();
        } catch (IOException e) {
            // TODO: handle exception
        }
        return true;
    }
    
    public boolean destroy() {
        this.release();
        return true;
    }
    
    private String getSDPath() { 
        File sdDir = null; 
        
        // 判断sd卡是否存在 
        boolean sdCardExist = Environment.getExternalStorageState()   
                            .equals(android.os.Environment.MEDIA_MOUNTED);   
        if (sdCardExist) {                         
            // 获取根目录 
            sdDir = Environment.getExternalStorageDirectory();
        }   
        return sdDir.toString();      
    }
}

