2011-12-20 66 views
0

我正在開發一款記憶遊戲,其中我想在開始遊戲時播放背景音樂。在整個遊戲過程中播放背景音樂

我使用了聲音池類併成功執行了它,但我在播放時遇到問題。我添加了兩分鐘的.mp3音頻文件,但它只播放了15秒多。

任何人都可以說我有什麼問題嗎?這是我的soundclass文件:

public class Soundclass { 
    private SoundPool soundpool; 
    private HashMap<Integer, Integer> soundpoolmap; 
    private AudioManager audiomanager; 
    private Context context; 

    public void initSounds(Context thecontext) { 
     context = thecontext; 
     soundpool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); 
     soundpoolmap = new HashMap<Integer, Integer>(); 
     audiomanager = (AudioManager) context 
       .getSystemService(Context.AUDIO_SERVICE); 
    } 

    public Soundclass() { 

    } 

    public void addSound(int Index, int soundID) { 
     soundpoolmap.put(Index, soundpool.load(context, soundID, 1)); 
    } 

    public void playSound(int Index) { 
     float streamVolume = audiomanager 
       .getStreamVolume(AudioManager.STREAM_MUSIC); 
     streamVolume = streamVolume 
       /audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     soundpool.play(soundpoolmap.get(Index), streamVolume, streamVolume, 1, 
       0, 1f); 
    } 

    public void playLoopedSound(int Index) { 
     float streamVolume = audiomanager 
       .getStreamVolume(AudioManager.STREAM_MUSIC); 
     streamVolume = streamVolume 
       /audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
     soundpool.play(soundpoolmap.get(Index), streamVolume, streamVolume, 1, 
       -1, 1f); 
    } 

} 

,我用這個類給出activitycalss。在OnCreate中功能

Button btn = (Button) findViewById(R.id.sound); 
     soundclass = new Soundclass(); 

     soundclass.initSounds(getBaseContext()); 
     soundclass.addSound(1, R.raw.jinglebells); 

     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       soundclass.playSound(1); 

      } 
     }); 
+0

你是什麼意思「2開發記憶遊戲」?你爲什麼使用前綴2? – Lion 2011-12-20 08:45:34

+0

編輯您的問題 – mH16 2011-12-20 08:49:37

+0

@Lion現在看到(我盡我最大努力使問題可讀) – 2011-12-20 09:04:00

回答

5

的Soundpool並不意味着用於背景音樂。它有許多限制,如文件大小加載和播放時間。它主要用於音效。 使用MediaPlayer類來播放背景音樂。

+0

感謝您的指導。我會嘗試在mediapalyer播放此文件 – Thamilvanan 2011-12-20 09:13:05

+0

@Thamilvanan:如果你覺得這個答案有幫助,然後將其標記爲接受。 – 2011-12-21 05:13:26

+0

嘿我用meadiaplayer背景音樂..它得到執行感謝。 – Thamilvanan 2011-12-21 06:20:03