高效的Java音乐播放类 2

/** *//**
     * 直接播放url文件
     * 
     * 
@param uri
     * 
@param ftype
     * 
@param loop
     * 
@return
     
*/

    
public boolean load(URI uri, int ftype, boolean loop) ...{

        
// 刷新数据
        reset();
        
if (uri == null...{
            
return false;
        }

        
// 获得SoundData
        SoundData data = new SoundData(uri, ftype, loop);
        
if (data == null || data.getData() == null...{
            
return false;
        }

        
return init(data.getData(), data.getType(), data.getLoop());

    }


    
/** *//**
     * 初始化sound相关数据
     * 
     * 
@param data
     * 
@param ftype
     * 
@param loop
     * 
@return
     
*/

    
private boolean init(byte[] data, int ftype, boolean loop) ...{
        
boolean result = false;

        ByteArrayInputStream bis 
= null;

        
try ...{
            bis 
= new ByteArrayInputStream(data);
        }
 catch (Exception e) ...{
            bis 
= null;
        }


        
if (bis == null...{
            
return false;
        }


        
// 判断类型
        switch (ftype) ...{

        
// MIDI
        case SoundData._L_SOUNDTYPE_MIDI:

            
// 当MIDI不存在时
            if (_midi == null...{

                
try ...{
                    
// 获得Sequencer
                    _midi = MidiSystem.getSequencer();
                    _midi.open();

                }
 catch (Exception ex) ...{
                    _midi 
= null;
                }


                
if (_midi != null...{
                    _midi.addMetaEventListener(
this);
                }


            }


            
// 当MIDI依旧未获得时
            if (_midi != null...{
                
// 重新创建Sequence
                Sequence sc = null;

                
try ...{
                    sc 
= MidiSystem.getSequence(bis);
                }
 catch (Exception e) ...{
                    sc 
= null;
                }


                
if (sc != null...{

                    
try ...{

                        _midi.setSequence(sc);

                        
// 获得是否循环播放
                        _loop = loop;

                        
// 获得是否载入
                        result = true;

                    }
 catch (Exception ee) ...{
                    }


                    
// 获得声音类型
                    _soundType = SoundData._L_SOUNDTYPE_MIDI;

                }


            }


            
try ...{
                bis.close();
            }
 catch (Exception ee) ...{
            }


            
break;

        
// Wav
        case SoundData._L_SOUNDTYPE_WAV:

            AudioFileFormat type 
= null;

            
// 获得Audio
            try ...{
                type 
= AudioSystem.getAudioFileFormat(bis);
            }
 catch (Exception e) ...{
                type 
= null;
            }


            
// 关闭流
            try ...{
                bis.close();
            }
 catch (Exception ex) ...{
            }


            
if (type == null...{
                
return false;
            }


            
// 根据指定信息构造数据行的信息对象
            DataLine.Info di = new DataLine.Info(Clip.class, type.getFormat());

            
// 转为Clip
            try ...{
                _audio 
= (Clip) AudioSystem.getLine(di);
            }
 catch (Exception e) ...{
            }


            
// 播放文件
            try ...{

                _audio.open(type.getFormat(), data, 
0, data.length);

                _loop 
= loop;

                result 
= true;

            }
 catch (Exception e) ...{
            }


            
// 获得文件类型
            _soundType = SoundData._L_SOUNDTYPE_WAV;

            
break;

        }


        
return result;
    }


    
public boolean play(SoundData data) ...{

        
if (!load(data)) ...{
            
return false;
        }


        
return play();

    }


    
public boolean play() ...{

        
switch (_soundType) ...{

        
case SoundData._L_SOUNDTYPE_MIDI:

            
try ...{

                _midi.start();

                _playing 
= true;

                _soundType 
= SoundData._L_SOUNDTYPE_MIDI;

            }
 catch (Exception ee) ...{
            }


            
break;

        
case SoundData._L_SOUNDTYPE_WAV:

            
if (_audio != null...{

                
if (_loop) ...{

                    
// 设定循环
                    _audio.setLoopPoints(0-1);
                    _audio.setFramePosition(
0);

                    _audio.loop(Clip.LOOP_CONTINUOUSLY);

                }
 else ...{

                    
// 强制设定播放位置至0
                    _audio.setFramePosition(0);

                    _audio.start();

                }


                _playing 
= true;

            }


            
break;

        }


        
return _playing;

    }


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

    
public boolean AutoPlay(SoundData data) ...{
        
if (!load(data)) ...{
html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值