2016-09-26 2 views
0

私は、新しいFragmentが開始されたときにサウンドの再生を開始する必要のあるアプリを開発中です。だから私はonCreateViewでサウンドを再生するためにサウンドプールを使用しますが、動作しません。何が問題なのでしょうか? onCreateViewでpoolpoolが動作しないのですか?SoundPoolクラスをonCreateView()で再生できますか?

これは私のコード

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    rootView = inflater.inflate(R.layout.next_ex, container, false); 

    createSoundPool(); 
    loadSounds(); 
    volumeSounds(); 

    mSoundPool.play(sdID_clock,volume/2,volume/2,0,0,1); 

    return rootView; 
} 

protected void createSoundPool(){ 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     mAttributesBuilder = new AudioAttributes.Builder(); 
     mAttributesBuilder.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION); 
     mAttributesBuilder.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION); 
     mAttributes = mAttributesBuilder.build(); 

     mSoundPoolBuilder = new SoundPool.Builder(); 
     mSoundPoolBuilder.setAudioAttributes(mAttributes); 
     mSoundPool = mSoundPoolBuilder.build(); 
    } else { 
     mSoundPool = new SoundPool(10, AudioManager.MODE_IN_COMMUNICATION,0); 
     mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 
      public void onLoadComplete(SoundPool soundPool, int sampleId, 
             int status) { 
       loaded = true; 
      } 
     }); 
    } 

} 

protected void loadSounds(){ 
    sdID_clock = mSoundPool.load(getContext(), R.raw.clock,1); 
} 

protected void volumeSounds(){ 
    getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); 
    curVolume = (float)audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    maxVolume = (float)audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 

    volume = curVolume/maxVolume; 
} 
+0

createSoundPool()を呼び出す必要があります。 after volumeSounds(); –

答えて

2

はい、あなたができることです。非同期スレッドからこれらのメソッドを呼び出します。

createSoundPool(); 
loadSounds(); 
volumeSounds(); 

次に、再生する前にサウンドプールがロードされるように、setOnLoadCompleteListenerをオーバーライドします。

関連する問題