【问题标题】:button sound stops after while按钮声音在一段时间后停止
【发布时间】:2017-04-02 13:03:08
【问题描述】:

我创建了一个带有许多声音按钮的应用程序,问题是一段时间后它们停止工作,我必须重置应用程序。有什么办法管理吗?

public class TwoFragment extends Fragment{

SoundPool mySound1;
int ayyId;
SoundPool mySound2;
int eyyId;
SoundPool mySound3;
int keId;
SoundPool mySound4;
int kraId;
SoundPool mySound5;
int pandaId;
SoundPool mySound6;
int timeId;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mySound1 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    ayyId = mySound1.load(getActivity().getApplicationContext(), R.raw.ayy, 1);
    mySound2 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    eyyId = mySound2.load(getActivity().getApplicationContext(), R.raw.eyy, 1);
    mySound3 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    keId = mySound3.load(getActivity().getApplicationContext(), R.raw.kee, 1);
    mySound4 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    kraId = mySound4.load(getActivity().getApplicationContext(), R.raw.kraaa, 1);
    mySound5 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    pandaId = mySound5.load(getActivity().getApplicationContext(), R.raw.pandaa, 1);
    mySound6 = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
    timeId = mySound6.load(getActivity().getApplicationContext(), R.raw.time, 1);
        }
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_two,container,false);
    View v = inflater.inflate(R.layout.fragment_two,container,false);
    Button des1 = (Button) view.findViewById(R.id.des1);
    Button des2 = (Button) view.findViewById(R.id.des2);
    Button des3 = (Button) view.findViewById(R.id.des3);
    Button des4 = (Button) view.findViewById(R.id.des4);
    Button des5 = (Button) view.findViewById(R.id.des5);
    Button des6 = (Button) view.findViewById(R.id.des6);
    des1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {mySound1.play(ayyId, 1, 1, 1, 0, 1);}
    });
    des2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {mySound2.play(eyyId, 1, 1, 1, 0, 1);}
    });
    des3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {mySound3.play(keId, 1, 1, 1, 0, 1);}
    });
    des4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {mySound4.play(kraId, 1, 1, 1, 0, 1);}
    });
    des5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {mySound5.play(pandaId, 1, 1, 1, 0, 1);}
    });
    des6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {mySound6.play(timeId, 1, 1, 1, 0, 1);}
    });

    MobileAds.initialize(getActivity(), "ca-app-pub-xxxxxxxxxxxxxxxxxxxx~xxxxxxxx");
    AdView mAdView = (AdView) view.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    return view;

}
}

我还想问当我点击另一个按钮时如何停止播放按钮。谢谢

【问题讨论】:

    标签: android audio soundpool


    【解决方案1】:

    我建议使用.release()。根据文档:

    释放 SoundPool 资源。释放 SoundPool 对象使用的所有内存和本机资源。 SoundPool 无法再使用,应将引用设置为 null。

    你也可以将播放的streamid保留在arraylist中,然后点击播放按钮时清除并播放。

        List<Integer> streams = new ArrayList<Integer>();
    SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    
    public void onClick(View view) {
        int streamId = 0;
        stop_clear_stream();
        switch (view.getId()) {
            case R.id.button1:
                streamId = pool.play(soundid1, 1.0f, 1.0f, 1, 0, 1.0f);
                break;
            case R.id.button2:
                streamId = pool.play(soundid2, 1.0f, 1.0f, 1, 0, 1.0f);
                break;
            .
            .
            .
            .
            .
    
            streams.add(streamId);
    
        }
    }
    
    private void stop_clear_stream() {
        if (streams != null && streams.size() > 0)
            for (Integer stream : streams)
                pool.stop(stream);
    
        streams.clear();
    }
    

    我还没有测试过上面的代码。

    【讨论】: