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

📄 audio.java

📁 手机流媒体的实现
💻 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 + -