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, February 5, 2009

Assignment 1+2

The only item of interest in this project which I would think any others would have is the music player. To use it, songs need to be added to a *yourproject*/res/raw. As of now, I know it supports mp3 and m4a. One needs to import android.media.MediaPlayer, as well as other desired imports (such as android.media.MediaPlayer.OnCompletionListener).

Besides for this, one just needs to create and init a MediaPlayer object, and then tell it start. As mentioned, listeners can be attached to listen for it to end. Others may be possible.

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).

Friday, April 18, 2008

Let's See If I Can Get This Right

Name:
HelloAndroid
Purpose:
This code is the Hello World which is to be used to test if the emulator is working correctly and because tradition dictates a Hello World as the first program to run.



package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}