andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

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

Goto page 1, 2, 3, 4, 5, 6  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2639
Location: College Park, MD

PostPosted: Fri Nov 30, 2007 1:26 am    Post subject: [VIDEO-Tut] - Playing Media(mp3) on the emulator Reply with quote

[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 Smile

What it will look like:
Filesize: 5 MBytes Exclamation


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 !!! Wink
                         }
                    });
               }
        });
    }
}


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile

| Android Development Community / Tutorials


Last edited by plusminus on Sun Dec 02, 2007 6:23 pm; edited 2 times in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
JimCummins
Junior Developer
Junior Developer


Joined: 28 Nov 2007
Posts: 10
Location: Ames, IA

PostPosted: Fri Nov 30, 2007 2:23 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address
JimCummins
Junior Developer
Junior Developer


Joined: 28 Nov 2007
Posts: 10
Location: Ames, IA

PostPosted: Fri Nov 30, 2007 3:26 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2639
Location: College Park, MD

PostPosted: Fri Nov 30, 2007 7:52 am    Post subject: Reply with quote

Hello Jim,

@First Post.
That thing you mentioned is really interesting, i'll have a look for sth. like that as soon as possible Exclamation

@Second Post.
Strange, that Eclipse doesn't offer you that Auto-Import (what happens if you hit CTRL + SHIFT + O ) Question
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 Idea
Please remember, that this board is give & take Smile

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
JimCummins
Junior Developer
Junior Developer


Joined: 28 Nov 2007
Posts: 10
Location: Ames, IA

PostPosted: Fri Nov 30, 2007 9:53 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2639
Location: College Park, MD

PostPosted: Fri Nov 30, 2007 10:47 am    Post subject: Reply with quote

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 Exclamation) 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 Idea
Please remember, that this board is give & take Smile

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
rtreffer
Junior Developer
Junior Developer


Joined: 23 Nov 2007
Posts: 15

PostPosted: Fri Nov 30, 2007 7:51 pm    Post subject: Reply with quote

+- 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 Sad >2s lag - seems like I'll have to investigate that....

hint1: have a look at jamendo and elephant dreams for CC media Smile 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 Wink

_________________
root@localhost# : ( ) { : | : & } ; :
Back to top
View user's profile Send private message
dave007
Freshman
Freshman


Joined: 30 Nov 2007
Posts: 4

PostPosted: Sat Dec 01, 2007 1:05 am    Post subject: Need Help! Reply with quote

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
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2639
Location: College Park, MD

PostPosted: Sat Dec 01, 2007 11:06 am    Post subject: Reply with quote

Hello Dave,

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

Java:
       cmd_play.setOnClickListener(new OnClickListener(){
               // @Override
               public void onClick(View arg0) {


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dave007
Freshman
Freshman


Joined: 30 Nov 2007
Posts: 4

PostPosted: Sat Dec 01, 2007 11:07 pm    Post subject: me again Reply with quote

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
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2639
Location: College Park, MD

PostPosted: Sun Dec 02, 2007 2:23 am    Post subject: Reply with quote

Hello dave,

that NullPointer-Exception in "com.google.android.autocomplete" is pretty strange...
Where does it break down during debugging (which line) Question

What happens if you use exactly the same code as I posted above (without the theme and the background) Question

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dave007
Freshman
Freshman


Joined: 30 Nov 2007
Posts: 4

PostPosted: Sun Dec 02, 2007 12:01 pm    Post subject: still ... Reply with quote

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
View user's profile Send private message
zaac
Freshman
Freshman


Joined: 03 Dec 2007
Posts: 4
Location: France

PostPosted: Mon Dec 03, 2007 12:34 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2639
Location: College Park, MD

PostPosted: Mon Dec 03, 2007 12:54 am    Post subject: Reply with quote

Hello zaac,

check your AndroidManifest.xml Exclamation
XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.anddev.android.musicplayer">

    <application android:icon="@drawable/icon">
        <activity class=".MusicPlayer" android:label="@string/app_name">
            <intent-filter>
                <action android:value="android.intent.action.MAIN" />
                <category android:value="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

and have a close look at these lines:
XML:
...  
package="org.anddev.android.musicplayer">
...  
<activity class=".MusicPlayer" android:label="@string/app_name">


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

Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile

| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
zaac
Freshman
Freshman


Joined: 03 Dec 2007
Posts: 4
Location: France

PostPosted: Mon Dec 03, 2007 1:55 am    Post subject: Reply with quote

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

Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page 1, 2, 3, 4, 5, 6  Next
Page 1 of 6

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.