public class FileUtils {

private static MediaPlayer mediaPlayer;

public static void stopMediaPlayer(){
    if (mediaPlayer!=null) {
        mediaPlayer.stop();
        mediaPlayer.pause();
        mediaPlayer.release();
        mediaPlayer = null;
    }
}
/**
 * 打开assets下的音乐mp3文件
 */
public static void playAssetVoice(Context context,String tipMediaName) {
    try {
        stopMediaPlayer();
        //播放 assets/a2.mp3 音乐文件
        AssetFileDescriptor fd = context.getAssets().openFd(tipMediaName);
        if (mediaPlayer==null)
            mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

 

posted on 2020-10-15 20:33  灯火、阑珊处  阅读(148)  评论(0编辑  收藏  举报