Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can the android emulator play audio

I wanted to record and pass through the recorded sound to the phone's speaker, but I could not get the recording code to work (app crashes, SEE MY ATTEMPT HERE) so I am now trying to see if the emulator can do anything related to audio or not. I copied a 1 sec recording, in both wav (16 bit pcm, 44k sampling frequency, mono) and mp3 (recording and conversion both done through Audacity) to the sdcard. I can see the files in the IDE's file explorer, so I guess the sdcard is being properly detected by the emulator. But I could not get the emulator's built in music player to detect them (Why ??).

As a second attempt, I copied the code HERE to the sample hello world Android app. Here's the main activity class

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // String PATH_TO_FILE = "/sdcard/asMP3.mp3";
        // String PATH_TO_FILE = Environment.getExternalStorageDirectory().getPath()+"/asMP3.mp3";
        String PATH_TO_FILE = Environment.getExternalStorageDirectory().getPath()+"/wavSigned16bitPCM.wav";
        MediaPlayer mp1 = new MediaPlayer();
        try
        {

            mp1.setDataSource(PATH_TO_FILE);
            mp1.prepare();
            mp1.start();
            Toast.makeText(getApplicationContext(), "HERE", Toast.LENGTH_SHORT).show();
        }
        catch (IllegalArgumentException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalStateException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}  

I assumed this would start playing the sound as soon as the app starts. The Toast shows up so I know the code is executing. The program does not crash but nothing else happens either, no sound in this case as well (Why ?)

As a third attempt, I used the code HERE, and added the files I wanted to play in res\raw as it says. This program does not crash either, but I still cannot hear anything.

So the question is, is it possible to do anything at all related to audio, on the emulator? Looking at THIS QUESTION it looks like this should be possible, so why isn't it happening in my program? Do I need to set any permissions int he manifest for audio output as well?

----EDIT----

I have also seen THIS, but if I use the -useaudio option the emulator just says -useaudio is an unknown option, and emulator -help does not list it, hen it is clearly shown as an option in the developers website and moreover it says that useaudio is enabled by default. So why isn't my emulator playing any sound?

--- UPDATE ---
It seems the audio features do not work if the emulator has been started using a snapshot. If not, the audio feature still may or may not work depending on the computer. Please see HERE

like image 708
user13267 Avatar asked Nov 01 '22 16:11

user13267


1 Answers

Yes, you can do audio related work on emulator.

Your code sequence should be -

mp1 = new MediaPlayer();
mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp1.setDataSource(PATH_TO_FILE);
mp1.prepare();
mp1.start();

And for setting permissions in the manifest file.

Use these for record feature-

and these for playing feature -

There is a guide article on official android developer website

Guide to audio capture and Guide to media playback

and if it dosn't work, then post your log cat screenshot.

like image 119
Karan Nagpal Avatar answered Nov 12 '22 23:11

Karan Nagpal