?? playlistrms.java
字號:
package com.wootion.rms;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
/**
* <b>類描述:</b></br>     該類用來對PlayListBean對象進行RMS操作 </br>
* <b>單位:</b></br>     華通科技
*
* @author 劉長雷</br>
* @version 2008-3-12
*
*/
public class PlayListRMS {
private RecordStore rs;
/**
* 構造一個空歌曲信息RMS操作類
*
*/
public PlayListRMS() {
// TODO Auto-generated constructor stub
}
/**
*
* <b>方法描述:</b>打開指定RMS</br>     </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>     </br>
*/
public void closeRS() {
try {
rs.closeRecordStore();
}
catch (Exception e) {
System.out.println("關閉記錄異常");
}
}
/**
*
* <b>方法描述:</b>添加一個播放列表歌曲信息記錄</br>     </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>    
* 保存參數設置</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>     根據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>     </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>     </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>     </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>     </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>     </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>     </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>     </br>
*/
public void deletePlRecordStore(String rsname) {
openRS(rsname);
try {
RecordStore.deleteRecordStore(rs.getName());
}
catch (Exception e) {
System.out.println("清空異常");
}
closeRS();
}
/**
*
* <b>方法描述:</b>歌曲上移的方法,將其信息與序號在前的一首歌進行調換</br>     </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>     </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>     </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 + -