Dynamically re-reading a midi file
I am writing an app that dynamically creates a midi file which I then want to play. Each time I pr开发者_JAVA百科ess a button in my app a new midi file is created and written to external storage. I then want the app to read that file and play it. I am using SoundPool to play the file. However, from the samples I have found, SoundPool is initialized during onCreate and therefore my file is only read once. Meaning the same file is played everytime I hit the button regardless of any changes I make to it. I have tried moving the SoundPool initialisation into my Audio1ButtonHandler method but then I get no sound or get a selection of “sample x not ready” errors. The relevant code from my app is here:
public void onCreate(Bundle savedInstanceState) {
…………
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(SOUNDPOSITION, soundPool.load("/sdcard/test.mid", 1));
}
public void Audio1ButtonHandler(View target) {
ArrayList<Integer> chord = new ArrayList<Integer>();
chord = Chord.calculateChord(kSelection, cSelection);
for (int i = 0; i < chord.size(); i++) {
System.out.println("chord value is " + chord.get(i).toString());
}
Midi.createMidiFile(chord);
playSound();
releaseSound();
}
public void playSound() {
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(soundPoolMap.get(SOUNDPOSITION), volume, volume, 1, 0, 1f);
}
public void releaseSound() {
soundPoolMap.clear();
soundPool.release();
}
As you can see I have tried to release the resources (within releaseSound) to try and force them to be re-read but to no avail.
Any pointers would be greatly received. Thanks
First stop playing before you release! Then, after release, set soundPool to null.
精彩评论