2014-10-28 3 views
0

의도를 사용하는 다른 클래스에서이 클래스를 호출하고 있습니다. 클래스의로드시이 노래 "song.ogg"를 재생하려고합니다. 어떻게 도와주십시오.의도의로드시 소리가 울리지 않습니다.

public class DetlsActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.detls_layout); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 

     SoundPool soundPool; 
     int soundID; 
     soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
     soundID = soundPool.load(this,R.raw.song, 1); 
     AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
     //soundPool.play(soundID,1f,1f,1,0,1f); 
     Toast.makeText(this,"Ringing Song",Toast.LENGTH_LONG).show(); 
     try { 
     AssetFileDescriptor assetFileDescriptor = this.getAssets().openFd("song.ogg"); 
     soundPool.load(assetFileDescriptor,1); 
      Toast.makeText(this,"Played",Toast.LENGTH_LONG).show(); 
     } 
     catch (Exception e) { 
      Log.d("LOGCAT","Exception Song"); 
     } 
+0

LogCat에서 예외가 있습니까? – aProgrammer

+0

아니요, 예외가 없으므로이 코드가 맞습니까? – Makwana

+0

나는 토스트를 받고있다 "Ringing Song"은 아니지만 토스트는 "playing" – Makwana

답변

0
SoundPool soundPool; 
    int soundID; 
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    Toast.makeText(this,"Ringing Song",Toast.LENGTH_LONG).show(); 
    try { 
     soundID = soundPool.load(this,R.raw.song, 1); 
     int waitLimit = 1000; 
     int waitCounter = 0; 
     int throttle = 10; 
     while(soundPool.play(soundID, 1.f, 1.f, 1, 0, 1.f) == 0 && waitCounter < waitLimit) 
      {waitCounter++; SystemClock.sleep(throttle);} 
     Toast.makeText(this,"Played "+soundID,Toast.LENGTH_LONG).show(); 

    } 
    catch (Exception e) { 
     Log.d("LOGCAT","Exception Song"); 
     Toast.makeText(this,"Exception "+e,Toast.LENGTH_LONG).show(); 
    } 

위 코드를 사용하여 코드를 바꿉니다. 현재 코드는 경고 soundpool 샘플 1이 준비되지 않음이므로 조건을 사용하여 이것을 확인하십시오.

여기 리소스가 원시 폴더에 있으므로 우리는 soundID = soundPool.load(this,R.raw.song, 1);을 사용합니다. 리소스가 해당 시점에 자산 폴더에있는 경우 soundID = soundPool.load(getAssets().openFd("song.ogg"),1);

+0

고마워요 .. 아주 좋아요 :) – Makwana

관련 문제