It is a sample code of native playback 24bit/192kHz wav file for third-party App.
I hope this sample code will help developers of third-party App.
There are two points.
1. It is necessary to convert byte data of 24bit file to unsigned int 32bit byte data. Do not use float 32bit. (16bit do not need to be converted)
2. It is necessary to specify a special value of 10 for AudioFormat. ENCODING_PCM_FLOAT must not be specified. (16bit specify standard ENCODING_PCM_16 BIT)
Note
This sample code convert all byte data of audio file to 32bit at the beginning first and passes it to AudioTrack, so it can only be used with music files of very short time (a few seconds).
In actuality, convert byte data to uint32bit sequentially in the background thread.
About these processes, I think that music application developers have more technology than I do.
So I do not specifically describe code to convert to unsigned int 32bit.
Code:
// 24bit/192kHz wav file from external storage
File file = new File(path);
input = new FileInputStream(file);
wavData = new byte[(int) input.available()];
input.read(wavData);
input.close();
int headerByteNum = caliculateWavHeaderByteNumber(wavData);
// Convert 24bit to unsigned int 32bit. If 16bit, not require convert
wavData = Convert32bit(wavData, headerByteNum);
int SamplingRate = 192000;
// 24bit AudioFormat.
int audioFormat = 10; // X5 3rd Custom AudioFormat????(java format?)
//int audioFormat = AudioFormat.ENCODING_PCM_FLOAT; // X5 3rd can't play 32bit float. require unsigned int 32bit.
// 16bit AudioFormat.
//int audioFormat = AudioFormat.ENCODING_PCM_16BIT; // 16bit
int bufSize = android.media.AudioTrack.getMinBufferSize(SamplingRate, AudioFormat.CHANNEL_OUT_STEREO, audioFormat);
AudioTrack audioTrack = new AudioTrack(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build(),
new AudioFormat.Builder()
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
.setEncoding(audioFormat)
.setSampleRate(SamplingRate)
.build(),
bufSize,
AudioTrack.MODE_STREAM,
AudioManager.AUDIO_SESSION_ID_GENERATE);
audioTrack.play();
audioTrack.write(wavData, headerByteNum, wavData.length - headerByteNum);