?? audio.java
字號:
/*///////////////////////////////////////////////////////////////////////////////
//文檔生成日期:2006.3.28
//
//(1)概述:
//類名稱:Audio
//類說明:
// Audio manager. Contains one player for each sound.
//所在子系統: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.IOException;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;
import com.ultrapower.view.FormPlayer;
/**
* <p>
* Audio manager. Contains one player for each sound.
* </p><p>
* Implementation note: the format of the sound is hardcoded
* in method <code>playSound</code>.
* </p>
*
* @author zhengyun_ustc
*/
public class Audio
{
/** The media players */
protected static Player[] m_sounds = new Player[2];
/** Current playing player index */
protected static int m_currentPlayer = -1;
/** Current prefetching player index */
protected static int m_currentPrefetcher = -1;
protected static final String TYPE_AMR = "audio/amr";
/**
* 第一個參數是要播放的媒體資源,
* 第二個參數是要使用的播放器的序號,是第一個呢還是第二個player,
* 如果當前這個播放器還沒有創建出來,就新建一個,并直接對媒體資源進行預運算;
* 如果已經有了的話,那么就直接對媒體進行預運算。
*/
public synchronized static void prefetchSound(FormPlayer formPlayer
,InputStream input,
int sequence)
{
System.out.println(sequence +
">>+==++Enter Audio::prefetchSound");
// Start sound
try
{
// No player, create one
if (m_sounds[sequence] != null)
{
m_sounds[sequence].stop();
m_sounds[sequence].close();
}
String type = TYPE_AMR;
if(input != null)
{
System.out.println(sequence +
">>+==++Audio::prefetchSound createPlayer Begin:" + type);
m_sounds[sequence] =
Manager.createPlayer(input, type);
}
}
catch (MediaException e)
{
System.out.println(sequence +
">>prefetchSound>>createPlayer MediaException!");
e.printStackTrace();
return;
}
catch (IOException e)
{
System.out.println(sequence +
">>prefetchSound>>createPlayer IOException!");
e.printStackTrace();
return;
}
// Start player
Player player = m_sounds[sequence];
if (player != null)
{
try
{
System.out.println(sequence +
">>+==++Audio::prefetchSound realize Begin!");
player.realize();
// 添加播放器監聽事件
player.addPlayerListener(formPlayer);
// 設置音量
VolumeControl volume = (VolumeControl)
player.getControl("VolumeControl");
if (volume != null)
volume.setLevel(35);// 小點聲!!
// 預運算
System.out.println(sequence +
">>+==++Audio::prefetchSound prefetch Begin!");
player.prefetch();
m_currentPrefetcher = sequence;
System.out.println(sequence +
">>+==++Audio::prefetchSound prefetch End!");
}
catch (MediaException e)
{
System.out.println(sequence +
">>prefetchSound>>prefetch IOException!");
e.printStackTrace();
}
}
}
/**
* 第一個參數是要使用的播放器的序號,是第一個呢還是第二個player,
* 直接調用該播放器的start方法來播放他已經運算好的媒體資源。
*/
public synchronized static void playSound(int sequence)
{
System.out.println(sequence +
">>+==++Audio::playSound Begin!");
// Start sound
{
// Start player
Player player = m_sounds[sequence];
if (player != null)
{
try
{
player.start();
m_currentPlayer = sequence;
}
catch (MediaException e)
{
e.printStackTrace();
}
}
}
System.out.println(sequence +
">>+==++Audio::playSound End!");
}
/**
* Stops specified sound if it is playing.
* @param snd The id of the sound to stop.
*/
public synchronized static void stopSound(int snd)
{
if (m_sounds[snd] != null)
{
try
{
m_sounds[snd].stop();
}
catch (MediaException e)
{
e.printStackTrace();
}
}
}
/**
* Stops all sounds and cleans up resources.
*/
public static void shutdown()
{
for (int i = 0; i < m_sounds.length; i++)
{
stopSound(i);
if (m_sounds[i] != null)
{
m_sounds[i].deallocate();
}
}
}
/** Prevent instantiation */
private Audio() {}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -