Assignment Listing

Everything
FLots
Assignment 1+2:
Songs need to be added, cannot share due to copyrights.
Assignment 0:
File does not exist, but text to do it is the lowest post.

Thursday, May 8, 2008

P.S. for the last post, I claim having the first Hello World in the group.

Anyways, this is a tad bit for a bigger, more complex, and actually useful application for Android. As such, there is a lot of non-Java code that must be edited, so for the sake of me being able to write a how-to guide, I am assuming you are using eclipse with the default settings of where things are at.

Now, to begin with, this is a music player, so as such, you need to pick a song to be played. What ever the song is, make a copy of the actual file and rename the song to be mysong.mp3 (I don't know if this code supports non-.mp3's as I have not tested them, so use one at you own risk (and send me the results)).

Now, create a new android project.
set the path to be musicPackage.myFirstMusicPlayer
set the activity name (and thus the name of the java file you are editing) to be MyFirstMusicPlayer.

Now, before we do any coding, you need to find the 'res' folder. Open it up and make a new folder named 'raw'. In the raw folder, drag and drop mysong.mp3.

Next, open up main.xml in the text editor form (right click and select openwith)(I don't actually know how to use the xml editor, so if anyone minds passing me a tutorial...).

Add this code:


Button android:id="@+id/cmd_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play the music !!!"
/>



Also note that you need to add a < before the 'Button', but if I add it here, it will create a big button and I don't want that right now.

right above the last line of code (at least, for me it was the last line of code) which reads:





Now time for the java code.
Here it is:
(P.S. just copy and past it, I don't know how to fix it from running off the screen and still keep it styled, but copying and pasting will get you all the code, even the part off the screen.)


package musicPackage.myFirstMusicPlayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MyFirstMusicPlayer extends Activity
{
/** Called when the activity is first created. */
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);

Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
cmd_play.setOnClickListener(new OnClickListener()
{

public void onClick(View arg0)
{
MediaPlayer mp = MediaPlayer.create(MyFirstMusicPlayer.this ,R.raw.mysong);
try
{
mp.prepare();
}
catch(Exception exc)
{
exc.printStackTrace();
}
mp.start();
// i.e. react on the end of the music-file:
mp.setOnCompletionListener(new OnCompletionListener()
{

public void onCompletion(MediaPlayer arg0)
{
}
});
}
});
}
}



Now, one last thing to do before running.

At the top of the screen, go to Run->Open Run Dialog.
Click on the 'target' tag, and add "-useaudio" into the field named 'Additional Emulator Command Line Options'.

Now, there will be some R. stuff showing in red. This code will be auto generated upon running (one thing I like to know how they do it).

So, now run the program. The emulator should pop up and a button clearly labeled should be there. Click it once (unless you want to here multiple tracks of your music playing with a little lag between each one).

No comments: