[VIDEO-Tut] - Playing Media(mp3) on the emulator

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[VIDEO-Tut] - Playing Media(mp3) on the emulator

Postby plusminus » Fri Nov 30, 2007 1:26 am

[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 MBytes :!:
Image


The Full Source:
Do not forget: /res/raw/everlast.mp3 (or a similar name; How to --> see the screencast!)

/res/layout/main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <Button id="@+id/cmd_play"
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    android:text="Play the music !!!"
  11.    />
  12. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


/src/your_package_structure/MusicPlayer.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.musicplayer;
  2.  
  3. import android.app.Activity;
  4. import android.media.MediaPlayer;
  5. import android.media.MediaPlayer.OnCompletionListener;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10.  
  11. public class MusicPlayer extends Activity {
  12.     /** Called when the activity is first created. */
  13.     @Override
  14.     public void onCreate(Bundle icicle) {
  15.         super.onCreate(icicle);
  16.         setContentView(R.layout.main);
  17.        
  18.         // Find the Button from the xml-file.
  19.         Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
  20.         cmd_play.setOnClickListener(new OnClickListener(){
  21.  
  22.                         @Override
  23.                         public void onClick(View arg0) {
  24.                                 MediaPlayer mp = MediaPlayer.create(MusicPlayer.this,
  25.                                                 R.raw.everlast);
  26.                                 mp.prepare();
  27.                                 mp.start();
  28.                                 // i.e. react on the end of the music-file:
  29.                                 mp.setOnCompletionListener(new OnCompletionListener(){
  30.  
  31.                                         // @Override
  32.                                         public void onCompletion(MediaPlayer arg0) {
  33.                                                 // File has ended !!! <img src="http://www.anddev.org/images/smilies/wink.png" alt=";)" title="Wink" />
  34.                                         }
  35.                                 });
  36.                         }
  37.         });
  38.     }
  39. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Last edited by plusminus on Sun Dec 02, 2007 6:23 pm, edited 2 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby JimCummins » Fri Nov 30, 2007 2:23 am

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."
JimCummins
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Nov 28, 2007 6:06 pm
Location: Ames, IA

Postby JimCummins » Fri Nov 30, 2007 3:26 am

Ok I was working along just fine and got to this point:
Image

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."
JimCummins
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Nov 28, 2007 6:06 pm
Location: Ames, IA

Postby plusminus » Fri Nov 30, 2007 7:52 am

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/referenc ... tener.html

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby JimCummins » Fri Nov 30, 2007 9:53 am

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."
JimCummins
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Nov 28, 2007 6:06 pm
Location: Ames, IA

Postby plusminus » Fri Nov 30, 2007 10:47 am

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).
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby rtreffer » Fri Nov 30, 2007 7:51 pm

+- 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# : ( ) { : | : & } ; :
rtreffer
Junior Developer
Junior Developer
 
Posts: 15
Joined: Fri Nov 23, 2007 4:14 pm

Need Help!

Postby dave007 » Sat Dec 01, 2007 1:05 am

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         cmd_play.setOnClickListener(new OnClickListener(){
  2.  
  3.                @Override
  4.  
  5.                public void onClick(View arg0) {
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
dave007
Freshman
Freshman
 
Posts: 4
Joined: Fri Nov 30, 2007 4:35 pm

Postby plusminus » Sat Dec 01, 2007 11:06 am

Hello Dave,

simply comment it out. Normally this ""bug"" happens, when you are using a JRE below 1.6 .

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         cmd_play.setOnClickListener(new OnClickListener(){
  2.                // @Override
  3.                public void onClick(View arg0) {
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

me again

Postby dave007 » Sat Dec 01, 2007 11:07 pm

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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.autocomplete;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.View;
  10.  
  11. import android.view.View.OnClickListener;
  12.  
  13. import android.widget.Button;
  14.  
  15. import android.media.MediaPlayer;
  16.  
  17. import android.media.MediaPlayer.OnCompletionListener;
  18.  
  19.  
  20.  
  21. public class autocomplete extends Activity {
  22.  
  23.     /** Called when the activity is first created. */
  24.  
  25.     @Override
  26.  
  27.     public void onCreate(Bundle icicle) {
  28.  
  29.         super.onCreate(icicle);
  30.  
  31.         //setting system theme
  32.  
  33.         setTheme(android.R.style.Theme_Black);
  34.  
  35.         this.setContentView(R.layout.main);
  36.  
  37.         //setting a background
  38.  
  39.         this.getWindow().setBackgroundDrawableResource(R.drawable.background);
  40.  
  41.         Button ok_button = (Button)findViewById(R.id.ok_button);      
  42.  
  43.        
  44.  
  45.         ok_button.setOnClickListener(new OnClickListener(){
  46.  
  47.  
  48.  
  49.                         //@Override
  50.  
  51.                         public void onClick(View arg0) {
  52.  
  53.                                 // TODO Auto-generated method stub
  54.  
  55.                                 MediaPlayer mp = MediaPlayer.create(autocomplete.this,R.raw.legacy);
  56.  
  57.                                 mp.prepare();
  58.  
  59.                                 mp.start();                            
  60.  
  61.                                 mp.setOnCompletionListener(new OnCompletionListener(){
  62.  
  63.                                         @Override
  64.  
  65.                                         public void onCompletion(MediaPlayer arg0) {
  66.  
  67.                                                 // TODO Auto-generated method stub                                             
  68.  
  69.                                         }                                      
  70.  
  71.                                 });                            
  72.  
  73.                         }
  74.  
  75.         });
  76.  
  77.     }
  78.  
  79.  
  80.  
  81. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
dave007
Freshman
Freshman
 
Posts: 4
Joined: Fri Nov 30, 2007 4:35 pm

Postby plusminus » Sun Dec 02, 2007 2:23 am

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
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

still ...

Postby dave007 » Sun Dec 02, 2007 12:01 pm

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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.myplayer;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.media.MediaPlayer;
  8.  
  9. import android.media.MediaPlayer.OnCompletionListener;
  10.  
  11. import android.os.Bundle;
  12.  
  13. import android.view.View;
  14.  
  15. import android.view.View.OnClickListener;
  16.  
  17. import android.widget.Button;
  18.  
  19.  
  20.  
  21. public class MyPlayer extends Activity {
  22.  
  23.     /** Called when the activity is first created. */
  24.  
  25.     @Override
  26.  
  27.     public void onCreate(Bundle icicle) {
  28.  
  29.         super.onCreate(icicle);
  30.  
  31.         setContentView(R.layout.main);
  32.  
  33.        
  34.  
  35.         // Find the Button from the xml-file.
  36.  
  37.         Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
  38.  
  39.         cmd_play.setOnClickListener(new OnClickListener(){
  40.  
  41.  
  42.  
  43.                //@Override
  44.  
  45.                public void onClick(View arg0) {
  46.  
  47.                     MediaPlayer mp = MediaPlayer.create(MyPlayer.this,
  48.  
  49.                               R.raw.legacy);
  50.  
  51.                     mp.prepare();
  52.  
  53.                     mp.start();
  54.  
  55.                     // i.e. react on the end of the music-file:
  56.  
  57.                     mp.setOnCompletionListener(new OnCompletionListener(){
  58.  
  59.  
  60.  
  61.                           //@Override
  62.  
  63.                          public void onCompletion(MediaPlayer arg0) {
  64.  
  65.                               // File has ended !!! Wink
  66.  
  67.                          }
  68.  
  69.                     });
  70.  
  71.                }
  72.  
  73.         });
  74.  
  75.     }
  76.  
  77. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4

i'll try report the problem on google android developers page. Thank you.
dave007
Freshman
Freshman
 
Posts: 4
Joined: Fri Nov 30, 2007 4:35 pm

Postby zaac » Mon Dec 03, 2007 12:34 am

Hello, i try to do work the sample but it don't work.

I have this:
Image


Is it possible to send me a zip with all project ?
zaac
Freshman
Freshman
 
Posts: 4
Joined: Mon Dec 03, 2007 12:24 am
Location: France

Postby plusminus » Mon Dec 03, 2007 12:54 am

Hello zaac,

check your AndroidManifest.xml :!:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="org.anddev.android.musicplayer">
  4.     <application android:icon="@drawable/icon">
  5.         <activity class=".MusicPlayer" android:label="@string/app_name">
  6.             <intent-filter>
  7.                 <action android:value="android.intent.action.MAIN" />
  8.                 <category android:value="android.intent.category.LAUNCHER" />
  9.             </intent-filter>
  10.         </activity>
  11.     </application>
  12. </manifest>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

and have a close look at these lines:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. ...  
  2. package="org.anddev.android.musicplayer">
  3. ...  
  4. <activity class=".MusicPlayer" android:label="@string/app_name">
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


The first of them contains the packageStructure 'MyApplication.java' is located in.
The second contains a dot followed by the name of the 'MyApplication.java'.
So both together fit to the full path to the Application you want to start. (Check for Typos and Case Sensitivity)

Let us know if that was the solution. :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby zaac » Mon Dec 03, 2007 1:55 am

Yes it is the solution but now i have an other bug when i click on the button play:

Image
zaac
Freshman
Freshman
 
Posts: 4
Joined: Mon Dec 03, 2007 12:24 am
Location: France

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests