高效的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)) ...{
http:

你可能感兴趣的:(java,音乐)