I am trying to toggle the microphone of Android mobile. App. freezes if I run the mute and unmute in a loop, With a delay of say 500ms. Is there a specific reason mobile behaves this way? (Motorola Droid is not even able to mute the phone) however all other mobiles are able to do it.
This runs in a loop for 20 times
audioService.setMicrophoneMute(true);
if(audioService.isMicrophoneMute())
{
Toast.makeText(getBaseContext(), "MUTED", 1).show();}
try {
Thread.sleep(1000,90);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
audioService.setMicrophoneMute(false);
if(!audioService.isMicrophoneMute()){
Toast.makeText(getBaseContext(), "Un MUTED", 1).show();
It was running on main thread, works fine once I spawned a new thread.
Related
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
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.
Well... Here is the background:
I'm working in a experimental app that uses pocketsphinx. Basically, I'm running one service that runs a thread. This thread manages a RecognizerTask object. It gives a signal to the RecognizerTask object to start recording and recognizing the speech and after 15 seconds, it signals shutdown to the RecognizerTask object. Right after, it signals start again and the procedure repeats.
Follows the code of the thread that manages the RecognizerTask object:
public void run() {
while(!this.interrupted){
try{
Thread.sleep(15000);
}
catch (InterruptedException e){
e.printStackTrace();
return;
}
rec.shutdown();
try {
rec_thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(Thread.interrupted())
return;
rec = new RecognizerTask();
rec_thread = new Thread(rec);
System.gc();
rec.setRecognitionListener(MyService.this);
rec_thread.start();
rec.start();
}
}
The problem in here is the following: If the device is awake and the screen, turned on, everything runs fine. But if the screen is locked, the thread just freezes at the .join() call. Inspecting further, I realized that the .join() freezes because one call to the AudioRecord instance that exists inside the ReconizerTask object just does not return! And as soon as I unlock the phone, everything runs fine again! Also, my service acquire a Partial Awake Lock, while it's running.
And worse: this only happens with Galaxy S3 devices (the virtual devices and a Galaxy Nexus didn't show this behavior), as far as I know.
So, the question is: what can be causing this problem?
I wrote simple application, for my Galaxy SII, for permanent connection with remote server. Ones for 5 second it sends and receives data. While application is working nobody can't call to me. Network answers him - interlocutor is unavailable.
The same happend me when I use mail client like K-9. It doesn't matter I use GPRS or 3G connection.
What is a main rule (if is it?) to construct internet application to avoid this problem (I mean problem with ordinary incomming phone connection)?
My ordinary code for sending data (in remote service) is like this:
While (condition)
{
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
out.write(data + "\n");
out.flush();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Regards,
Artik
First, if you want to wait for 5 seconds you should use Thread.sleep(5000); not Thread.sleep(500); which is half a second.
Second, consider sending your data to the server using Timer instead of Thread.sleep() and while(condition)
After modifying your code with the above suggestions, try to test from the emulator and simulate a phone call.
I have an application that shows, plays and delete videos. I want to know that how to pause the current thread or processing while the media scanner is running. it takes about 4 to 7 seconds for media scanner to complete its activity..
any suggestions
If I understood you correctly you just need to implement the OnScanCompletedListener.
If not, add some code to clarify your question.
So if you want to know when the media scanner is done you have to register BroadcastReceiver which will listen for ACTION_MEDIA_SCANNER_FINISHED. Here is good presentation that gives good explanation of Intent, IntentFilter and BroadcastReceiver.
I use this code:)
while(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
System.out.println("wait sdcard mount.");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
My first post here. This website has been very useful for learning Android programming, thanks to everyone.
I have a simple app that loads an MP3 stream and plays it. It works fine on 1.6 and 2.1 but on 2.2 it doesn't quite work right. It seems my service is having a problem starting, it's giving my an ANR and the dialog where I have to tap "Wait", and then finally the service starts. Why is the service taking a long time to start up?
Here's my simple code that sets the source and plays the audio:
public class MyActivityService extends Service {
MediaPlayer player = new MediaPlayer();
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
try {
player.setDataSource("URL OF MUSIC FILE");
} 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();
}
try {
player.prepare();
player.setVolume(1, 1);
player.setLooping(true);
player.start();
Toast.makeText(getApplicationContext(), "Audio Service Started.", Toast.LENGTH_SHORT).show();}
ADDENDUM:
Turns out it wasn't my service not starting, it's the audio that's not starting quickly enough in 2.2...
I got rid of the ANRs when my service started and stopped by putting the onCreate() and onDestroy() methods of my service in their own threads, which is probably how it should have been from the beginning? Sorry, just learning.
But the delay that's the real problem remains, to clarify --
For example, my code, as it is right now, works fine and dandy and just how I want it to in 2.1 AND 2.2 when I set the data source to an MP3 file like this: http://dl.dropbox.com/u/6916184/TestSongStream.mp3
BUT when I set the data source to an audio stream location like this: http://d.liveatc.net/kjfk_twr or this: http://relay.radioreference.com:80/192236577 it works correctly ONLY in 2.1.
In 2.2 the sound does start playing eventually, but it takes "StagefrightPlayer" about 25 seconds or so after setting the data source until the audio starts as shown here:
07-31 02:54:30.176: INFO/StagefrightPlayer(34): setDataSource('http://relay.radioreference.com:80/192236577')
07-31 02:54:55.228: INFO/AwesomePlayer(34): calling prefetcher->prepare()
07-31 02:54:56.231: INFO/Prefetcher(34): [0x2cc28] cache below low water mark, filling cache.
07-31 02:54:56.337: INFO/AwesomePlayer(34): prefetcher is done preparing
07-31 02:54:56.347: DEBUG/AudioSink(34): bufferCount (4) is too small and increased to 12
07-31 02:54:57.337: ERROR/AwesomePlayer(34): Not sending buffering status because duration is unknown.
It also takes the same amount of time, about 25 seconds, to stop the media player after player.stop() is called in the onDestroy() method of my service, and the onDestroy() method continues on to Toast a message and cancel a notification.
Is that just the way it is with 2.2? If so, I can work around the delays, that's not a problem. Or more likely is it something I am doing wrong? But it works exactly as I want in 1.6 and 2.1!
Would posting more code help?
My code is very simple. There are no audio controls or anything like that. Simply a start audio button and stop audio button that start and stop a service that plays an audio stream.
Thank you for any help!
Do not call prepare() from the main application thread, particularly for a stream, because that may take much longer than you're allowed before an ANR. Use prepareAsync() instead. I have no idea if that is the root of your particular problem, but it would certainly be one cause of an ANR in your current implementation.