按钮声音停止后,而

问题描述:

我已经创建了一个应用程序与许多声音按钮,问题是,一段时间后,他们停止工作,我必须重置应用程序。有什么方法可以管理它吗?按钮声音停止后,而

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; 

} 
} 

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

我推荐使用.release()。根据文档:

释放SoundPool资源。释放SoundPool对象使用的所有内存和本机资源。 SoundPool不能再使用,并且引用应该设置为null。

此外,您还可以将播放流保持在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(); 
} 

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