2013-07-08 115 views
-1

播放声音并停止播放声音的最佳方法是什么?Android - 播放和停止声音

我试过使用RingtoneManager,MediaPlayer和SoundPool,但未能停止声音。

使用RingtoneManager TYPE_ALARM时有没有办法停止声音?

请一个简单的片段。

这是我想的最后一件事:

 SoundPool pool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    List<Integer> streams = new ArrayList<Integer>(); 
    int soundId = pool.load(getApplicationContext(), R.drawable.alarm, 1); //There are several versions of this, pick which fits your sound 


    try{ 

     if(myWifiInfo.getRssi() < -55) 
     { 
      /* 
      Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); 
      r.play();*/ 

      Thread.sleep(1000); 
      int streamId = -1; 
      streamId = pool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f); 
      streams.add(streamId); 
      textRssi.setText(String.valueOf(myWifiInfo.getRssi() + " WARNING")); 
     } 
     else { 
      Log.e("WIFI","Usao"); 
       for (Integer stream : streams) { 
        pool.stop(stream); 
       } 
       streams.clear(); 
      Log.e("WIFI","Izasao"); 
     } 
+0

发布您尝试过的代码。我知道我已经看到媒体播放器和声音库的停止方法。 –

+0

@GabeSechan编辑帖子。干得好。 –

回答

0

我假设这个函数被调用多少次?问题在于你要将streamID添加到本地列表中。每次调用该函数时都会重新创建该列表,因此当您尝试呼叫停止时,该列表将始终为空。

如果它只被调用一次,那么问题是你从来没有打过停止,只玩(他们在不同的分支)。

+0

我尝试了你的建议,但它不起作用。声音在一段时间后也会停止播放,但不会在一定时间后播放。 –