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();
}
}
Related
Currently I am using
mAudioManager.registerMediaButtonEventReceiver(mRemoteControlResponder);
but this is now deprecated in 5.0 and replaced by setMediaButtonReceiver. There are 5 links in Google all pointing to developer.android.com.
Has anyone used this yet? If so can you provide an example?
Check this page: http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-0/android/media/session/MediaSession.java It is a rather large example of the complete flow.Here one of the most relevant parts
Set a pending intent for your media button receiver to allow restarting playback after the session has been stopped. If your app is started in this way an android.content.Intent.ACTION_MEDIA_BUTTON intent will be sent via the pending intent.
Parameters: nullmbr The android.app.PendingIntent to send the media button event to.
public void More ...setMediaButtonReceiver(#Nullable PendingIntent mbr) {
try {
mBinder.setMediaButtonReceiver(mbr);
} catch (RemoteException e) {
Log.wtf(TAG, "Failure in setMediaButtonReceiver.", e);
}
}
Let's say I have async tasks that when finished, lock the vibrator and send it a pattern. In a nutshell, that's the kind of code I'm dealing with:
lock(vib);
vib.vibrate(pattern);
release(vib);
return;
The problem is, the vibration starts and just immediately stops due to the task's returning and I could use some help in changing that.
I have tried
lock(vib);
long duration = getDuration(pattern);
vib.vibrate(pattern);
synchronized(this){
try {
wait(duration);
} catch (InterruptedException e) {
...
}
}
release(vib);
return;
but that doesn't seem to actually do anything.
Since I have not found a way to determine whether or not the phone is currently vibrating, any suggestions on how I should best resolve the issue?
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'm trying to create an audio streamer with Android's MediaPlayer. It's just fine if it doesn't work with Android 2.1 or bellow. I need to be able to play the audio from a SHOUTcast stream. Here's my code:
player = new MediaPlayer();
try {
player.setDataSource("http://87.230.103.107:8000");
player.prepareAsync();
} 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();
}
player.start();
For some reason, this code will play just nothing. I think it may be related to the app permissions. Does anyone know what's going on?
Thanks!
[UPDATE]
I'm getting the following errors:
04-25 23:35:15.432: ERROR/MediaPlayer(283): start called in state 4
04-25 23:35:15.432: ERROR/MediaPlayer(283): error (-38, 0)
04-25 23:35:15.602: ERROR/MediaPlayer(283): Error (-38,0)
04-25 23:35:17.542: INFO/AwesomePlayer(33): calling prefetcher->prepare()
04-25 23:35:18.547: INFO/Prefetcher(33): [0x17650] cache below low water mark, filling cache.
04-25 23:35:18.762: INFO/AwesomePlayer(33): prefetcher is done preparing
04-25 23:35:19.769: ERROR/AwesomePlayer(33): Not sending buffering status because duration is unknown.
Sorry for the late response, ran across this while looking for answers to another problem.
If you still need an answer, you're calling start() while it's still being prepared. PrepareAsync() returns immediately, unlike prepare(). The problem with using 'prepare()` with streams is that it will block until it has enough data to start play.
What you want to do is set an OnPreparedListener, and have the start() called from there.
The problem is that content type "audio/aacp" streaming is not supported directly . Some decoding library can be sued to play "aacp", please see the solution below:
Freeware Advanced Audio (AAC) Decoder for Android
How to use this library?
For more detail please see this.
Consider legal issues while using it.
the project http://code.google.com/p/aacplayer-android/ is licensed
under GPL, so you can create commercial apps * on top of it, but you
need to fullfill the GPL - mainly it means to publish your code as
well. * If you use the second project
http://code.google.com/p/aacdecoder-android/ , then you do not need to
publish your * code (the library is licensed under LGPL).
1)Avoid using prepare(), use prepareAsyc() instead. (or) put your playing logic in a worked thread or a separate thread. 1)Avoid using player.prepare(), instead use player.prepareAsync();
(or) keep the logic in a separate thread. (you can use intent service, AsyncTask,etc)
2)Also try with static constructor MediaPlayer.create(Uri);
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.