Monday, May 6, 2013

Media Player

This document represents the description of a mobile application that is able to act like a media player. The main purpose of the application is to be able to embed an audio file then play it and show the position of the song. The application provides two buttons and a seek bar. The buttons are used for starting, pausing and stopping the song and the seek bar was used for jumping to a specific location of the song.


The program start by creating a MediaPlayer object together with the two buttons and the seek bar. The maximum value of the seek bar is set to the length of the song.

For the Media Player to respond, setOnSeekBarChangeListener was address to the seek bar and a setOnClickListener was added to the buttons.

One of the problems that we encountered in the setOnSeekBarChangeListener was that the listener on the seek bar was not responding to the user changes. The solution was to use the boolean variable fromUser to check if the action is done by the user or not.
       

   seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   
   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
    
   }
   
   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
    
   }
   
   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
    if (fromUser)
     musicService.mp.seekTo(seekBar.getProgress());
   }
  });

Another problem that we faced was that we could not manage to make the seek bar work without locking the user interface. The solution was to use a handler to post a delay on a runnable object Runnable r = new Runnable() that causes the Runnable r 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.

       

playButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    if (stopped) {
     seekBar.setMax(musicService.mp.getDuration());
     handler.postDelayed(r, 500);
     stopped = false;
    }
    if (musicService.isPlaying()) {
     musicService.playPause();
     playButton.setText("Play");
    } else {
     musicService.playPause();
     playButton.setText("Pause");
    }
   }
  });
After you write bindService(i, servConn, Context.BIND_AUTO_CREATE); android takes some time to process the binding request and you can not use the Service Object immediately after the bindService() method call


Download link: http://ge.tt/4LDRTtf/v/0

Sunday, April 21, 2013

Project ideas


The application is about being able to locate your friends on the map depending on your position.
The application will look for your location via the GPS chip and then receive the other coordinates from your friends and place them on the map as well.
The application might have the feature of tagging some places on the map and share them on your friends’ maps.

Thursday, April 18, 2013

Activity


There are three activities and on each of them there are four buttons. When you are in one activity you can go to either one of the other ones. For going to another activity you have two buttons (one that goes without any flags and the other one that has the FLAG_ACTIVITY_REORDER_TO_FRONT).

Each time an activity is created, for intents are created (two for each of the other activities without any flags and two with the flags). When a button is pressed, it will start an activity with the proper intent depending on the text of the button.


  

  Button bSecondWithFlag = (Button) findViewById(R.id.button1);
  Button bThirdWithFlag = (Button) findViewById(R.id.button2);
  Button bSecondWithoutFlag = (Button) findViewById(R.id.button3);
  Button bThirdWithoutFlag = (Button) findViewById(R.id.button4);
  final Intent i2withFlag = new Intent(this, SecondActivity.class);
  i2withFlag.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  final Intent i3withFlag = new Intent(this, ThirdActivity.class);
  i3withFlag.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  final Intent i2withoutFlag = new Intent(this, SecondActivity.class);
  final Intent i3withoutFlag = new Intent(this, ThirdActivity.class);
  bSecondWithoutFlag.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
   startActivity(i2withoutFlag);
   }
  });
  bSecondWithFlag.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
   startActivity(i2withFlag);
   }
  });


There is the drawing with the activities started without any flags:

Description:

Every time an activity is created without a flag, another instance of that activity is created and put on top of the stack. All the other activities that were on the stack are just pushed back one position.



There is the picture with the activity stack when the activities were started with the flag:

Description:

Each time you start an activity with the FLAG_ACTIVITY_REORDER_TO_FRONT it will look through the stack to find an activity that is of the same type. If it finds one, it will resume that one and move it to the top of the stack. If it doesn't find any activity of that type, it will just create one and place it on top of the stack.



The difference between the two examples:
In the first case, the application will just add new instances of the activities each time you start another one without the flag, while in the second case it can also move one activity that was already started to the top and you can use that one without creating a new one.

Optional 1:

After we created some activities without the flag, it is obvious that in the stack could be more activities that are of the same type. When we try to start one more time one activity that is already on the stack but this time with the flag, it will look through the stack and move to the top the last instance of that activity that was pushed to the stack.

Optional 2:

If the same activity was created before, it will invoke the last instance of that activity that is on the stack (the last activity that was invoked before).
If it is the first time that this type of activity is created, it will just create a new instance of the activity.




Monday, February 25, 2013

Android Media Player


This document represents the description of a mobile application that is able to act like a media player. The main purpose of the application is to be able to embed an audio file and then play it. The application provides two buttons and a seek bar. The buttons are used for starting, pausing and stopping the song and the seek bar was used for jumping to a specific location of the song.



The program start by creating a MediaPlayer object together with the two buttons and the seek bar. The maximum value of the seek bar is set to the length of the song.

For the buttons to respond to the user input, an OnClickListener event was set for both of them. Each button has a different behaviour depending on its functionality. For example, the Stop button is supposed to call the stop method for the MediaPlayer object when clicked.

One problem encountered during this method was that after the MediaPlayer was stopped, the song was not able to start any more  The solution that was found for this problem implies calling a prepare method for the MediaPlayer object after it was stopped. After the Stop button is pressed, the status of the Play/Pause button is set to the Play status. Also, the seek bar is reset to the beginning.


The Play/Pause button is used either to start or to pause the song, depending on the state of the MediaPlayer object. If it is in the playing state, it changes the state of the button to Pause and vice-versa.




The functionality of the seek bar is to allow the user to jump to a specific time of the song.  This way, the OnSeekBarChangeListener of the seek bar calls the seekTo method of the MediaPlayer object with the specific progress.




The thread that allows the user to see the actual progress of the song on the seek bar was not implemented yet, but it will be available in future versions.

Source Code at http://ge.tt/4iNVuUZ/v/0?c