?? threadplaymain.java
字號:
/*///////////////////////////////////////////////////////////////////////////////
//文檔生成日期:2006.3.28
//
//(1)概述:
//類名稱:ThreadPlayMain
//類說明:
// 發送httpConnection請求給我的服務器網頁,這個網頁會告訴我他看到的IP地址是什么
//所在子系統:StreamingDemo
//
//系統總描述:
用兩個Player交替播放從網絡上下載的流媒體。
上面的代碼可以從
http://www.cnblogs.com/Files/zhengyun_ustc/StreamingDemo.rar
下載;
安裝的jar包從
http://www.cnblogs.com/Files/zhengyun_ustc/StreamingDemo-deployed.rar下載。
本文屬于討論稿,提供的僅僅是建議和測試意見。
本文還可以從
http://www.cnblogs.com/zhengyun_ustc/archive/2006/3/28/StreamingDemo.html 得到最新稿。
//(2)歷史記錄:
//創建人: 鄭昀(2006.3.28)
//修改歷史:
//聯系我: Google Talk >> zhengyun@gmail.com
//Blogs: http://blog.csdn.net/zhengyun_ustc/以及http://www.cnblogs.com/zhengyun_ustc
//(3)版權聲明:
//我這個版本j2me客戶端代碼僅僅允許您借鑒,但不得用于商業用途,除非得到鄭昀本人的授權。本人保留所有權利。
////////////////////////////////////////////////////////////////////*/
package com.ultrapower.model;
import java.io.InputStream;
import javax.microedition.media.MediaException;
import com.ultrapower.common.CommandResources;
import com.ultrapower.control.GUIController;
import com.ultrapower.view.FormPlayer;
/**********************************************************
//ThreadPlayMain
//
//Class Description:
//
//
//Author:
// zhengyun@ultrapower 2006.3.28
//
**********************************************************/
public class ThreadPlayMain
implements Runnable {
// 利用這個標志控制何時退出
private boolean m_bRunAnyway;
// 依靠這個標志判斷是否需要去獲取遠程音樂
public boolean m_bGetMusic = false;
// 依靠這個標志判斷是否需要去預加載音樂資源
private boolean m_bPrefetchIt = false;
// 依靠這個標志判斷是否需要去播放音樂資源
private boolean m_bPlayIt = false;
private GUIController m_controller;
private FormPlayer m_form;
private int m_nSequenceMIN;
private int m_nSequenceMAX;
// 代表當前的播放器正在播放音樂,不允許當前線程再作其他的操作了
private boolean m_bPlaying = false;
// 代表播放器的數組序列號,0為第一個播放器
private int m_Sequence;
// 紀錄當前正在下載哪一個音樂資源序號
private int m_nCurrentMusicIndex;
// 這是從網絡上獲取音樂數據的流
private InputStream m_isInputMusic;
/*
* MainOrSecondary指的是本線程代表哪一個播放器;共有兩個播放器,一個Main,一個Secondary;
* 前者用0表示,后者是1;
*/
public ThreadPlayMain(GUIController control, FormPlayer form,
int MainOrSecondary,
int SequenceMIN,
int SequenceMAX)
{
System.out.println(MainOrSecondary +
">>>Enter ThreadPlayMain Constructor!");
m_controller = control;
m_form = form;
m_bRunAnyway = true;
m_Sequence = MainOrSecondary;
m_nSequenceMIN = SequenceMIN;
m_nSequenceMAX = SequenceMAX;
m_nCurrentMusicIndex = m_nSequenceMIN;
}
/*
* 允許外界設置當前是否在播放
*/
public void setPlaying(boolean bPlaying)
{
m_bPlaying = bPlaying;
}
/*
* 通知當前播放器停止播放音樂
*/
public synchronized void notifyStop() throws MediaException
{
m_bPlayIt = false;
m_bPrefetchIt = false;
// 停止音樂播放
Audio.stopSound(m_Sequence);
}
/*
* 通知下載音樂片段
*/
public synchronized void notifyGetMusic()
{
m_bGetMusic = true;
}
/*
* 通知線程退出
*/
public synchronized void notifyCloseThread()
{
m_bPlayIt = false;
m_bPrefetchIt = false;
m_bRunAnyway = false;
}
/*
* 當前的播放器準備數據
*/
public synchronized void notifyPrefetch()
{
m_bPrefetchIt = true;
}
/*
* 當前的播放器應該數據已經準備好了,那么就播放他
*/
public synchronized void notifyPlay()
{
m_bPlayIt = true;
}
public final void run()
{
/* Use networking if necessary */
long lngStart;
long lngTimeTaken;
try
{
while(m_bRunAnyway)
{
if(m_bGetMusic && !m_bPlaying)
{
System.out.println(m_Sequence +
">>>ready to get remote music.m_nCurrentMusicIndex="
+ m_nCurrentMusicIndex);
m_isInputMusic =
CommandResources.getMusicResource(m_nCurrentMusicIndex);
m_nCurrentMusicIndex =
(m_nCurrentMusicIndex<m_nSequenceMAX)?
(m_nCurrentMusicIndex+2):m_nSequenceMAX;
// 下載過了就遞增2
// 下載完畢就不再下載了,等待下次通知
m_bGetMusic = false;
System.out.println(m_Sequence +
">>>download completed!");
if(m_Sequence == 0)
{
m_controller.handleEvent(GUIController.EventID.EVENT_MAIN_DownloadCompleted,
null);
}
}
if( m_bPrefetchIt && !m_bPlaying)
{
System.out.println(m_Sequence +
">>>Start prefetch!");
String strError = "";
boolean bHasException = false;
try
{
Audio.prefetchSound(m_form,
m_isInputMusic,
m_Sequence);
}
catch(Exception exc)
{
bHasException = true;
exc.printStackTrace();
strError =
"Exception:"
+ exc.getMessage() +
"/" + exc.getClass();
}
if(bHasException == true)
{
////////////////////////////////////////////
// 告訴控制器出錯了
Object[] argvErrors = {strError};
m_controller.handleEvent(GUIController.EventID.EVENT_FORM_ERROR,
argvErrors);
////////////////////////////////////////////
}
m_bPrefetchIt = false;
}
if( m_bPlayIt && !m_bPlaying)
{
System.out.println(m_Sequence +
">>>Start play!");
String strError = "";
boolean bHasException = false;
try
{
m_bPlaying = true;
Audio.playSound(m_Sequence);
}
catch(Exception exc)
{
bHasException = true;
exc.printStackTrace();
strError =
"Exception:"
+ exc.getMessage() +
"/" + exc.getClass();
}
if(bHasException == true)
{
////////////////////////////////////////////
// 告訴控制器出錯了
Object[] argvErrors = {strError};
m_controller.handleEvent(GUIController.EventID.EVENT_FORM_ERROR,
argvErrors);
////////////////////////////////////////////
}
m_bPlayIt = false;
}
lngStart = System.currentTimeMillis();
lngTimeTaken = System.currentTimeMillis() - lngStart;
if(lngTimeTaken < 100)
Thread.sleep(75 - lngTimeTaken);
}
}
catch(Exception exc)
{
System.out.println(m_Sequence +
">>>Eception in downloading!");
m_bRunAnyway = false;
exc.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -