2013-07-11 39 views
0

我有一个ListPreference设置,它有四个选项。我只是想知道是否可以在选择其中一个选项后播放声音?我希望在从ListPreference中选择它之后创建一个声音作为响铃的声音,并且我希望在选择它之后播放一个声音样本。从ListPreference中选择选项后,我不希望单击按钮播放声音。在选择完成后,我希望它在PreferenceScreen打开时播放。选择ListPreference时播放声音

回答

0

也许你可以添加一个监听器,并在回调中从classpath中读取声音文件来播放它。我很快就黑了一个例子(使用Java7)来演示播放音频文件的例子,希望它有帮助:

import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.UnsupportedAudioFileException; 

public class Main { 

    public static void main(String args[]) { 
     Clip clip; 
     try { 
      clip = AudioSystem.getClip(); 
      AudioInputStream inputStream = AudioSystem.getAudioInputStream(
        Main.class.getResourceAsStream("Morse.aiff")); 
      clip.open(inputStream); 
      clip.start(); 
      // optionally, sleep the thread for the duration of the audio clip, 
      // or else you may not hear it 
      Thread.sleep(5000); 
     } catch (LineUnavailableException | UnsupportedAudioFileException | IOException | InterruptedException ex) { 
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
}