javax.sound to Android
Добавлено: 13 сен 2013, 12:14
Написал код на Java, и с печалью осознал, что Андроид не знаком с пакетом javax.sound.
Т.е. мне нужно загрузить звуковой mp3 файл в память и получить его byte массив.
Вот кусок кода на Java (я все функции в одну свалил, что бы было... понятнее), как тот же самый функционал сделать на Адроиде?
[syntax=java5]
private static float sampleRate = 0;
public static byte[] decoding(File audioFile) throws IllegalArgumentException, UnsupportedAudioFileException, IOException {
AudioInputStream in = null;
if(audioFile.exists())
in = AudioSystem.getAudioInputStream(audioFile);
.
.
.
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
sampleRate = baseFormat.getSampleRate();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16, //16-bits
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
.
.
.
ByteArrayOutputStream out = null;
try {
byte[] buffer = new byte[4096];
out = new ByteArrayOutputStream();
int length = -1;
while ((length = din.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, length);
}
byte[] audio = out.toByteArray();
}
finally {
din.close();
out.close();
}
return audio;
}
[/syntax]
Т.е. мне нужно загрузить звуковой mp3 файл в память и получить его byte массив.
Вот кусок кода на Java (я все функции в одну свалил, что бы было... понятнее), как тот же самый функционал сделать на Адроиде?
[syntax=java5]
private static float sampleRate = 0;
public static byte[] decoding(File audioFile) throws IllegalArgumentException, UnsupportedAudioFileException, IOException {
AudioInputStream in = null;
if(audioFile.exists())
in = AudioSystem.getAudioInputStream(audioFile);
.
.
.
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
sampleRate = baseFormat.getSampleRate();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16, //16-bits
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
.
.
.
ByteArrayOutputStream out = null;
try {
byte[] buffer = new byte[4096];
out = new ByteArrayOutputStream();
int length = -1;
while ((length = din.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, length);
}
byte[] audio = out.toByteArray();
}
finally {
din.close();
out.close();
}
return audio;
}
[/syntax]