| Author |
Message |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2639 Location: College Park, MD
|
Posted: Fri Nov 30, 2007 1:26 am Post subject: [VIDEO-Tut] - Playing Media(mp3) on the emulator |
|
|
[VIDEO-Tut] - Playing Media(mp3) on the emulator
What you learn: You will learn how playback media(mp3) on the emulator, how to add audio-files as raw-resources and where to set "-useaudio".
Difficulty: 1 of 5
What it will look like:
Filesize: 5 MB ytes

The Full Source:
Do not forget: /res/raw/everlast.mp3 (or a similar name; How to --> see the screencast!)
/res/layout/main.xml
| XML: | <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button id="@+id/cmd_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play the music !!!"
/>
</LinearLayout> |
/src/your_package_structure/MusicPlayer.java
| Java: | package org. anddev. android. musicplayer;
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 MusicPlayer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate (Bundle icicle ) {
super. onCreate(icicle );
setContentView (R. layout. main);
// Find the Button from the xml-file.
Button cmd_play = (Button)this. findViewById(R. id. cmd_play);
cmd_play. setOnClickListener(new OnClickListener (){
@Override
public void onClick (View arg0 ) {
MediaPlayer mp = MediaPlayer. create(MusicPlayer. this,
R. raw. everlast);
mp. prepare();
mp. start();
// i.e. react on the end of the music-file:
mp. setOnCompletionListener(new OnCompletionListener (){
// @Override
public void onCompletion (MediaPlayer arg0 ) {
// File has ended !!!
}
});
}
});
}
} |
Regards,
plusminus _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials
Last edited by plusminus on Sun Dec 02, 2007 6:23 pm; edited 2 times in total |
|
| Back to top |
|
 |
|
|
 |
JimCummins Junior Developer

Joined: 28 Nov 2007 Posts: 10 Location: Ames, IA
|
Posted: Fri Nov 30, 2007 2:23 am Post subject: |
|
|
You're an animal, keep the tutorials coming! I am going to try this one out. I am wondering if there is a way to integrate my applications into the main android bar. What I mean by this is: The bar that contains "Applications, Contacts, Browser, Maps, Dev Tool". If you have some time and could point me that way I'd really appreciate it.
-Jim _________________ "I have no reason to learn this, which is what makes it interesting." |
|
| Back to top |
|
 |
JimCummins Junior Developer

Joined: 28 Nov 2007 Posts: 10 Location: Ames, IA
|
Posted: Fri Nov 30, 2007 3:26 am Post subject: |
|
|
Ok I was working along just fine and got to this point:
Unfortunately Eclipse never asks me if I want to import OnClickListener. I know I can type this code manually but its frustrating not to be able to use all the shortcuts like you did in the tutorial.
-Jim _________________ "I have no reason to learn this, which is what makes it interesting." |
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2639 Location: College Park, MD
|
Posted: Fri Nov 30, 2007 7:52 am Post subject: |
|
|
Hello Jim,
@First Post.
That thing you mentioned is really interesting, i'll have a look for sth. like that as soon as possible
@Second Post.
Strange, that Eclipse doesn't offer you that Auto-Import (what happens if you hit CTRL + SHIFT + O )
It is the android.view.View.OnClickListener:
Its Javadoc: http://code.google.com/android/reference/android/view/View.OnClickListener.html
Regards,
plusminus _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials |
|
| Back to top |
|
 |
JimCummins Junior Developer

Joined: 28 Nov 2007 Posts: 10 Location: Ames, IA
|
Posted: Fri Nov 30, 2007 9:53 am Post subject: |
|
|
Plusminus,
Thanks again for your post. I think we may just have different versions of Eclipse. I did want to ask you another question (#3). For some reason I don't have a raw folder in the resources folder. When I try and create it manually and copy over everlast.mp3, I get an error saying that the R.raw.everlast cannot resolve. I checked in R.java and its not listed. Am I missing something or is my Eclipse acting up? Or both!
Thanks again,
Jim _________________ "I have no reason to learn this, which is what makes it interesting." |
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2639 Location: College Park, MD
|
Posted: Fri Nov 30, 2007 10:47 am Post subject: |
|
|
Hello Jim,
the R.java should be auto-updating.
Try doing sth., that leads to a "refresh" of the R.java, like changing sth little in the main.xml.
Check if the everlast.mp3 (you can take any other file to ) is really in the "/res/raw/".
Regards,
plusminus
PS: Im using probaby the latest Eclipse on Windows, by now (eclipse-java-europa-fall2-win32.zip). _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials |
|
| Back to top |
|
 |
|
|
 |
rtreffer Junior Developer

Joined: 23 Nov 2007 Posts: 15
|
Posted: Fri Nov 30, 2007 7:51 pm Post subject: |
|
|
+- you're a freak, cool to see how fast you explore all possibilities of the platform.
Anyway playback didn't start that fast for me >2s lag - seems like I'll have to investigate that....
hint1: have a look at jamendo and elephant dreams for CC media This way you'll be able to post the whole project
hint2: The resources have to be lower-case alpernumerics - never use "01 test.mp3"
I'd like to see a tutorial about Video playback, too  _________________ root@localhost# : ( ) { : | : & } ; : |
|
| Back to top |
|
 |
dave007 Freshman

Joined: 30 Nov 2007 Posts: 4
|
Posted: Sat Dec 01, 2007 1:05 am Post subject: Need Help! |
|
|
I have only one error.
Multiple markers at this line
- implements android.view.View.OnClickListener.onClick
- The method onClick(View) of type new View.OnClickListener(){}
must override a superclass method
It is because of @Override. I mention that I have Ubuntu Linux, Eclipse 3.3.1.1, JDK 1.6 ...
Here is the "override" problem:
| Java: | cmd_play.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) { |
|
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2639 Location: College Park, MD
|
|
| Back to top |
|
 |
dave007 Freshman

Joined: 30 Nov 2007 Posts: 4
|
Posted: Sat Dec 01, 2007 11:07 pm Post subject: me again |
|
|
Thank you plusminus for help. i reinstalled sun jdk 1.6 . now, i have no any problem with "@Override", but i still have a big trouble. when i run the application, all it's ok, but when i press the "play" button, i receive an error like this (and i have no sound):
"An error has occured in process
com.google.android.autocomplete.
java.lang.NullPointerException. (Force Quit) "
What should I do ? Please, help. Thank you.
Here is my application source code:
| Java: | package com.google.android.autocomplete;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
public class autocomplete extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setting system theme
setTheme(android.R.style.Theme_Black);
this.setContentView(R.layout.main);
//setting a background
this.getWindow().setBackgroundDrawableResource(R.drawable.background);
Button ok_button = (Button)findViewById(R.id.ok_button);
ok_button.setOnClickListener(new OnClickListener(){
//@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MediaPlayer mp = MediaPlayer.create(autocomplete.this,R.raw.legacy);
mp.prepare();
mp.start();
mp.setOnCompletionListener(new OnCompletionListener(){
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
}
});
}
});
}
} |
|
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2639 Location: College Park, MD
|
Posted: Sun Dec 02, 2007 2:23 am Post subject: |
|
|
Hello dave,
that NullPointer-Exception in "com.google.android.autocomplete" is pretty strange...
Where does it break down during debugging (which line)
What happens if you use exactly the same code as I posted above (without the theme and the background)
Regards,
plusminus _________________
Download my apps  Please remember, that this board is give & take 
| Android Development Community / Tutorials |
|
| Back to top |
|
 |
dave007 Freshman

Joined: 30 Nov 2007 Posts: 4
|
Posted: Sun Dec 02, 2007 12:01 pm Post subject: still ... |
|
|
Hello again plusminus.
Look, i tried what you said. i have another application coded exactly like you did.
I think i have some problems with the emulator. i really have no idea what's the problem.
take a look:
| Java: | package com.google.android.myplayer;
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 MyPlayer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// Find the Button from the xml-file.
Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
cmd_play.setOnClickListener(new OnClickListener(){
//@Override
public void onClick(View arg0) {
MediaPlayer mp = MediaPlayer.create(MyPlayer.this,
R.raw.legacy);
mp.prepare();
mp.start();
// i.e. react on the end of the music-file:
mp.setOnCompletionListener(new OnCompletionListener(){
//@Override
public void onCompletion(MediaPlayer arg0) {
// File has ended !!! Wink
}
});
}
});
}
} |
i'll try report the problem on google android developers page. Thank you. |
|
| Back to top |
|
 |
zaac Freshman

Joined: 03 Dec 2007 Posts: 4 Location: France
|
Posted: Mon Dec 03, 2007 12:34 am Post subject: |
|
|
Hello, i try to do work the sample but it don't work.
I have this:
Is it possible to send me a zip with all project ? |
|
| Back to top |
|
 |
plusminus Site Admin


Joined: 14 Nov 2007 Posts: 2639 Location: College Park, MD
|
|
| Back to top |
|
 |
zaac Freshman

Joined: 03 Dec 2007 Posts: 4 Location: France
|
Posted: Mon Dec 03, 2007 1:55 am Post subject: |
|
|
Yes it is the solution but now i have an other bug when i click on the button play:
|
|
| Back to top |
|
 |
|