2013-04-17 2 views
0

나는이 사운드 풀 클래스를 (튜토리얼의 도움을 얻어서) 구현하려고 시도했지만 가상 장치에서 실행하려고하면 사운드가 재생되지 않는다 ... main_activity XML 파일은 잘로드되지만 선적 할 때, 소리가 나는 예상되었다.내 soundpool 프로젝트가 제대로 재생되지 않습니까?

여기는 소스 코드입니다.

public class MainActivity extends Activity { 

private SoundPool soundpool; 
private boolean soundPoolLoaded = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Create a new SoundPool instance. 
    // 2 = number of sounds that can be simultaneously played 
    // AudioManager.STREAM_MUSIC = The type of sound stream. STREAM_MUSIC is the most common one 
    // 0 = sound quality, default is 0. 
    // setOnLoadCompleteListener loads the file 
    soundpool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0); 
    soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
      soundPoolLoaded = true; 
     } 
    }); 

    playSound("car_phonic"); 
} 


// This method will load a sound resource from the res/raw folder by the input name given as a string (soundName). 
// If the sound resource is found by name, it will attempt to play the sound 
// soundPoolLoaded value becomes true when the file is fully loaded 
public void playSound(String soundName) { 
    if(soundPoolLoaded == true) 
    { 
     // Get the current context resources and find the correct sound resource for playing the sound 
     Resources res = this.getResources(); 
     int soundId = 0; 
     soundId = res.getIdentifier(soundName, "raw", this.getPackageName()); 

     if(soundId != 0){ 
     soundpool.play(soundId, 1, 1, 0, 0, 1); 
     } 


     SystemClock.sleep(500); 
     playSound(soundName); 

    } 
    } 
} 

아무 도움이됩니다.

답변

1
  • 놀이() SoundPools 부하 (에 의해 반환 된 soundID),
  • 에서는 onLoadComplete 다음 사운드 파일이로드가 완료라고하지 일반 안드로이드 리소스 ID를 취

그래서에서 sampleId 인수를 통지 그것의 기본 양식 :

soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() { 
    @Override 
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 
     soundpool.play(sampleId, 1, 1, 0, 0, 1); 
    } 
}); 

soundpool.load(this, R.raw.car_phonic, 0); 

SoundPool은 백그라운드에서 데이터를로드하므로 샘플을 즉시 사용할 수 없습니다. 여기

0

soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0); 
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
     float curVolume = audioManager 
       .getStreamVolume(AudioManager.STREAM_MUSIC); 
     float maxVolume = audioManager 
       .getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     float leftVolume = curVolume/maxVolume; 
     float rightVolume = curVolume/maxVolume; 
     int priority = 10; 
     // int priority_1=5; 
     int no_loop = 0; 
     float normal_playback_rate = 1f; 


final int ID=1; 
    soundPool.play(soundPoolMap.get(ID, 
         leftVolume, rightVolume, priority, no_loop, 
         normal_playback_rate); 
soundpool 사용하는 방법입니다
관련 문제