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 Previous  1, 2, 3, 4, 5, 6, 7  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
Steel
Developer
Developer


Joined: 28 Dec 2007
Posts: 43
Location: Copenhagen, Denmark

PostPosted: Wed Mar 19, 2008 3:36 pm    Post subject: Reply with quote

I'm trying to play an mp3-file: beep.mp3

Winamp info box:
Code:
Size: 3600 bytes
Header found at: 256 bytes
Length: 0 seconds
MPEG-1 layer 3
128kbit, approx. 8 frames
44100Hz Joint Stereo
CRCs: Yes, Copyrighted: No
Original: No, Emphasis: None

Code:
Java:
        mBeeper = MediaPlayer.create(this, R.raw.beep);
        mBeeper.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public void onError(MediaPlayer mediaPlayer, int what, int extra) {
                Log.e(ACTIVITY_NAME, "MediaPlayer.onError: what=" + what + " extra=" + extra);
            }
        });
        mBeeper.prepareAsync();

Output:
Code:
MediaPlayer.onError: what=1 extra=0


Now does anyone know what the hell 'what' means? Laughing
That is - does anyone have any idea about the meaning of these method params, what and extra?

_________________
Eclipse is a fork, IntelliJ is the ninja sword. Which weapon are you going into battle with?
Back to top
View user's profile Send private message MSN Messenger
JaydeRyu
Freshman
Freshman


Joined: 16 Mar 2008
Posts: 4

PostPosted: Thu Mar 20, 2008 12:57 am    Post subject: Reply with quote

Steel wrote:
I'm trying to play an mp3-file: beep.mp3

Winamp info box:
Code:
Size: 3600 bytes
Header found at: 256 bytes
Length: 0 seconds
MPEG-1 layer 3
128kbit, approx. 8 frames
44100Hz Joint Stereo
CRCs: Yes, Copyrighted: No
Original: No, Emphasis: None

Code:
Java:
        mBeeper = MediaPlayer.create(this, R.raw.beep);
        mBeeper.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public void onError(MediaPlayer mediaPlayer, int what, int extra) {
                Log.e(ACTIVITY_NAME, "MediaPlayer.onError: what=" + what + " extra=" + extra);
            }
        });
        mBeeper.prepareAsync();

Output:
Code:
MediaPlayer.onError: what=1 extra=0


Now does anyone know what the hell 'what' means? Laughing
That is - does anyone have any idea about the meaning of these method params, what and extra?


I would suggest


Java:

try{
        mBeeper = MediaPlayer.create(this, R.raw.beep);
        mBeeper.prepareAsync();
}catch(IOException e){
        Log.e(_YOURTAG, _YOURINFO, e)
}


I couldn't find any details on the errors listed from the onErrorListener().
I don't know if this will help, but I found using

MediaPlayer.create(this, R.raw.beep);
to
MediaPlayer.create(YOUR_CLASS.this, R.raw.RESOURCE);

if it's a mp.prepareAsync() then i'm nor sure what problem is.
Back to top
View user's profile Send private message
markww
Freshman
Freshman


Joined: 24 Feb 2008
Posts: 9

PostPosted: Thu Mar 20, 2008 1:00 am    Post subject: Reply with quote

Although the media player may be able to play a single sound one time, it still means it's unusable for any application that hopes to make any serious use of sound. You don't know when the sound has stopped, you can't tell how many times you'll be able to play the sound before the player crashes. I still haven't seen any solid solution on this, I've decided to ignore use of the media player until it's fixed in some distant android release. If anyone DOES have a complete solution, that'd be great to know of.

Thanks
Back to top
View user's profile Send private message
Steel
Developer
Developer


Joined: 28 Dec 2007
Posts: 43
Location: Copenhagen, Denmark

PostPosted: Thu Mar 20, 2008 1:10 am    Post subject: Reply with quote

Thanks for the reply, JaydeRyu.

prepare() throws an IOException - I tried that one first, but there was no explanation-text with the IOException. That's why I tried prepareAsync(), because then you can attach an OnErrorListener, which at least gives the clue, that what was wrong ... was 1 Laughing

I'm creating & preparing the MediaPlayer in my onCreate() in my main Activity, so specifying the class name in front of this should be redundant, but I'll try it anyway ...

_________________
Eclipse is a fork, IntelliJ is the ninja sword. Which weapon are you going into battle with?
Back to top
View user's profile Send private message MSN Messenger
JaydeRyu
Freshman
Freshman


Joined: 16 Mar 2008
Posts: 4

PostPosted: Thu Mar 20, 2008 7:02 am    Post subject: Reply with quote

Steel wrote:
Thanks for the reply, JaydeRyu.

prepare() throws an IOException - I tried that one first, but there was no explanation-text with the IOException. That's why I tried prepareAsync(), because then you can attach an OnErrorListener, which at least gives the clue, that what was wrong ... was 1 Laughing

I'm creating & preparing the MediaPlayer in my onCreate() in my main Activity, so specifying the class name in front of this should be redundant, but I'll try it anyway ...


Sorry wasn't completely awake earlier. Don't use mp.prepare(). It didn't work for me. I did eventually get a sound file running from the resource.


This is all it takes to get my .wav file working.
Java:

mpt = MediaPlayer.create(MY_CLASS.this, R.raw.snd120);
mp.start();
Back to top
View user's profile Send private message
Steel
Developer
Developer


Joined: 28 Dec 2007
Posts: 43
Location: Copenhagen, Denmark

PostPosted: Wed Apr 02, 2008 10:31 am    Post subject: Reply with quote

LOL, you were right, JaydeRyu: Don't prepare the MediaPlayer (like the manual says), just start it.

And to replay a sound, re-create the MP - you can't use the same object, which already played the sound. Wierd.

_________________
Eclipse is a fork, IntelliJ is the ninja sword. Which weapon are you going into battle with?
Back to top
View user's profile Send private message MSN Messenger
zengcity
Once Poster
Once Poster


Joined: 05 May 2008
Posts: 1

PostPosted: Mon May 05, 2008 6:04 pm    Post subject: Reply with quote

I met the NullPointerException too.
And I change the <Button id="@+id/cmd_play" to <Button android:id="@+id/cmd_play" in the "main.xml" file.
It works.
Hope it helps;
Back to top
View user's profile Send private message
irobot
Freshman
Freshman


Joined: 27 Mar 2008
Posts: 5
Location: Coimbatore, India

PostPosted: Wed May 07, 2008 8:09 am    Post subject: Reply with quote

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




I have tried prepareAsync() instead of prepare() and it works fine.

_________________
If at first you don't succeed, look in the trash for the instructions.
Back to top
View user's profile Send private message
marielisacr
Junior Developer
Junior Developer


Joined: 21 May 2008
Posts: 13

PostPosted: Wed May 21, 2008 10:52 pm    Post subject: Reply with quote

Hi, i am trying to do a media player in android. Now i can play songs from an URL, i download the .mp3 file to a temp file and play it. The problem is that after listening a few songs the emulator crashes and restart by it itselft, it doesn´t show any error.

If anyone can help i really appreciate, thanks
Back to top
View user's profile Send private message
Steel
Developer
Developer


Joined: 28 Dec 2007
Posts: 43
Location: Copenhagen, Denmark

PostPosted: Wed May 21, 2008 11:21 pm    Post subject: Reply with quote

marielisacr wrote:
Hi, i am trying to do a media player in android. Now i can play songs from an URL, i download the .mp3 file to a temp file and play it. The problem is that after listening a few songs the emulator crashes and restart by it itselft, it doesn´t show any error.

If anyone can help i really appreciate, thanks


That sounds like the same problem markww described - that the emulator just crashes without errors after playing media sometimes. Wait for a new release Confused

_________________
Eclipse is a fork, IntelliJ is the ninja sword. Which weapon are you going into battle with?
Back to top
View user's profile Send private message MSN Messenger
sreekanth
Once Poster
Once Poster


Joined: 03 Jun 2008
Posts: 1

PostPosted: Tue Jun 03, 2008 1:25 pm    Post subject: regarding sound in musicplayer Reply with quote

Hi,

I am running the musicplayer on my android.I am using fedora 7 and android m5.In my system the programme is running normally but the sound is not coming.I am sending my musicplayer.java and main.xml......


package com.google.android.musicplayer;

import java.io.IOException;

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

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

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.santosam4);
MediaPlayer mp=new MediaPlayer();
try {
Toast.makeText(musicplayer.this, R.string.musicopen,
Toast.LENGTH_SHORT).show();

mp.setDataSource("/root/lion.wav");
mp.prepareAsync();
mp.start();
}catch(IOException e) {
Log.e("ioexception","",e);
}

Toast.makeText(musicplayer.this, R.string.musicopen,
Toast.LENGTH_SHORT).show();

mp.setOnCompletionListener(new OnCompletionListener(){

//@Override
public void onCompletion(MediaPlayer arg0){
Toast.makeText(musicplayer.this, R.string.musicclose,
Toast.LENGTH_SHORT).show();

}
});
}
});
}
}


main.xml



<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, musicplayer"/>
<Button android:id="@+id/cmd_play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="play the music ...."/>
</LinearLayout>




Regards,
Sreekanth.



error.png
 Description:
 Filesize:  57.21 KB
 Viewed:  9362 Time(s)

error.png


Back to top
View user's profile Send private message
drik_wen
Freshman
Freshman


Joined: 12 Jul 2008
Posts: 6

PostPosted: Sun Jul 13, 2008 3:13 pm    Post subject: playing media(mp3) from the URL problem Reply with quote

I try to play a mp3 file from the URL..
but the song will also stop buffering after 31%..
so I always fail to play the mp3 file
is there someone can tell me why?? a lil bit hurry..
thx in advanced
Back to top
View user's profile Send private message MSN Messenger
mike_lee
Once Poster
Once Poster


Joined: 12 Jul 2008
Posts: 1

PostPosted: Sun Jul 13, 2008 11:38 pm    Post subject: Re: regarding sound in musicplayer Reply with quote

I have the same problem here using fedora 8.

I can not hear sound, even if I hit the volume up/down button on emulator.

However, I can hear the mp3 using other mp3 player on my system.

does anyone have any clue on that?

Thanks a lot in advance

-sincerely

mike



sreekanth wrote:
Hi,

I am running the musicplayer on my android.I am using fedora 7 and android m5.In my system the programme is running normally but the sound is not coming.I am sending my musicplayer.java and main.xml......


package com.google.android.musicplayer;

import java.io.IOException;

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

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

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.santosam4);
MediaPlayer mp=new MediaPlayer();
try {
Toast.makeText(musicplayer.this, R.string.musicopen,
Toast.LENGTH_SHORT).show();

mp.setDataSource("/root/lion.wav");
mp.prepareAsync();
mp.start();
}catch(IOException e) {
Log.e("ioexception","",e);
}

Toast.makeText(musicplayer.this, R.string.musicopen,
Toast.LENGTH_SHORT).show();

mp.setOnCompletionListener(new OnCompletionListener(){

//@Override
public void onCompletion(MediaPlayer arg0){
Toast.makeText(musicplayer.this, R.string.musicclose,
Toast.LENGTH_SHORT).show();

}
});
}
});
}
}


main.xml



<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, musicplayer"/>
<Button android:id="@+id/cmd_play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="play the music ...."/>
</LinearLayout>




Regards,
Sreekanth.
Back to top
View user's profile Send private message
marielisacr
Junior Developer
Junior Developer


Joined: 21 May 2008
Posts: 13

PostPosted: Mon Jul 14, 2008 2:56 pm    Post subject: Reply with quote

Hi all,

There is a really good tutorial of how to play music from an Url here

http://www.anddev.org/viewtopic.php?p=8319#8319.

Try that and i hope you can solve your problems, it works for me.

I was capable to play sound in windows an Mac but I haven't try it on Linux.
Back to top
View user's profile Send private message
CiprianU
Freshman
Freshman


Joined: 24 Oct 2008
Posts: 6
Location: Bucharest, Romania

PostPosted: Fri Oct 24, 2008 11:56 am    Post subject: refresh a timer inside a music player app Reply with quote

Hi guys,

Can anyone of you help me with this thing:
I have an Activity which plays an .mp3, i added a SeekBar and now i want to add a label to indicate the time reached in the play. This is the code:
Java:

timer.schedule(new TimerTask()
          {
               public void run()
               {
                    int min = 0;
                    int sec = 0;
                    String str = "00:00";
                    int curPos = local_mp.getCurrentPosition();
                    seekBar.setProgress(curPos);
                    
                    /* calculate the seconds elapsed */
                    sec = (curPos % 60000)/1000;
                    /* calculate the minutes elapsed*/
                    min = curPos / 60000;
                    /* print the position in mm:ss format */
                    if(min < 10)
                    {
                         if(sec < 10)
                              str = "0"+min+":0"+sec;
                         else
                              str = "0"+min+":"+sec;
                    }
                    else
                    {
                         if(sec < 10)
                              str = min+":0"+sec;
                         else
                              str = min+":"+sec;
                    }
                    timePosition.setText(str);
               }
          }, 1000, 1000);

The progress of the SeekBar is well refreshed, but the label remains 00:00 (the value set in onCreate method). When i stop the file rendering it prints well the string, but while playing the text label isn't changed.
Can anyone of you help me pls!

PS: I'm a beginner in JAVA / Android, so any kind of help is usefull Wink
Back to top
View user's profile Send private message
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page Previous  1, 2, 3, 4, 5, 6, 7  Next
Page 5 of 7

 
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.