5

I have a problem with audio on android.

Short Question:

I need to play and record audio in aac format an all Android devices. I found that it is possible starts from API 10, but on my BLU device(2.3.5) it works by using MediaRecorder and MediaPlayer. But on HTC Nexus One it doesn't work. Have you any suggestions?

Long question:

To record and play audio in AAC format I'm using following code. It pretty simple and stupid, but it works for testing.

String pathForAppFiles = getFilesDir()
                .getAbsolutePath();
        pathForAppFiles += "/bla.mp4";
        if (audioRecorder == null) {

            File file = new File(pathForAppFiles);
            if (file.exists())
                file.delete();

            audioRecorder = new MediaRecorder();

            audioRecorder
                    .setAudioSource(MediaRecorder.AudioSource.MIC);

            audioRecorder
                    .setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

            audioRecorder
                    .setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

            audioRecorder.setOutputFile(pathForAppFiles);

            try {
                audioRecorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            audioRecorder.start();

        } else {
            audioRecorder.stop();
            audioRecorder.release();
            audioRecorder = null;

            new AudioUtils().playSound(pathForAppFiles);
        }

new AudioUtils().playSound(pathForAppFiles); - creates MediaPlayer and play sound from file;

To make it work on nexus I tried aac-decoder - it doesn't play file to the end (plays only 6 second of 10 seconds file). And it doesn't plays sound recorded by code above.

Also I tried to install FFmpeg, but I haven't experience to make this library work.

So can you recommend something?

Yanny
  • 490
  • 4
  • 16
  • https://code.google.com/p/aacplayer-android/ – R KiranKumar Mar 13 '13 at 11:21
  • aac-decoder is a short version of aacplayer. It doesn't play aac file to the end and doesn't play sound recorded by device. – Yanny Mar 13 '13 at 14:12
  • I found partial solutions. 1. On some devices audio not played because I'm trying to play it from files derectiory - data/data/com.myapp.player/files/music.aac. 2. On some devices it not working but aacdecoder can play file to the end. Just set to it small buffer size AACPlayer aacPlayer = new MultiPlayer(clb, 200, 200); – Yanny Mar 20 '13 at 14:23

1 Answers1

1

I resolve this issue by changing audio type to MP3, because some devices (like Kindle Fire) do not play aac from anywhere.

My recommendation is: If you want to make cross platform sounds - use MP3. You can convert any sound to MP3 with lame encoder.

Yanny
  • 490
  • 4
  • 16