Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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 在soundboard应用程序的片段/选项卡中使用soundpool_Java_Android - Fatal编程技术网

Java 在soundboard应用程序的片段/选项卡中使用soundpool

Java 在soundboard应用程序的片段/选项卡中使用soundpool,java,android,Java,Android,我正在尝试制作一个soundboard应用程序,我想在其中一个片段/选项卡中使用soundpool,我被困在getSystemService for audiomanager上。即使apk构建成功,该应用程序也无法工作。这是我的片段类代码。提前谢谢 public class FragmentQuiz extends android.support.v4.app.Fragment { ListView listView; int[] soundArray; private

我正在尝试制作一个soundboard应用程序,我想在其中一个片段/选项卡中使用soundpool,我被困在getSystemService for audiomanager上。即使apk构建成功,该应用程序也无法工作。这是我的片段类代码。提前谢谢

public class FragmentQuiz extends android.support.v4.app.Fragment {

    ListView listView;
    int[] soundArray;
    private SoundPool soundPool;
    private int soundID;
    boolean loaded  = false;
    private int MAX=1;
    View view;

    Context context=getContext();


    public FragmentQuiz() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.quiz_fragment, container, false);

       soundArray = new int[5];
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        soundPool = new SoundPool(MAX, AudioManager.STREAM_MUSIC,0);
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener(){
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

                loaded = true;
            }

        });

        soundArray[0] = soundPool.load(getActivity().getApplicationContext(), R.raw.bird,1);
        soundArray[1] = soundPool.load(getActivity().getApplicationContext(),R.raw.rooster,1);
        soundArray[2] = soundPool.load(getActivity().getApplicationContext(),R.raw.elephant,1);


        this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                loaded = true;
            }
        });


        AudioManager audiomanager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float actualVolume = (float) audiomanager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxvolume =(float) audiomanager
                .getStreamMaxVolume(audiomanager.STREAM_MUSIC);
        final float volume = actualVolume / maxvolume;



       listView = (ListView) getView().findViewById(R.id.list);
       String[] values = new String[]{
               "Android Lollipop", "beta","marshmello"
       };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext().getApplicationContext(),android.R.layout.simple_list_item_1,android.R.id.text1,values);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int itemPosition = position;
                String itemValue = (String) listView.getItemAtPosition(position);
                Toast.makeText(getActivity().getApplicationContext(), "position: "+itemPosition + "ListItem: "+itemValue, Toast.LENGTH_SHORT).show();

                soundPool.play(soundArray[position], volume, volume , 1,0,1f);
            }
        });




        return view;
    }

    private void setVolumeControlStream(int streamMusic) {
    }


}
公共类碎片测验扩展了android.support.v4.app.Fragment{
列表视图列表视图;
int[]声音阵列;
私人声池声池;
私有内部声音ID;
布尔加载=假;
私有int MAX=1;
视图;
Context=getContext();
公共碎片测验(){
}
@可空
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、Bundle savedInstanceState){
视图=充气机。充气(R.layout.quick_碎片,容器,假);
soundArray=新整数[5];
这个.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool=新的soundPool(最大值,AudioManager.STREAM_MUSIC,0);
soundPool.setOnLoadCompleteListener(新的soundPool.OnLoadCompleteListener(){
@凌驾
public void onLoadComplete(声音池声音池,int-sampleId,int-status){
加载=真;
}
});
soundArray[0]=soundPool.load(getActivity().getApplicationContext(),R.raw.bird,1);
soundArray[1]=soundPool.load(getActivity().getApplicationContext(),R.raw.rooster,1);
soundArray[2]=soundPool.load(getActivity().getApplicationContext(),R.raw.elephant,1);
this.soundPool.setOnLoadCompleteListener(新的soundPool.OnLoadCompleteListener(){
@凌驾
public void onLoadComplete(声音池声音池,int-sampleId,int-status){
加载=真;
}
});
AudioManager AudioManager=(AudioManager)getSystemService(音频服务);
浮动实际音量=(浮动)音频管理器
.getStreamVolume(AudioManager.STREAM_MUSIC);
浮点最大音量=(浮点)音频管理器
.getStreamMaxVolume(audiomanager.STREAM_MUSIC);
最终浮动体积=实际体积/最大体积;
listView=(listView)getView().findViewById(R.id.list);
字符串[]值=新字符串[]{
“安卓棒棒糖”、“贝塔”、“马什梅洛”
};
ArrayAdapter=新的ArrayAdapter(getContext().getApplicationContext(),android.R.layout.simple_list_item_1,android.R.id.text1,值);
setAdapter(适配器);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
int itemPosition=位置;
String itemValue=(String)listView.getItemAtPosition(position);
Toast.makeText(getActivity().getApplicationContext(),“position:+itemPosition+”ListItem:+itemValue,Toast.LENGTH\u SHORT).show();
声音池播放(声音阵列[位置],音量,音量,1,0,1f);
}
});
返回视图;
}
私有void setVolumeControlStream(int streamMusic){
}
}

Context=getContext()是错误的。这意味着,在片段实例化之前,您将无法访问上下文。您应该从onCreateView(…)方法调用它。您甚至可以直接在该方法中使用getContext(),而不是实例化变量

Context context;
public FragmentQuiz() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
    container, Bundle savedInstanceState) {
    context = getContext();
}
而且,由于您在一个片段中工作,所以不能仅此而已

AudioManager audiomanager = (AudioManager) getSystemService(AUDIO_SERVICE);
这是因为片段不是上下文的子类(如活动的方式,因此您无权访问getSystemService(…)方法。相反,您应该执行以下操作:

AudioManager audiomanager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
这是您的初始代码,包括所有这些更改。我还没有测试过它,但它应该可以工作

public FragmentQuiz() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, Bundle savedInstanceState) {

view = inflater.inflate(R.layout.quiz_fragment, container, false);

soundArray = new int[5];
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(MAX, AudioManager.STREAM_MUSIC,0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener(){
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

        loaded = true;
    }

});

soundArray[0] = soundPool.load(getActivity().getApplicationContext(), R.raw.bird,1);
soundArray[1] = soundPool.load(getActivity().getApplicationContext(),R.raw.rooster,1);
soundArray[2] = soundPool.load(getActivity().getApplicationContext(),R.raw.elephant,1);


this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        loaded = true;
    }
});


AudioManager audiomanager = (AudioManager) 
getContext().getSystemService(Context.AUDIO_SERVICE);
float actualVolume = (float) audiomanager
        .getStreamVolume(AudioManager.STREAM_MUSIC);
float maxvolume =(float) audiomanager
        .getStreamMaxVolume(audiomanager.STREAM_MUSIC);
final float volume = actualVolume / maxvolume;



listView = (ListView) getView().findViewById(R.id.list);
String[] values = new String[]{
       "Android Lollipop", "beta","marshmello"
};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext().getApplicationContext(),android.R.layout.simple_list_item_1,android.R.id.text1,values);

listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int itemPosition = position;
        String itemValue = (String) listView.getItemAtPosition(position);
        Toast.makeText(getActivity().getApplicationContext(), "position: "+itemPosition + "ListItem: "+itemValue, Toast.LENGTH_SHORT).show();

        soundPool.play(soundArray[position], volume, volume , 1,0,1f);
    }
});




return view;
}

    private void setVolumeControlStream(int streamMusic) {}
}
公共碎片测验(){
}
@可空
@凌驾
创建视图上的公共视图(LAYOUTINGER充气机,@Nullable ViewGroup
容器,捆绑包savedInstanceState){
视图=充气机。充气(R.layout.quick_碎片,容器,假);
soundArray=新整数[5];
这个.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool=新的soundPool(最大值,AudioManager.STREAM_MUSIC,0);
soundPool.setOnLoadCompleteListener(新的soundPool.OnLoadCompleteListener(){
@凌驾
public void onLoadComplete(声音池声音池,int-sampleId,int-status){
加载=真;
}
});
soundArray[0]=soundPool.load(getActivity().getApplicationContext(),R.raw.bird,1);
soundArray[1]=soundPool.load(getActivity().getApplicationContext(),R.raw.rooster,1);
soundArray[2]=soundPool.load(getActivity().getApplicationContext(),R.raw.elephant,1);
this.soundPool.setOnLoadCompleteListener(新的soundPool.OnLoadCompleteListener(){
@凌驾
public void onLoadComplete(声音池声音池,int-sampleId,int-status){
加载=真;
}
});
AudioManager AudioManager=(AudioManager)
getContext().getSystemService(Context.AUDIO_服务);
浮动实际音量=(浮动)音频管理器
.getStreamVolume(AudioManager.STREAM_MUSIC);
浮点最大音量=(浮点)音频管理器
.getStreamMaxVolume(audiomanager.STREAM_MUSIC);
最终浮动体积=实际体积/最大体积;
listView=(listView)getView().findViewById(R.id.list);
字符串[]值=新字符串[]{
“安卓棒棒糖”、“贝塔”、“马什梅洛”
};
ArrayAdapter=新的ArrayAdapter(getContext().getApplicationContext(),android.R.layout.simple_list_item_1,android.R.id.text1,值);
setAdapter(适配器);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
int itemPosition=位置;
String itemValue=(String)listView.getItemAtPosition(position);
Toast.makeText(getActivity().getApplicationCo