Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:AudioTrack无法正常工作_Java_Android_Audiotrack - Fatal编程技术网

Java Android:AudioTrack无法正常工作

Java Android:AudioTrack无法正常工作,java,android,audiotrack,Java,Android,Audiotrack,我想用ListView制作一个活动,在单击列表项时播放不同的声音效果。我用MediaPlayer和SoundPool测试了这个功能,两者都很好 我实现了AudioTrack,但当我从ListView中单击一个项目时,它只播放噪音或崩溃,并说“音频缓冲区大小无效” 这是我的密码 public class ATSoundAdapter extends ArrayAdapter { private ArrayList<SoundData> mData; private C

我想用ListView制作一个活动,在单击列表项时播放不同的声音效果。我用MediaPlayer和SoundPool测试了这个功能,两者都很好

我实现了AudioTrack,但当我从ListView中单击一个项目时,它只播放噪音或崩溃,并说“音频缓冲区大小无效”

这是我的密码

public class ATSoundAdapter extends ArrayAdapter {

    private ArrayList<SoundData> mData;
    private Context context;
    private AudioTrack at;


    public ATSoundAdapter(Context context, ArrayList<SoundData> mData) {
        super(context, -1);
        this.context = context;
        this.mData = mData;
    }

    public class ViewHolder {
        private TextView mSoundNameTv;
        private LinearLayout container;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.list_item,
                            parent, false);
            holder = new ViewHolder();
            holder.mSoundNameTv = (TextView) convertView.findViewById(R.id.sound_name_tv);
            holder.container = (LinearLayout) convertView.findViewById(R.id.container);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final SoundData currentSound = mData.get(position);
        if (currentSound != null) {
            holder.mSoundNameTv.setText(currentSound.getSoundName());
            holder.container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    playAudioTrack(currentSound.getSoundResource());
                }
            });
        }
        return convertView;
    }

    private void playAudioTrack(int resource) {
        int i = 0;
        InputStream inputStream = context.getResources().openRawResource(resource);
        long bufferSize = context.getResources().openRawResourceFd(resource).getLength();
        byte[] buffer = new byte[(int)bufferSize];
        try {
            int lengthOfAudioClip = inputStream.read(buffer, 0, buffer.length);
            AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC,
                    44100, AudioFormat.CHANNEL_OUT_STEREO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    lengthOfAudioClip, AudioTrack.MODE_STATIC);
            at.write(buffer, 0, lengthOfAudioClip);
            inputStream.close();
            at.play();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getCount() {
        return mData.size();
    }
}
公共类ATSoundAdapter扩展了ArrayAdapter{
私有ArrayList mData;
私人语境;
私人音轨at;
公共ATSoundAdapter(上下文,ArrayList mData){
super(上下文-1);
this.context=上下文;
this.mData=mData;
}
公共类视图持有者{
私有文本视图mSoundNameTv;
专用线性布局集装箱;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=LayoutInflater.from(上下文)
.充气(右布局列表项),
父母,假);
holder=新的ViewHolder();
holder.mSoundNameTv=(TextView)convertView.findViewById(R.id.sound\u name\u tv);
holder.container=(LinearLayout)convertView.findViewById(R.id.container);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
最终声音数据currentSound=mData.get(位置);
如果(currentSound!=null){
holder.mSoundNameTv.setText(currentSound.getSoundName());
holder.container.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
播放音频曲目(currentSound.getSoundResource());
}
});
}
返回视图;
}
专用void playAudioTrack(内部资源){
int i=0;
InputStream InputStream=context.getResources().openRawResource(资源);
long bufferSize=context.getResources().openrawsourcefd(resource.getLength();
byte[]buffer=新字节[(int)bufferSize];
试一试{
int lengthOfAudioClip=inputStream.read(buffer,0,buffer.length);
AudioTrack at=新的AudioTrack(AudioManager.STREAM_MUSIC,
44100,AudioFormat.CHANNEL\u OUT\u立体声,
AudioFormat.ENCODING_PCM_16位,
长距离音频剪辑、音轨、模式(静态);
at.write(缓冲区,0,长度片段);
inputStream.close();
at.play();
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
public int getCount(){
返回mData.size();
}
}
我认为字节数组的大小是错误的。。。但是当我在logcat中检查它时,它正好是原始文件的大小。 我是android新手,不知道如何使用AudioTrack


提前谢谢

您可以更改代码

AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, lengthOfAudioClip, AudioTrack.MODE_STREAM);    

或者您可以检查此项

那么
MediaPlayer
SoundPool
有什么问题检查这个