Showing posts with label Code Example. Show all posts
Showing posts with label Code Example. Show all posts

Monday, 7 September 2015

MediaPlayer experiments

When working with the media player there are some little things I have found that you need to keep an eye out for. The first is that the MediaPlayer object is quite happy to continue playing your media track after you have closed the app itself. This is an example of how important it is to release all resources and to monitor which ones persist in the memory of the device! While bumming around on the android developer website, I started looking into the billing API and found another example in the Google Payments "Binding" when an in-app purchase is attempted. When an app closes, if that binding is not released, it can use up system resources and degrade performance.

My experiments with the media player began with finding a question on the learnandroid subreddit, which asked about pausing media with a touchevent. From my reading I could see that the poster had not prepared the media track after stopping it, something which is unique to that circumstance but not the pause method. The poster also had a bit of confusion about the touch event itself and where to trigger the media playing. After posting a quick suggestion I decided that I should try to implement a music player in a test app too to get to grips with the little things that might be confusing for a beginner.

Following on from my blank screen SurfaceView test, I used VLC to convert a ripped mp3 into the OGGVorbis format with reduced sound quality (to save on memory) and added that into a new assets folder. From my previous experiemnts with Soundpool (An app with pikachu on it that says pikachu when you touch the screen, and plays a computer booting up sfx from pokemon firered on launch), I knew that sound files can be retrieved using the AssetManager in android. I made the first mistake in not putting the new directory in the right place in the file structure, but that didn't become apparent until it was fully set up.

My first set up was to have all of the media player implemented in the SurfaceView class itself, which is a Runnable in a class called Screen. The implementation included instantiating an asset manager, trying to get the file i wanted an AssetFileDescriptor which I could pass into a new instance of MediaPlayer. The process itself was all set up and the data source set and media started, but the app itself didn't work. After adding some Log notes and an exception message for the try-catch block I found that the asset manager wasn't retrieving the files in the folder, and looked back to my original pikachu app to find the structure difference. 

Once the file was able to load and play I ran the app and lo and behold, the music played, (Nine Inchnails- Satellites for those who need to know!). I was elated, then I closed the app and hmm.. still music.
I knew this had something to do with releasing the resources and the activity life cycle so I started to look there for ways to control my music. I started with onStop, onPause and onResume methods inside my SurfaceView which were called from the main activity, and implemented checks for if the media player was valid and playing etc, and stopped the music and restarted it as I expected. This did not work. The app was happy enough to stop the music but not to restart it when the app was loaded up again. I understood that the app was saved in temporary memory when the app was minimized or obscured, so the app was going through the onPause method, but I didn't realize the onStop method was being called also!

Going back I was able to debug my app and place appropriate boolean flags to control the music playing. In the onPause method, the music is (if it is not already) paused, and on resume the music is (if not null such as on initial bootup of the app) started again. The call to onStop was moved into an if block which checked that the app was not being closed fully (isFinishing()) and if so, stopped playback and also released the media player.
With my experiment finally performing the way I wanted it to, I was finally able to begin abstracting the audio into a seperate class. I created an Audio class that could handle music using static methods, which would mean it is accessible throughout my app. Instead of using a default constructor to set up the Audio environment, I opted instead to use static methods that only initialize variables if they are going to be used. I currently have a method that is needed to pass in a context for use in attaining the assets, but later on I will access this statically from a core class that handles the game.

The Audio class also has onPause, onResume and onStop methods which can be called to ensure that any active media, be it sound FX from the soundPool or music from MediaPlayer are paused and resumed correctly, and destroyed on closing the app.

I am really quite happy with the progress I am making in understanding these (albeit simple) aspects of app development. I want to be able to say not only that I understand the theory, but that I can put code on paper to describe the processes and show that I understand the way these systems work together on a more holistic level.

The code for my Audio and Screen app are linked below for viewing.

MainActivity Class
Audio Class
SurfaceView Class

Saturday, 22 November 2014

Github, the Git and The Table State

At the suggestion of a guest at my own place of work, I got myself looking at Git this week. While I admit so far I have been using it as a GUI based, upload my work when I fancy it machine with no branches, I do plan to make fuller use of it soon. While in the super early stages of my concept, I have no need of branches as I am literally treating my gamedev environment as a bouncy house for programming. I'm having fun creating and solving problems and the future is still hazy as to the end product. While I would love for the program to be a success, I have not given myself any promises that this will be a masterpiece. Should my interest head in a different direction based on what I learn during the course of this work, I'm happy to let it go and move on.

While unfinished work is probably the biggest time waste for many programmers, I have taken the view that any time spent working on my project is constructive. Any ideas that make it into the program are successes and the failed ones are lessons.

So, this week I have been working a lot IRL so my time has been spent planning the Table Service aspect of my game. Working with time in Java was an initial hurdle but didn't require too much googling to ensure I was getting the numbers I needed in the format that worked for me. Each guest will be able to generate a set of times which state how long an aspect of their stay will take. John might take two minutes to eat, but Sally might take twenty five. Using this knowledge, the game is able to check for an "all ready" like state, by comparing the state of each guest and acting when all of them match. Using this system requires a set of enum types for the Guest and also for the Table to use as state markers which can be looked at by the other Entities. States in this way happen to be far more effective for the long list of possible states than boolean values had been, as rather than turning one value to true and another to false, the overall state simply switches. This makes a list of instructions much more safe to run as the state can only be one at a time, and no accidentally crosslined booleans will throw me off.


 if(!isActive){  
return;
}
elapsedTime = timeSat + System.currentTimeMillis();

if(state == guestState.sat){
if(startOrderTimer <= 0) startOrderTimer = System.currentTimeMillis();
if(startDrinkOrderTimer <= 0){
startDrinkOrderTimer = System.currentTimeMillis();
}
if(System.currentTimeMillis() - startDrinkOrderTimer > drinkOrderTime){
state = guestState.drinkOrderReady;
}
} else {
return;
}

if(state == guestState.drinkOrderReady){
// when Orders system implemented, fill in order creation here
}


if(state == guestState.drinkOrderTaken){
if(startOrderTimer <= 0){
startOrderTimer = System.currentTimeMillis();
}
if(System.currentTimeMillis() - startOrderTimer >= orderTime){
state = guestState.foodOrderReady;
}
}
In this early example of a guests update method you can see that the state is modified when criteria are met such as an elapsed time and manage progression through a typical service by acting as flags that the table can look for in each guest to create a Task that can then be acted upon by a Staff entity.

The table itself is able to scan through any number of occupying guests in order to check that all guests are in the same state, as shown here.
 private void getGuestStates() {  
if(guests[0].getState() == guestState.sat){
return;
}
Guest.guestState[] states = new Guest.guestState[guests.length];
for (int i = 0; i < guests.length; i++) {
states[i] = guests[i].getState();
}

Guest.guestState temp1 = guests[0].getState();
for (Guest.guestState state : states) {
Guest.guestState temp = state;
if (temp1 == temp) {
temp1 = temp;
} else {
return;
}

System.out.println("Table has determined that all guests' states match: " + state);
}
}
The initial state of guestState.sat will progress from within the Guest so if this state is in place then we save cpu time by exiting the getGuestStates() method. Checking against a fixed array index guests[0] is not a problem as a guests array is not able to have fewer than 1 entry, as 1 is the minimum number of guests in a group (which seems redundant to have a group of 1, but this is simply a container for the guests to proceed into the restaurant queuing system).

Once the basic framework for the states is finished I will make all new table placeholder sprites to help show the state visually. This may be a layered approach using the base table and then an "items on table" type layer such as is used in RPGMaker, but that will come in its own time. I have 3 days booked off this coming week and hope to spend a good chunk of them coding. Until then!