I am working on an audio stream android app and I parsed JSON object from a server to a TextView to display 'now Playing' for the song name and artist. So when the play button is clicked, the song name playing artist is displayed to the user. The problem is that I want this automatically loaded to the app view when JSON URL link is updated from the server. I don't want the user pressing pause and play to update the view from the app. How do I go about this because I don't want the user restarting the service each time a new song isPlaying to get song information.
You can either poll server in short intervals to check if song changed or open socket connection to server to make possible server initiating communication to device.
First approach in simplest form is a very bad practice, as it puts strain on both device and server to check it often enough.
However there is different way to use it, called long polling. With this, you send request to the server, and server does not respond immediately, but holds connection open until it has something to say. After getting reply instantaneously new request is created to make sure no delay is made by it.
The best approach is opening a socket connection, but not every server and program support it.
You can try libraries like SignalR (this one is for .NET mostly, but it's the first one that came to my mind) that choose which approach is the best and takes care of holding connection, reconnecting etc.
Are you fetching this JSON metadata every time the song is played? If so, that doesn't sound like a good idea. The ideal would be to add song metadata when adding a song to the playlist, then either update it periodically (once a couple of days perhaps) and save that information into a SQLite database for later retrieval.
Related
I'm creating an app in which multiple devices can connect to each other in a LAN and play songs from each other's device.
Right now, when device A requests a song from device B, B sends the song file back to A over a socket. Once the song finishes downloading, A starts playing it using MediaPlayer.
Now the problem with this is that even on a high speed LAN, it can take a couple of seconds to finish fetching the song, so there's a noticeable delay between selecting a song and it's playback actually starting.
I want to find a way to play a song while it's still being downloaded. Here are some possible solutions I found, but wasn't able to explore for certain reasons:
In the MediaPlayer documentation, I noticed a constructor that takes a MediaDataSource implementation allowing me to manually return parts of the media file whenever the MediaPlayer requires it so I could hold off returning a byte until it finishes downloading (if it hasn't finished downloading already), effectively acting like a "buffering" action. It seemed like a solution but unfortunately, my app's minSdk is set to 16 and MediaDataSource is for 23 and above only, so I couldn't try it out.
One option was to use MediaPlayer's setDataSource(FileDescriptor fd, long offset, long length) method. This would allow me to tell the MediaPlayer to only play up to a certain byte of the song. That way, I could wait for more parts of the file (or the entire file) to become available, and then use setNextMediaPlayer() and pass in a new MediaPlayer object that prepares the entire song and is made to seek up to the point where the previous media player object will stop playing so that there's a seamless transition.
But there's another problem with this. I need to be able to calculate the millisecond position that would be reached at that last specified byte of the first incomplete media player object. Otherwise I wouldn't know what position to seek the next media player object to in order to get a seamless transition. This calculation seems impossible for lossy formats.
I don't really know if this option will work or not, I'm just making assumptions. I noticed that setDataSource() takes a Uri. If that Uri points to a file on the phone, the media player just loads the entire file. But if the Uri points to an audio file on the internet that needs to be streamed, it figures that out on it's own and it handles all the details of downloading and buffering. So what I want to know is, is it possible to expose the song on device B as a Uri to device A so that media player just treats it as if it's a song on the internet? All this time I was using sockets and manually copying a file from one device to another so I have no idea how this would work. Could anyone explain if this is possible?
There's actually a reason why I haven't been exploring ways to "stream" a song from one device to another. That's because I want to song to be cached on device B so that later if I switch to another song and then back the previously played song from device A, it shouldn't have to stream it again. It should just play the cached copy.
Finally, I came across ExoPlayer. It seems to provide a large amount of customization. It seems like I could make custom implementations of it's classes to handle all the buffering. But the documentation and examples are few and far too complicated for me. I have no idea how to pull it off. Is this solution too complex for what I want to achieve?
Hope someone can point me in the right direction here.
Ended up using an answer from here:
MediaPlayer stutters at start of mp3 playback
This solution sets up a local server that the MediaPlayer can use to stream the file. The server only sends bytes to the MediaPlayer while those bytes are available. Otherwise, it blocks until more bytes are available. Thus the MediaPlayer buffers it as if it were a file from a web server.
I took the same class provided in that answer and just tweaked it to fit my situation.
I am making a game for Android/iOS. I have recently got daily quest to work nicely, the only problem is I can't figure out when I should save quest progress.
I don't want to make a servercall everytime you do something in the game that could effect quest progress.
Say for example your quest is "win 3 games", I was thinking of just putting quest progress in preferences until you completed the quest, but that can lead to a bunch of unwanted behaviour if you exit the game when your progress is for example 2/3.
Is there a smart way to go about this or do I just have to make a servercall and save directly into db every time you do something that can effect a daily quest?
As far as I know there is no way to do it when the user exits the app?
Is there a smart way to go about this or do I just have to make a
servercall and save directly into db every time you do something that
can effect a daily quest?
iQue, make a server call every time and validate the data they are sending you (did they really achieve what they're claiming?), users can root the device or modify the game client, so this is the only correct way to do it.
As far as I know there is no way to do it when the user exits the app?
If you want to do anything when the app is exited on Libgdx, there is again only one correct (in the spirit of Libgdx) way to do it and it's not mentioned in the other answers, as you can write this cross-platform without delving into Android/iOS specifics: override the dispose method of your main game class (ApplicationListener).
Is there a smart way to go about this or do I just have to make a
servercall and save directly into db every time you do something that
can effect a daily quest?
I'd choose server call.
As far as I know there is no way to do it when the user exits the app?
There are onPause, onDestroy methods in android. You can use SharedPreferences and when user exit the app, delete that data.
But whatever you do, never trust the client data. Always try to validate data in server.
I would save it as soon as something changes. Your server will probably be able to handle this and you don't need to mess around with platform specific stuff.
If you really want to avoid this for some reason you would have to look at the platform specific ways to execute code just before the app exits. On iOS this seems to be applicationDidEnterBackground() on Android you can use onDestroy() .
I habe a app for event informations an app which sends pushmessages. Now I want to give the user the option to disable the messages for a specific time (one hour, one day ...). I know how to dissable it compleatly, but how I can set it like I want (one hour...) even if the app is closed?
Well, the simple solution would be to check if there is a time restriction is present in the same place where you notify user for event information. If there is a time restriction dont show the notification else show it
I am a DBA, not a web developer. I am trying to build a complex website with Joomla. The user page needs to be interactive without refresh. I am not sure I am using the right words, but here goes.
Assume a user is logged in and a session is started - and we have that data along with the users IP address.
The database is crunching things from other users too, just like this one. As the database works, it generates information to be displayed on EACH users screen in real-time, without the user clicking, without screen refresh, and without the web-page code polling the server at intervals. In fact the user may click a different action from their screen simultaneously, so the user screen cannot be sitting waiting for a reply or polling at predefined intervals. It is basically receiving and transmitting "virutally" at the same time. If this is possible to do, a single piece of code would work and results could be deciphered for the right screen entry point (several boxes).
What can be used to do this? Thanks, Bruce
off cource its possible. make ajax call to link like
"index.php?controller=myController"
and in myController set header of either xml or json and prepare response of AJAX there. and at end of code write $app->close(); to avoide rendering of other modules and content
I have something of an abstract question regarding managing live feeds/polling on web sites.
I am creating a web app (built on Java/Spring/Hibernate) and on the user's home page I want a live feed of the latest activity from all the members of there team, and I am trying to work out the best way to handle this query on the server side.
The brute force way would be to load the current users list of team mates, and then iterate through each of his team mates, loading their latest conversations/file uploads/etc, and then merging all this activity in to a single list sorted by timestamp and returning that (lets say for sake of example that we just return the top 10 latest activity for the feed).
However, that seems very un-performant, especially as this operation would need to be done regularly (depending on the polling interval).
I have also considered making all the potential activities (conversations/status updates/uploads) as extending an Activity class and then just having a direct SQL/JPQL query in a DAO that selects all the latest activity from a set of users to be returned, but concerned that might bypass the caching and continued access to the DB would also reduce performance.
Has anyone handled this type of problem before? any one know what a good approach is?
Thanks!
This is an old one now, but here is what i did for this:
All tasks that should appear on a live wall extend Activity (this was already the case)
Created a new Notification object, the Notification had a link to the underlying Activity and a link to a user (who was being notified).
Created a pre-persist hook for Activity that created a Notification object for the Activity being persisted - it did this for every user that was interested (all users following the user that was persisting the Activity)
For the moment, Notifications are persisted/retrieved to the DB - possibly not scalable to very high volumes, but the approach I think supports moving to a Queue based system (such as LinkedIn's Kafka queue library which is designed exactly for this purpose). As it is per-user, it also provides the option to have a read/unread notification flag for significant notifications.