I invest my few days but not getting the correct answer.
I am developing an application in which 12 tracks should executes parallely and they are, but my problem is that the start time is not same.
Actually my question is about to sync my 12 tracks. All the tracks start at one time, there should not be millisecond difference between all tracks. If there any difference occurs then tracks mixing can not be done perfectly.
My code which play all the tracks on button click.
mp1_track_a.start();
mp1_track_b.start();
mp2_track_a.start();
mp2_track_b.start();
mp3_track_a.start();
mp3_track_b.start();
mp4_track_a.start();
mp4_track_b.start();
mp5_track_a.start();
mp5_track_b.start();
mp6_track_a.start();
mp6_track_b.start();
It is not possible to have a prallele work as you ask with a single processor with one core (you must consider the less power android device). Even tough you have multiple thraeds, in the reallity they will work sequencially.
Related
I have an application, where I'll be having between 10-100 minimum phone numbers on which i want to periodically (let's say once a week) make a missed call (about 20-40 seconds of ringing and then hanging up, using all numbers "at the same time").I haven't tried any code yet but i already looked up how to make calls, missed calls etc. so I hope I know my way around this stuff a little bit.
Now you see if I'd do it by using simplest way, I'd have about one hour of my phone making these missed calls one after each other. Is there a way to make these calls somehow overlap each other (make them all at the same time)? Because I looked up that I can't make a phone call in background and since we are using activity to start a call, if I immediately start call after previous one, I suppose since it's an activity it's gonna pause.
As far as I know, it's impossible to call different phone numbers simultaneously on Android and mobile operators do not let you do it either. If you really want to do it, you should use an API like Twilio to make all calls. However, if you're using the API, your own phone numbers will be used for calls, not the mobile phone owner number.
And if you want to know how to do it all at once, this is what threads are invented for! Assuming you have 100 phone numbers, use 100 threads to simultaneously call and hang them via API.
I want to create a Reminder application with a similar idea like this, but this implementation only have at most one alarm/reminder at a time (ie. when an alarm starts, we can only configure another alarm after the most recently fired alarm has finished). Where do I start if I want to implement an application that can fire off multiple alarms?
My initial idea is like this:
For example my reminder application has 10 alarms for today, 20 alarms for tomorrow, etc (a certain number of alarms at a certain day). Should I just create a fixed amount of threads and process the alarms one day at a time? Or maybe I can reuse a timer and timer task but dynamically change the delay value? Please help shed some light for this.
Should I just create a fixed amount of threads and process the alarms
one day at a time?
No, just create a thread when it's needed, to be honest you can just have a clock checker within your application and a table or some data structure which holds type of alarm, time, whatever else there and create thread only when it's needed, it will play sound, wait for termination, etc. (basically user input) or just wait for it to die after it will be done with its work. You can have some kind of fixed iteration amount.
Where do I start if I want to implement an application that can fire
off multiple alarms?
Think about what kind of features you want to add, this will pretty much shape design of your application, design yourself some kind of manager of alarms, and system to create an alarm at a certain time.
Make sure you use FXML and force yourself to keep things organized, since your application can get bigger as you go on and add additional features.
Think about what data will be shared between threads since it's highly possible that you will not like to let threads/alarms overlap each other // for instance when they will have "remind in 5 minutes" //, or just terminate alarm that's about to cross into another one, if user doesn't terminate its alarm within that period and it just keeps going and another one is gonna be started, make sure that there is an entity or some higher controller class which takes care of this. If you don't wanna limit this to some minute interval maximum.
This application that you have in your mind is quite small and trivial you might be able to avoid most problems.
Keep it simple, this might be a best advice anyone can give you.
Bud it's just my personal take on this, these kind of questions will attract opinion based answers.
I have a bit of a unique issue. I'm collaborating with several other Computer Science Majors at my university on an Android Metronome app that allows two users with the app to sync metronomes. Right now, I can send the desired beats per minute of the metronome from one phone to the other and both phones will start to play their respective metronomes. Due to the latency of Bluetooth, the metronomes are only in sync about 20% of the time.
So, here's where the problem is. We're trying to make it so both of the metronomes will start at the exact same time. One way I've thought of doing this is once the first user presses the send button on their phone, a time stamp will be created. This time stamp will be exactly two seconds after the user presses the send button. The time stamp will then be sent to the second phone and the phone will utilize this time stamp to start it's metronome at the same exact time as the first phone. I've tried accomplishing this by putting UTC time in a while loop and then constantly checking the time, to no avail. Does anyone have any ideas as to how we can go about implementing this? I couldn't find any similar problem on StackOverflow, or any other website for that matter.
It's a bit late probably for your project, but if interested. This is a bit long, it's more like a case study.
If your project was about syncing two device clocks remotely, then this won't help you, but if it was about syncing the metronomes, then this may be a start.
First, getting millisecond accurate timings and callbacks in android using java is next to impossible, since android is not a real time system ( All sleep methods and timer might not execute on the exact millisecond you're expecting ). You may want to go with NDK for triggering your beat events, because native threads in android can achieve that, but if you're happy with your single metronome implementation, then that's good enough to read the rest of this .
Full disclosure :
I am the author of JAM the app referenced in the answer I just published a few days ago.
I ran into the same problem when developing the app.
And whether this answer will help you or not, depending on your situation.
and if you're going down the "clock sync" route ( Which I do plan on exploring to improve my app down the road with more features ) but the solution that I found did not involves time sync.
I will mention my approach and then state what I found as advantages/disadvantages for each of the methods .
Instead of syncing clocks ( A very difficult problem, especially for what the app was supposed to do , and that is trigger ticks on multiple devices at the "same" time ) I opted for a subscribe/publish model, where multiple devices can "subscribe" to a host device via bluetooth, and the host device controls and "publishes" the metronome beats.
most times sending a few hundred bytes ( Enough information about each beat ) via bluetooth takes less than 2ms , it could spike up to 10 and sometimes even 30, but that rarely seems to happen .
That approach took care of the syncing problem, now I can send beat events from one device to another in 2ms , and if some sort of delay happened, it will self correct once the interference is gone, because the other event comes in time.
However that approach does require constant connection, you can't start and separate the devices later on, but it's much easier to implement.
Now the interesting part, the problem I never thought of when starting this, and that was the Android 10 millisecond audio path latency problem. I'm not affiliated with superpowered, but I did use their engine in my app to deal with that problem .
To summarize :
Metronome sync over bluetooth in Android has two issues :
1 - triggering beat events simultaneously ( or within acceptable latency )
2 - The beat event should trigger sound events with minimal delay after the beat event is received
#1 is related to the original question, #2 I thought was an important addition to the original problem this question is asking.
I would use (S)NTP to check the clock drift to a public time server. After that I would just send the interval and a start time and let the devices calculate the interval individually. So you just need to sync changes and relay on the clocks of the individual devices. Together with the now known clock drift you can fix this error.
To give some background information, I'm currently working on a Java coded pinball game. I'm keeping it in an MVC design model. It has a fairly realistic physics system that allows it to work collisions, gravity, friction etc. The system runs on a 20 FPS system right now.
The problem I'm having is that the physics loop that checks for collisions in the system works by running a method that using the current velocity of the ball calculates the time until the next collision. The most effective way for this to work would obviously be to keep running the check to account for the movement of the ball between checks to get it as accurate as possible, and if the time until collision is less than the time until the next check, then carry out the collision.
However, right now the system I am working with can only run the loop 20 times per second, which does not provide as accurate results as I would like, particularly during times of high acceleration, such as at ball launch.
The timer loop that I use is in the controller section of the MVC, and places a call to the physics section, located within the model. I can pass in the time remaining at the time the method is called in the controller, which the physics system can use, however I don't know how to run the loop multiple times while still tracking the remaining time before the next screen refresh?
Ideally I would like to run this at least 10 times per screen refresh. If anybody needs any more information please just ask.
Thanks for any help.
So the actual problem is that you do not know when the the collision will happen and when the next frame update is?
Shouldnt these be seperate running tasks? One thread that manages the collision detection and one that does the updating? each thread can run on its own interval (Timer.addTask(...)) and they should propebly be synchronized so colission/location updates are not performed when the render thread is executed.
Hope this answers your question.
Regards, Rob.
I've written an app which processes a lot of data on startup; first startup time of the app is rather slow, but so are subsequent launches.
I previously thought that the relatively un-responsive startup times (~7 seconds) were due to data processing on first load - a bit of debugging suggested about 2 seconds to cold-load. So I redesigned my data structures so they're easier to serialize to persistent storage in between launches (so it caches stuff) and it now saves state when the app finishes in about 20ms, and loading time is about 10-20ms. Much better. (p.s. it's not using the built-in Java serialization, as the state files are now so simple it's easier to save in a plain, human-readable (and editable) text file - it's still pretty quick at reloading from this though!)
However, I'm still finding that the app startup time is about 5 seconds - I've looked in aLogCat which has an entry:
I/ActivityManager(...) blah blah blah: +5s193ms
So more than 5 seconds to load a relatively simple layout (a few nested LinearLayouts, and a few TextViews - nothing special, and no images) - admittedly that time goes down with subsequent launches (I close the app and open it again, it's ~700ms; rinse and repeat for similar results).
Soooo my question is: how can I improve the app loading times? I'm happy that first load takes a little while to process everything - that's fine. But is there a way I can keep the thing in memory, or at least get it into a restorable state so whatever ActivityManager is doing doesn't take so long? I'd like it to be something I can open (probably bound to long-hold search or similar), do whatever, and close it again - thus it needs to open quickly!
Thanks in advance :)
Seb
Run your heavy-duty start-up work in a background thread that you launch in onCreate. (An AsyncTask is good for this.) Design your UI to show something meaningful (at least an indeterminate progress bar) when there is no data. When the data is loaded, post a Runnable (or implement onPostExecute in your AsyncTask) to update the UI to the real thing.
As Ted suggests, do only the minimum on the UI thread. Five seconds to load the layout is too much, which means that there is some start up method that requires a long time (maybe querying databases, or accessing the net, to populate a long ListView?).
If that processing is absolutely required, then use a splash screen (with a "Please wait while loading data" or similar label).
Divide the work using Asyntasks classes. If you have some data which is always the same store it in database for example to have quickest access next time you open the application, and update that content from time to time.