⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playlistrms.java

📁 J2ME音乐播放器 适合初学者参考
💻 JAVA
字号:
package com.wootion.rms;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

/**
 * <b>类描述:</b></br>&nbsp&nbsp&nbsp&nbsp 该类用来对PlayListBean对象进行RMS操作 </br>
 * <b>单位:</b></br>&nbsp&nbsp&nbsp&nbsp 华通科技
 * 
 * @author 刘长雷</br>
 * @version 2008-3-12
 * 
 */
public class PlayListRMS {
	private RecordStore rs;

	/**
	 * 构造一个空歌曲信息RMS操作类
	 * 
	 */
	public PlayListRMS() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * <b>方法描述:</b>打开指定RMS</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param rsname
	 *            RMS名称
	 */
	public void openRS(String rsname) {
		try {
			rs = RecordStore.openRecordStore(rsname, true);
		}
		catch (Exception e) {
			System.out.println("打开记录异常");
		}
	}

	/**
	 * 
	 * <b>方法描述:</b>关闭RMS</br>&nbsp&nbsp&nbsp&nbsp </br>
	 */
	public void closeRS() {
		try {
			rs.closeRecordStore();
		}
		catch (Exception e) {
			System.out.println("关闭记录异常");
		}
	}

	/**
	 * 
	 * <b>方法描述:</b>添加一个播放列表歌曲信息记录</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param musicName
	 *            歌曲名
	 * @param musicUrl
	 *            歌曲存放地址
	 * @return 添加歌曲是否成功标志位,返回true:成功,返回false:失败。
	 */
	public boolean addRecord(String rsname, String musicName, String musicUrl) {
		boolean success = false;
		openRS(rsname);
		try {
			int index = rs.getNumRecords() + 1;
			PlayListBean plb = new PlayListBean(index, musicName, musicUrl);
			byte[] data = plb.toBytes();
			rs.addRecord(data, 0, data.length);
			success = true;
			// System.out.println("添加记录成功");
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println("添加记录异常");
		}
		closeRS();
		return success;
	}
/**
 * 
 * <b>方法描述:</b></br>&nbsp&nbsp&nbsp&nbsp
 * 保存参数设置</br>
 * @param rsname
 * @param index
 * @param value
 */
	public void modParameter(String rsname, int index, int value){
		openRS(rsname);
		int id = getPRecordId(rsname, index);
		try {
			rs.enumerateRecords(null, null, false);
			ParameterBean pb = new ParameterBean(index,value);
			byte[] data = pb.toBytes();
			if (id != -1) {
				rs.setRecord(id, data, 0, data.length); 
			}
			else {
				rs.addRecord(data, 0, data.length);
			}
		}
		catch (Exception e) {
			System.out.println("修改记录异常");
			e.printStackTrace();
		}
		closeRS();
	}
	public int getPRecordValue(String rsname, int index) {
		openRS(rsname);
		int value = -1;
		RecordEnumeration re = null;
		try {
			re = rs.enumerateRecords(null, null, false); // enumeration
			int sum = getNumOfRecords();
			for (int i = 0; i < sum; i++) {
				int j = re.nextRecordId();
				ParameterBean pb = new ParameterBean(rs.getRecord(j));
				if (pb.getIndex() == index) {
					value = pb.getValue();
				}
			}
		}
		catch (Exception e) {
			System.out.println("得到P记录号异常");
		}
		return value;

	}
	/**
	 * 
	 * <b>方法描述:</b></br>&nbsp&nbsp&nbsp&nbsp 根据index取得记录集编号</br>
	 * 
	 * @param rsname
	 *            记录集名称
	 * @param index
	 *            参数的下标
	 * @return id
	 */
	public int getPRecordId(String rsname, int index) {
		int id = -1;
		RecordEnumeration re = null;
		try {

			re = rs.enumerateRecords(null, null, false); // enumeration
			int sum = getNumOfRecords();
			for (int i = 0; i < sum; i++) {
				int j = re.nextRecordId();
				ParameterBean pb = new ParameterBean(rs.getRecord(j));
				if (pb.getIndex() == index) {
					id = j;
				}
			}
		}
		catch (Exception e) {
			System.out.println("得到P记录号异常");
		}
		return id;

	}


	/**
	 * 
	 * <b>方法描述:</b>获取RMS中的记录条数</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @return 记录条数
	 */
	public int getNumOfRecords() {// 得到RMS中记录的条数
		try {

			int x = rs.getNumRecords();
			return x;
		}
		catch (Exception e) {
			System.out.println("获取记录总数异常");
			return 0;

		}
	}

	/**
	 * 
	 * <b>方法描述:</b>返回RMS中的所有歌曲</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @return 播放列表RMS中的所有歌曲
	 */
	public PlayListBean[] getRecords(String rsname) {// 取得RMS中的所有记录
		openRS(rsname);
		PlayListBean[] result = {};

		try {
			rs.enumerateRecords(null, null, false);
			result = new PlayListBean[rs.getNumRecords()];
			for (int i = 0; i < result.length; i++) {
				PlayListBean plb = new PlayListBean(rs.getRecord(getId(i + 1)));

				result[i] = plb;
			}
		}
		catch (Exception e) {
			System.out.println("取得所有记录异常");
		}
		closeRS();
		return result;
	}

	/**
	 * 
	 * <b>方法描述:</b>根据记录编号(参数 int j)取得一条记录</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param j
	 *            记录编号
	 * @return 播放歌曲信息对象
	 */
	public PlayListBean getRecord(int j) {// 根据记录编号(参数 int j)取得一条记录
		PlayListBean result = new PlayListBean();
		try {
			rs.enumerateRecords(null, null, false);
			result = new PlayListBean(rs.getRecord(j));
		}
		catch (Exception e) {
			System.out.println("根据编号取记录异常");
		}
		return result;
	}

	/**
	 * 
	 * <b>方法描述:</b>根据歌曲播放序号获取其在RMS中的存放ID</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param index
	 *            歌曲播放序号
	 * @return 歌曲在RMS中的记录号
	 */
	public int getId(int index) {// 得到记录号int j

		RecordEnumeration re = null;

		int x = 1;
		try {

			re = rs.enumerateRecords(null, null, false); // enumeration
			int sum = getNumOfRecords();
			for (int i = 0; i < sum; i++) {
				int j = re.nextRecordId();
				PlayListBean plb = new PlayListBean(rs.getRecord(j));
				if (plb.getIndex() == index) {
					x = j;
				}

			}

		}
		catch (Exception e) {
			System.out.println("得到记录号异常");
		}

		return x;
	}

	/**
	 * 
	 * <b>方法描述:</b>修改RMS中歌曲信息</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param id
	 *            该歌曲在RMS中的存放ID
	 * @param index
	 *            播放顺序序号
	 * @param musicName
	 *            歌曲名称
	 * @param musicUrl
	 *            歌曲存放路径
	 * @return 修改是否成功,true:成功,false:失败.
	 */

	public boolean setRecord(int id, int index, String musicName,
			String musicUrl) {
		boolean success = false;
		try {

			rs.enumerateRecords(null, null, false);
			PlayListBean plb = new PlayListBean(index, musicName, musicUrl);
			byte[] data = plb.toBytes();
			rs.setRecord(id, data, 0, data.length);
			success = true;
		}
		catch (Exception e) {
			System.out.println("修改记录异常");
			e.printStackTrace();
		}
		return success;
	}

	/**
	 * 
	 * <b>方法描述:</b>删除播放歌曲信息的方法</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param id
	 *            歌曲在列表中存放的ID
	 */
	public void deleteRecord(int id) {

		try {
			rs.deleteRecord(id);
		}
		catch (Exception e) {
			System.out.println("删除记录异常");
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * <b>方法描述:</b>清空该RecordStore</br>&nbsp&nbsp&nbsp&nbsp </br>
	 */
	public void deletePlRecordStore(String rsname) {
		openRS(rsname);
		try {
			RecordStore.deleteRecordStore(rs.getName());
		}
		catch (Exception e) {
			System.out.println("清空异常");
		}
		closeRS();
	}

	/**
	 * 
	 * <b>方法描述:</b>歌曲上移的方法,将其信息与序号在前的一首歌进行调换</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param index
	 *            歌曲播放序号
	 */
	public void moveUp(String rsname, int index) {
		openRS(rsname);
		int SourceRecordId = 0;
		int ObjectRecordId = 0;
		PlayListBean sourceplb = new PlayListBean();
		PlayListBean objectplb = new PlayListBean();
		SourceRecordId = getId(index);
		ObjectRecordId = getId(index - 1);
		sourceplb = getRecord(SourceRecordId);
		objectplb = getRecord(ObjectRecordId);
		setRecord(SourceRecordId, sourceplb.getIndex(), objectplb
				.getMusicName(), objectplb.getMusicUrl());
		setRecord(ObjectRecordId, objectplb.getIndex(), sourceplb
				.getMusicName(), sourceplb.getMusicUrl());
		closeRS();
	}

	/**
	 * 
	 * <b>方法描述:</b>歌曲下移的方法,将其信息与序号在后的一首歌进行调换</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param index
	 *            歌曲播放序号
	 */
	public void moveDown(String rsname, int index) {
		openRS(rsname);
		int SourceRecordId = 0;
		int ObjectRecordId = 0;
		PlayListBean sourceplb = new PlayListBean();
		PlayListBean objectplb = new PlayListBean();
		SourceRecordId = getId(index);
		ObjectRecordId = getId(index + 1);
		sourceplb = getRecord(SourceRecordId);
		objectplb = getRecord(ObjectRecordId);
		setRecord(SourceRecordId, sourceplb.getIndex(), objectplb
				.getMusicName(), objectplb.getMusicUrl());
		setRecord(ObjectRecordId, objectplb.getIndex(), sourceplb
				.getMusicName(), sourceplb.getMusicUrl());
		closeRS();
	}

	/**
	 * 
	 * <b>方法描述:</b>删除歌曲</br>&nbsp&nbsp&nbsp&nbsp </br>
	 * 
	 * @param index
	 *            歌曲序号
	 */
	public void deleteSong(String rsname, int index) {
		openRS(rsname);
		int SourceRecordId = getId(index);
		deleteRecord(SourceRecordId);
		int sum = getNumOfRecords();
		for (int i = index; i <= sum; i++) {
			int sRecordId = getId(i + 1);
			PlayListBean plb = getRecord(sRecordId);
			setRecord(sRecordId, i, plb.getMusicName(), plb.getMusicUrl());
		}
		closeRS();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -