GKTurnBasedMatch in a new game can i wait until another player has accepted my invitation? - iphone

I'm writing the multiplayer part of a turn based game,i'm using game center to manage the matches. Starting a new game with GKTurnBasedMatch do not send immediatly the invite notification to the other player. To send the notification i have to made my move and end first turn... Can i modify this behaviour? I wanna made my first turn only if the other player has already accept my invitation.

One way of appearing to do this would be to add a value in the NSData, say an int, that remembers what turn number it is. Start your game, then end your turn immediately without showing anything to the user. All it does is increments the turn number. When the other player accepts, if the turn number says its the second turn, increment the turn number again, and end their turn. Continue this end-increment-end pattern until the first player receives the turn back, then start playing normally, with all players having accepted the invitations and officially joined the game.
I do not recommend doing this, as this can cause a significant delay in the play time for the players who are ready to begin. They may expect it from your game eventually, but the standard pattern has a shorter apparent waiting time for the players to begin playing.

You can check the status property of the other participant who is of class GKTurnBasedParticipant. If its status continues to be GKTurnBasedParticipantStatusInvited, forbid yourself from opening the game controller and display an alert.
GKTurnBasedParticipant documentation

Related

Playing same audio on two players simultaneously using just_audio and just_audio_background

I am working on an app that plays audiobooks, and when you click a book in your list a full window player will start playing the book, however when you close/minimize it, I want a miniplayer to show right about the navbar in the bottom. This player has to continue playing from where the first player "stopped" until the user decides to pause.
Im unsure if this can be achieved without having to have both play the audio at the same time? Reason is that when the Fullscreen is closed, player.Dispose is called.
I currently have a function which saves the position of the player and stores it in shared pref, it however cuts the seconds of, so even if I called that in the miniplayer, it wouldnt continue from the same position.
Im new to flutter/dart btw, with only a few months experience. Thanks in advance
You should use a single player and then have two player "widgets" displaying the state of that same player. That way, you don't need to try to stop and start the player - the player can continue playing the whole time while the widgets are the only things changing.
If you're a beginner, the simplest way to define a single player that can be referenced from everywhere is to define it in a global variable, outside of any class declarations (for something more advanced, read about the service locator pattern):
final player = AudioPlayer();
Then let's say you have two widgets to view the state of the same player: FullPlayerWidget and MiniPlayerWidget. They can both reference the same player instance defined above.
Finally, your logic error is that you are calling dispose when minimising the full player widget. You shouldn't do that unless you want the audiobook to stop playing permanently. You don't want it to stop permanently, and you don't even want it to stop at all!
Actually, the only thing you want to happen when minimising is a visual thing. You want to hide the full widget and display the mini widget. But on the audio side, you want the player itself to be unaffected, continuing to play audio as if nothing had happened. Therefore, in this event you should in fact not call any methods on the player.
The best thing you can do as a beginner is to study lots of examples. E.g. podcastproject (although its dependencies are a bit out of date now).

Flutter Implementing pause and play..... Streaming Music

I've tried multiple flutter plug-ins to stream and play and pause music but as of currently, I haven't yet figured out a simple way to implement play and pause, whilst having the ability to avoid music overlapping, I want so that when the current song is playing, if I were to press play on new music track, the current song playing will stop and play the newly pressed song. I've tried flutter_sound, audio player, audio file player, and flute music player but I can't seem to figure out how to do this.
If it is possible either using one of these or a new plug-in, is there a way to implement what I have just described.
The library you seek is more of a multi-track player than a player. Which does not currently exists AFAIK.
The simplest way is to act according to the status of the tracks (which should be given by the player).
Roughly, what I suggest: have something that keeps the status (e.g. paused, playing, completed) of each track. (e.g. List of Tracks) When you press play on track 2 or track 1 finishes playing, you set to 'completed' (or as you wish) the track 1 and set to 'playing' the track 2.
Have a listener on the status of the current track and voila (for autoplay). When the track completes, notify the listener to call a function to start the next one on your list. When you press another track, set to 'completed' the current track and to 'playing' the track you pressed, then notify listeners.
Any library letting you know the status of a track is good enough for your purpose.

Game Center multiplayer, very long connection timeout

I've implemented a 2-player turnbased game using Game Center matchmaking/multiplayer, in other words using GKMatch. It works well. There is just one issue: when one player disconnects (ie. quits the app), it takes a while before the other players gets notified of this (with match:player:didChangeState:). Sometimes even 30 seconds.
That is not a good UX. I don't want a player to wait for nothing if the other player already disconnected 30 seconds ago.
2 questions:
Is this an issue with the sandbox server only?
If not, what can I do to shorten/eliminate this waiting period?
I did not rely on GC's notifyPlayer.. Everything from DC to quit game to receive calls i handle by my own codes..
for my code, when player A presses home button, means in app delegate's applicationDidEnterBackground, a data is sent to the player B to inform that player A has quit the game. This way, when the player A quits, player B will immediately know that player A has quit.. Rather than waiting for gamecenter to notify you..

Multitasking: Stop Background Audio at Specific Time

I am developing an iphone app which uses background audio (on an infinite loop) to continue playing after the app has entered the background.
My problem is I want to implement a "sleep timer" which stops playback after a specified period of time.
Is this possible? I have spent an hour looking for a method to do this with no avail.
EDIT: My current thought is to use a lower level API, the Audio Queue Services, and manually re-fill the queue with another instance of the loop during the AudioQueueOutputCallback. If the timer has expired I do not fill the loop. I'm assuming this should work since the documentation says audio callbacks are still fired when an app is playing multitasking background audio. Can anyone think of a better way or a reason why this wouldn't work?
While you queue sound data on the background your app remains fully functional and running as if it was in the foreground (well almost), so yes, you should just write a timer that stops the playback at a given time and it will be fired as expected.
Now to the second question: once you stop queueing things up, your app will be "frozen" until the user manually brings it to the foreground... So what you should do is start queueing audio data from the second file before the first one is done playing, and if you DO need to pause or stop, maybe a solution is to play 0 bytes (silence)?
I'm not actually sure this would be allowed in the App Store. An app is not allowed to execute at all in the background, with the exception of VoIP apps and push notifications.

UIAccelerometer is Shaking

I want a functionality in which i want to detect if my device is being shaked.The problem is i can detect the shake with didAccelerate method of UIAcceleratorDelegate , but i dont know how to detect if the device is still shaking. I want to play an audio file when the user shakes the device for first time,i have to check if the user is still shaking the device while playing the 1st audio file,if it is still being shaked, then i have to play another file.
See the sample project GLPaint from Apple which was found by visiting http://developer.apple.com/iphone and entering "shake" in the search box. Developer account not required.
You might consider writing a Method that runs in a separate thread that polls wether your device is being shaken every now and then and fire events that you handle somewhere else in your code (or instead of that, handle whatever you want to handle within the threads context itself, even tough i would not recommend doing that).
You just have to make sure, that your "shake-detektor"-thread exits at some point in time, you probably want to do that when the second audio file stopped playing. So your loop could test on that condition.
Hope I could help a bit.