mmsplayer v2 java 之(mmsTrack 音频类)

package com.weiny.mms;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

public class mmsTrack {
    
    int mFrequency;                                        // 采样率
    int mChannel;                                        // 声道
    int mSampBit;                                        // 采样精度
    AudioTrack mAudioTrack;                        
    public mmsTrack(int frequency, int channel, int sampbit){
            mFrequency = frequency;
            //mChannel = channel;
            //mSampBit = sampbit;
            if(channel==1) mChannel=AudioFormat.CHANNEL_CONFIGURATION_MONO;
            else if(channel==2) mChannel=AudioFormat.CHANNEL_CONFIGURATION_STEREO;
            if(sampbit==8) mSampBit=AudioFormat.ENCODING_PCM_8BIT;
            else if(sampbit==16) mSampBit=AudioFormat.ENCODING_PCM_16BIT;
    }
    
    public void init(){
            if (mAudioTrack != null){
                    release();
            }

            int minBufSize = AudioTrack.getMinBufferSize(mFrequency, 
                                                         mChannel,
                                                         mSampBit);

            mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                            mFrequency, 
                            mChannel, 
                            mSampBit, 
                            minBufSize,
                            AudioTrack.MODE_STREAM);
            
            
            
            
            mAudioTrack.play();        
    }
    public void release(){
            if (mAudioTrack != null){
                    mAudioTrack.stop();                                               
                    mAudioTrack.release();
                    mAudioTrack=null;
            }
    }
    
    public int playAudioTrack(byte []data, int offset, int length){
    		int nResult=0;
            if (data == null || data.length == 0){
                nResult=1;   
            	return nResult;
            }
            
            try {
                    mAudioTrack.write(data, offset, length);
            } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                    nResult=1;
            }
            return nResult;
    }
    
    public int getPrimePlaySize(){
            int minBufSize = AudioTrack.getMinBufferSize(mFrequency, 
                            mChannel,
                            mSampBit);
            
            return minBufSize * 2;
    }
}

这个音频类是通用的,大家可以用于其他的方面。之前说过,V2.0的JAVA部分代码会开放,现在我做到了。免得代码费劲去破解我的代码。哈哈。


版权所有,禁止转载

发布:http://blog.csdn.net/weinyzhou/article/details/7632739


posted @ 2012-06-05 00:21  weinyzhou  阅读(181)  评论(0编辑  收藏  举报