24
I am using a media codec class in Android (Jelly Bean) to encode PCM format to AAC. The file was encoded but no music player is able to play that file. I was not able to find any working code or proper documentation on the net.
This is my code:
public void doConvert()
{
new AsyncTask<Void, Void, Void>()
{
@Override
protected Void doInBackground(Void... params)
{
try
{
int codecCount = MediaCodecList.getCodecCount();
for ( int i=0; i < codecCount; i++)
{
MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
Logger.getLogger(MainActivity.class.getSimpleName()).log(Level.INFO, info.getName());
for ( String type : info.getSupportedTypes() )
{
Logger.getLogger(MainActivity.class.getSimpleName()).log(Level.INFO, type);
}
}
File inputFile = new File( Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/Ghajini27_Mono_8Khz.wav");
//File inputFile = new File( sampleFD.get);
Log.e("File", String.valueOf(inputFile.length()));
FileInputStream fis = new FileInputStream(inputFile);
fis.skip(44);//remove wav header
File outputFile = new File( Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/out.m4a");
if ( outputFile.exists()) outputFile.delete();
FileOutputStream fos = new FileOutputStream(outputFile);
//BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(outputFile));
MediaCodec codec = MediaCodec.createEncoderByType("audio/mp4a-latm");
MediaFormat outputFormat = MediaFormat.createAudioFormat("audio/mp4a-latm", 22050, 1);
outputFormat.setInteger(M