高效的Java音乐播放类

/** *//**
     * 自动播放,循环停止后结束。
     * 
     * 
@return
     
*/

    
public boolean AutoPlay() ...{
        _isRun 
= true;
        _thread 
= new Thread(this);
        _thread.start();
        
return _playing;
    }


    
/** *//**
     * 停止播放
     
*/

    
public void stop() ...{

        
if (_audio != null && _audio.isActive()) ...{
            
try ...{
                _audio.stop();
            }
 catch (Exception e) ...{
            }

        }


        
if (_midi != null...{
            _midi.stop();
        }

        _playing 
= false;
        _isRun 
= false;
    }


    
/** *//**
     * 释放数据
     * 
     
*/

    
public void reset() ...{

        stop();

        _loop 
= false;
        _soundType 
= 0;

        
if (_midi != null...{

            _midi.close();

            _midi 
= null;

        }


        
if (_audio != null && _audio.isOpen()) ...{

            _audio.close();

            _audio 
= null;

        }

        _isRun 
= false;
        _thread 
= null;
    }


    
/** *//**
     * 设定MetaMessage
     
*/

    
public void meta(MetaMessage meta) ...{
        
// 判断是否循环播放MIDI
        if (_loop && _soundType == SoundData._L_SOUNDTYPE_MIDI
                
&& meta.getType() == 47...{

            
if (_midi != null && _midi.isOpen()) ...{
                _midi.setMicrosecondPosition(
0);
                _midi.start();

            }

        }


    }


    
public void run() ...{
        
while (_isRun) ...{
            play();
            
// 因为播放类型唯一,所以只会返回一个_playing结果,以此判定。
            if (_midi != null...{
                _playing 
= _midi.isRunning();
            }

            
if (_audio != null...{
                _playing 
= _audio.isRunning();
            }

            
// 当播放停止
            if (!_playing) ...{
                
// 释放
                reset();
            }

            
try ...{
                Thread.sleep(_sleepTime);
            }
 catch (InterruptedException e) ...{
                e.printStackTrace();
            }

        }

    }


    
public int getSleepTime() ...{
        
return _sleepTime;
    }


    
/** *//**
     * 设定AutoPlay线程循环时间。
     * 
     * 
@param time
     
*/

    
public void setSleepTime(int time) ...{
        _sleepTime 
= time;
    }

}


这时我们需要面对的,仅是封装为实体的SoundData数据和SoundPlay操作,而不必和繁复的javax.sound再打交道。

调用方法如下:

package  org.test;

import  org.loon.framework.game.helper.StreamHelper;
import  org.loon.framework.game.net.URI;
import  org.loon.framework.game.sound.SoundData;
import  org.loon.framework.game.sound.SoundPlay;

/** */ /**
 * <p>Title: LoonFramework</p>
 * <p>Description:SoundPlay播放测试</p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: LoonFramework</p>
 * 
@author chenpeng  
 * @email:ceponline@yahoo.com.cn 
 * 
@version 0.1
 
*/

public   class  SoundPlayTest  ... {

    
static void selectPlay(int ftype)...{
        SoundData data
=null;
        
        
switch(ftype)...{
        
//通过loonframework下uri从网络播放音乐
        case 0:
            data
=new SoundData(new URI("http://looframework.sourceforge.net/midi/谁是大英雄.mid"),SoundData._L_SOUNDTYPE_MIDI,false);
            
break;
        
//通过本地资源下音乐文件的byte[]对象播放音乐
        case 1:
            
byte[] bytes=StreamHelper.GetResourceData("/midi/谁是大英雄.mid");
            data
=new SoundData(bytes,SoundData._L_SOUNDTYPE_MIDI,false);
            
break;
            
//通过音乐文件路径播放音乐    
        case 2:
            data
=new SoundData("C:/谁是大英雄.mid",SoundData._L_SOUNDTYPE_MIDI,false);
            
break;
        }

        SoundPlay play
=new SoundPlay();
        
//AutoPlay与Play方法的区别在于,AutoPlay播放完毕会自动停止并释放资源,play需手动中止。
        
//play.play(data);
        play.AutoPlay(data);
    }

    
    
public static void main(String[]args)...{
        selectPlay(
2);
    }

    
}




更详细方法,会待Loonframework-game完全公布后,再进行解释。

另:由于StreamHelper关联其他Loonframework中方法,暂不给出,inputStream转byte[]可用如下写法:
// is为获得的inputStream

   ByteArrayOutputStream byteArrayOutputStream 
=   new  ByteArrayOutputStream();
// 用于承接byte[]
         byte [] arrayByte  =   null ;
        
try   ... {
            
// 每次传输大小为4096
            byte[] bytes = new byte[4096];
            bytes 
= new byte[is.available()];
            
int read;
            
while ((read = is.read(bytes)) >= 0...{
                byteArrayOutputStream.write(bytes, 
0, read);
            }

            arrayByte 
= byteArrayOutputStream.toByteArray();
        }
  catch  (IOException e)  ... {
            
return null;
        }
  finally   ... {
            
try ...{
                
if (byteArrayOutputStream != null...{
                    byteArrayOutputStream.close();
                    byteArrayOutputStream 
= null;
                }

                
if (is != null...{
                    is.close();
                    is 
= null;
                }


            }
 catch (IOException e) ...{
            }

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值