Beginner android help playing audio from within a runnable - java

I'm playing around with some basic android, and I'm trying to write a metronome app. The basic idea is that I'm using a runnable in order to trigger a sound after a time period (msPeriod). I've tried to use SoundPool, but it will just log 'sample not loaded', and trying to ititialise a MediaPlayer causes the app to crash on opening. Could you explain to me where I'm going wrong please? Below is my code with MediaPlayer.
//I create the media player first thing inside MainActivity
private Handler handler = new Handler();
int msPeriod = 1000;
MediaPlayer mpHigh = MediaPlayer.create(this, R.raw.hightick);
MediaPlayer mpLow = MediaPlayer.create(this, R.raw.lowtick);
//within an onClick Listener
onClick(View v) { handler.postDelayed(startMetron, msPeriod); }
//the runnable that starts the metronome
private Runnable startMetron = new Runnable( ) {
#Override
public void run() {
if(isRunning){
if (count == 4) {
count = 1;
mphigh.start();
} else {
count++;
mplow.start();
}
}
textCount.setText(String.valueOf(count));
//triggering the next run
handler.postDelayed(this, msPeriod);
}
};
Thanks so much for bearing with me!

You are running a separate thread . The UI element must be updated form the main thread... so..
runOnUiThread(new Runnable(){
public void run() {
textCount.setText(String.valueOf(count));
}
});
This makes sure that the textview is update from the UI thread and your app will not crash.

Related

handler.postDelayed is not working(android eclipse)

I am trying to make a delay after playing the set of sounds in mediaplayer so that it won't overlap. i have tried thread.sleep and it worked but the problem is I have a button for sound to stop but because of the thread.sleep, it is not working so i searched and tried for an alternative way in which i can make a delay without locking the UI and i came up with handler.postDelayed but it is not working. is there anyone who can help me solve this problem? thanks in advance.
here is my code:
protected void managerOfSound() {
int size = tempq.size();
for (int i = 0; i < tempq.size(); i++) {
String u =tempq.get(i);
//WHOLE
if (u.equals("a4")){
mp = MediaPlayer.create(this, R.raw.a4);
mp.start();
final Runnable r = new Runnable() {
public void run() {
handler.postDelayed(this, 2000);
}
};
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// stuff you wanna delay
}
}, 2000);
The postDelayed(Runnable r, long delayMillis ) causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached ( int htis case the UI thread) . The time-base is uptimeMillis(). Time spent in deep sleep will add an additional delay to execution.
Reference : Android API documentation

Java Timer equivalent in Android

I recently began working with Java and am exploring Android development. I was trying to port over one of the Java programs I made, but I am having some difficulty with getting the java Timer to function the same way in Android. I read through a number of posts and they, for the most part, indicated that it would be better to use the Handler class in android as opposed to Timer.
This was my timer in Java:
playTimer = new Timer(1000/model.getFPS(), new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// do something
...
if( finished everything ) playTimer.stop();
}
});
And once a certain button was clicked, I would simply run "playTimer.start()" to start it.
As you can see, I had it set up so that the user could set the FPS they wanted (by simply setting the first parameter of the Timer class to 1000/model.getFPS()).
Now I've tried to do something similar in Android using handlers, but I am having some difficulty. It appears that the Handler ticks are not firing at the proper intervals. It seems that they are quite slow compared to what I need it to be. This is what I did in android so far:
public void startTimer() {
playHandler = new Handler();
startTime = System.currentTimeMillis();
playHandler.removeCallbacks(updateTimeTask);
playHandler.postDelayed(updateTimeTask, 0);
}
private Runnable updateTimeTask = new Runnable() {
public void run() {
// do something
...
if( finished everything ) playHander.cancel();
else {
playHandler.postDelayed(updateTimeTask, 1000/model.getFPS());
}
}
};
Excuse the semi-pseudocode. Can anyone shed any light? Thanks guys.
You can use a timer as below. The timer runs every second incrementing the counter. Displs the counter value in textview.
Timer runs on a different thread. SO you should set the text on the UI Thread.
The counter runs from 0 to 99. After 99 the timer is cancelled. Also cancel the timer when not required like in onPause().
public class MainActivity extends Activity {
TextView _tv,tv2;
Timer _t;
int _count=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_tv = (TextView) findViewById( R.id.textView1 );
_t = new Timer();
_tv.setText(R.string.app_name);
_t.scheduleAtFixedRate( new TimerTask() {
#Override
public void run() {
_count++;
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
_tv.setText(""+_count);
if(_count==99)
{
_t.cancel();
}
}
});
}
}, 1000, 1000 ); //change this value of 1000 to whatever you need.
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
_t.cancel();
}
}
If you decide not to use Timer (for whatever reason) you can just write a separate Thread that sleeps for x milliseconds and then wakes up and calls whatever Runnable you want it to call. That's going to be pretty precise. I have it working at the 10 millisecond level and it works quite nicely.
Just remember that it HAS to call a Runnable because a separate Thread can't have direct effect on anything on the main display thread.
public boolean keepPlayingAnimation = true
Handler h = new Handler()
Runnable updateDisplay = new Runnable(){
public void run(){
//do something in my display;
}
}
new Thread(){
public void run(){
while(keepPlayingAnimation){
try{
sleep(10);
}catch(Exception e){
}
h.post(updateDisplay);
}
}
}.start();
Just don't forget to set keepPlayingAnimation to false when you're done with this cause otherwise it will sit there running in the background for ever (or just about).
Take a look at Android Timer
It already has everything you need i guess. From ticking every 1 second to finish handly and so on.
Here is an example how to setup an TimerTask: setup
Not sure if you need such but i just remembered that i made this.

Replace an image in android/Java once a sound has stopped playing

I have an app which replaces an image and plays a sound on click of a button, What I am looking to do is revert back to the original image once the sound has stopped playing.
My button click listener:
public void onClick(View v) {
// Perform action on click
Context context = getApplicationContext();
CharSequence text = "Playing Theme";
int duration = Toast.LENGTH_SHORT;
//this is the replaced image while the sound is playing
imgSheep.setImageResource(R.drawable.replacedimage);
Toast.makeText(context, text, duration).show();
playSound(R.drawable.sound);
}
My Sound playing function:
//plays a sound file
private void playSound(int sFile) {
//set up MediaPlayer
final int medFile = sFile;
Thread thread = new Thread(new Runnable() {
public void run() {
playSound = MediaPlayer.create(getApplicationContext(), medFile);
playSound.start();
}
});
thread.start();
}
I know I can use a method like:
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
performOnEnd();
}
});
So can I have it like this:
playSound.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer playSound) {
imgSheep.setImageResource(R.drawable.originalimage);
}
});
Your problem is your thread. In playSound, you're starting a new thread which creates the media player and plays the sound. However you're setting the onCompletionListener in onCreate. There's a race condition there- if the new thread isn't schedules and doesn't run and set the mediaPlayer variable before you hit that line, you'll crash with a NullPointerError.
I suggest just losing the thread. MediaPlayer already will play in the background and not hang the UI thread.

Android SDK: Handler causing crash on some devices

I have a handler used to display images in a specified interval loop, on reaching the last image, it goes back to the first image which is the correct. However, i'm having problems with it as it's causing some devices to crash and makes the CPU usage go up significantly, i'm just wondering what is wrong with the code?
I instantiate it like the following at the top of the fragment:
final public static Handler handler = new Handler();
boolean isRunning = false;
Then in the onPostExecute part of an AsyncTask, I have this code:
#Override
protected void onPostExecute(Void v) {
if(!isRunning) {
Runnable runnable = new Runnable() {
#Override
public void run() {
anImageView.setVisibility(View.VISIBLE);
isRunning = true;
counter++;
//imageDownloader.download(data.get(i).getImageURL(), anmageView);
if(TabsViewPagerFragmentActivity.theImages !=null && TabsViewPagerFragmentActivity.theImages.size() > 0){
Bitmap anImage = TabsViewPagerFragmentActivity.theImages.get(i);
anImageView.setImageBitmap(anImage);
}
i++;
if(i>TabsViewPagerFragmentActivity.theImages.size()-1)
{
i=0;
}
handler.postDelayed(this, 1500);
}
};
handler.postDelayed(runnable, 0);
}
}
The above AsyncTask is called within the onCreate() method.
Secondly, I have a refresh button which re-downloads these images in order to get the latest ones as they change periodically. Therefore I have an onClick() event attached to the refresh button. This also works fine but here is the code which is called:
#Override
protected void onPostExecute(Void v) {
for(int i=0;i<data.size()-1;i++) {
Bitmap anImage = getBitmapFromURL(data.get(i).getImageURL());
theImagesRefreshed.add(anImage);
}
if(!isRunning) {
Runnable runnable = new Runnable() {
#Override
public void run() {
anImageView.setVisibility(View.VISIBLE);
isRunning = true;
counter++;
//imageDownloader.download(data.get(i).getImageURL(), anImageView);
if(theImagesRefreshed !=null && theImagesRefreshed.size() > 0){
Bitmap anImage = theImagesRefreshed.get(i);
anImageView.setImageBitmap(anImage);
}
i++;
if(i>theImagesRefreshed.size()-1)
{
i=0;
}
handler.postDelayed(this, 1500);
}
};
handler.postDelayed(runnable, 0);
}
}
I think that the handler is not setup right and is causing the performance issues. Can anyone see anything wrong with this code?
Thanks in advance!
You need to call Looper.prepare() while using handlers in threads .So write Looper.prepare() after you are creating instance of Runnable

Android: runOnUiThread does not always choose the right thread?

I've got an activity that keeps reading words to the user, and using onUtteranceCompleted with textTospeech to display something when the code is completed.
Inside onUtteranceCompleted I have this code to delay a function with a second:
Runnable task = new Runnable() {
public void run() {
//runs on ui
runOnUiThread(new Runnable() {
public void run() {
readWord();
}
});
}
};
worker.schedule(task, 1, TimeUnit.SECONDS);
This seems like it works well, but I think it is causing a problem.
When I rotate the screen of my phone (I guess this starts a new activity).
I hear some words being read in the background. I guess this is because of runOnUiThread() which makes the activity continue in the background.
How could I avoid 2 activities running ? I would prefer if I don't have to stop the screen from rotating on doing some weird patch!
Thank you
EDIT:
public void readWord() {
if (this.readingOnPause) {
return;
}
txtCurrentWord.setText(currentItem[1]);
this.hashAudio.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"word");
this.tts.setLanguage(Locale.US);
this.tts.speak(this.currentItem[1], TextToSpeech.QUEUE_FLUSH,this.hashAudio);
}
EDIT2:
instantiation of worker:
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
I would use a Handler instead of runOnUiThread().
For one thing, you're using a Thread that starts another Thread - why?
Secondly, if you create a simple Handler, it should kill itself on the rotate config change. IE:
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
// do your background or UI stuff
}
};
Then later use a Thread to call the handler, which will kick off whatever process you want to run on the UI thread:
new Thread() {
#Override
public void run() {
long timestamp = System.currentTimeMillis();
// thread blocks for your 1 second delay
while (System.currentTimeMillis() - timestamp <= 1000) {
// loop
}
handler.sendEmptyMessage(0);
}
}.start();
Ok so this is a fix I've come up with, if someone has a better solution, I'm listening.
I've added android:configChanges="keyboardHidden|orientation" inside the activity in the androidmanifest
2.
and then a function that is called when the screen is rotate:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setContentView(R.layout.streaming);
initializeUI(); //contains all the findViewByID etc...
}

Categories

Resources