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

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

Postby Steel » Wed Mar 19, 2008 3:36 pm

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

Winamp info box:
Code: Select all
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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.          mBeeper = MediaPlayer.create(this, R.raw.beep);
  2.         mBeeper.setOnErrorListener(new MediaPlayer.OnErrorListener() {
  3.             public void onError(MediaPlayer mediaPlayer, int what, int extra) {
  4.                 Log.e(ACTIVITY_NAME, "MediaPlayer.onError: what=" + what + " extra=" + extra);
  5.             }
  6.         });
  7.         mBeeper.prepareAsync();
  8.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

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


Now does anyone know what the hell 'what' means? :lol:
That is - does anyone have any idea about the meaning of these method params, what and extra?
Steel
Developer
Developer
 
Posts: 48
Joined: Fri Dec 28, 2007 1:11 pm
Location: Herning, Denmark

Top

Postby JaydeRyu » Thu Mar 20, 2008 12:57 am

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

Winamp info box:
Code: Select all
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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.          mBeeper = MediaPlayer.create(this, R.raw.beep);
  2.         mBeeper.setOnErrorListener(new MediaPlayer.OnErrorListener() {
  3.             public void onError(MediaPlayer mediaPlayer, int what, int extra) {
  4.                 Log.e(ACTIVITY_NAME, "MediaPlayer.onError: what=" + what + " extra=" + extra);
  5.             }
  6.         });
  7.         mBeeper.prepareAsync();
  8.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

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


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


I would suggest


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try{
  2.         mBeeper = MediaPlayer.create(this, R.raw.beep);
  3.         mBeeper.prepareAsync();
  4. }catch(IOException e){
  5.         Log.e(_YOURTAG, _YOURINFO, e)
  6. }
  7.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


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.
JaydeRyu
Freshman
Freshman
 
Posts: 4
Joined: Sun Mar 16, 2008 10:10 am

Postby markww » Thu Mar 20, 2008 1:00 am

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
markww
Freshman
Freshman
 
Posts: 9
Joined: Sun Feb 24, 2008 7:36 am

Postby Steel » Thu Mar 20, 2008 1:10 am

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 :lol:

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 ...
Steel
Developer
Developer
 
Posts: 48
Joined: Fri Dec 28, 2007 1:11 pm
Location: Herning, Denmark

Postby JaydeRyu » Thu Mar 20, 2008 7:02 am

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 :lol:

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.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mpt = MediaPlayer.create(MY_CLASS.this, R.raw.snd120);
  2. mp.start();
  3.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
JaydeRyu
Freshman
Freshman
 
Posts: 4
Joined: Sun Mar 16, 2008 10:10 am

Postby Steel » Wed Apr 02, 2008 10:31 am

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.
Steel
Developer
Developer
 
Posts: 48
Joined: Fri Dec 28, 2007 1:11 pm
Location: Herning, Denmark

Top

Postby zengcity » Mon May 05, 2008 6:04 pm

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;
zengcity
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 05, 2008 4:49 pm

Postby irobot » Wed May 07, 2008 8:09 am

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

Image



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.
irobot
Freshman
Freshman
 
Posts: 5
Joined: Thu Mar 27, 2008 8:32 am
Location: Coimbatore, India

Postby marielisacr » Wed May 21, 2008 10:52 pm

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
marielisacr
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed May 21, 2008 10:04 pm

Postby Steel » Wed May 21, 2008 11:21 pm

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 :?
Steel
Developer
Developer
 
Posts: 48
Joined: Fri Dec 28, 2007 1:11 pm
Location: Herning, Denmark

regarding sound in musicplayer

Postby sreekanth » Tue Jun 03, 2008 1:25 pm

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.
Attachments
error.png
error.png (57.21 KiB) Viewed 12317 times
sreekanth
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jun 03, 2008 1:13 pm

playing media(mp3) from the URL problem

Postby drik_wen » Sun Jul 13, 2008 3:13 pm

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
drik_wen
Freshman
Freshman
 
Posts: 6
Joined: Sat Jul 12, 2008 3:00 am

Re: regarding sound in musicplayer

Postby mike_lee » Sun Jul 13, 2008 11:38 pm

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.
mike_lee
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Jul 12, 2008 2:59 pm

Postby marielisacr » Mon Jul 14, 2008 2:56 pm

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.
marielisacr
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed May 21, 2008 10:04 pm

refresh a timer inside a music player app

Postby CiprianU » Fri Oct 24, 2008 11:56 am

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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. timer.schedule(new TimerTask()
  3.  
  4.                 {
  5.  
  6.                         public void run()
  7.  
  8.                         {
  9.  
  10.                                 int min = 0;
  11.  
  12.                                 int sec = 0;
  13.  
  14.                                 String str = "00:00";
  15.  
  16.                                 int curPos = local_mp.getCurrentPosition();
  17.  
  18.                                 seekBar.setProgress(curPos);
  19.  
  20.                                
  21.  
  22.                                 /* calculate the seconds elapsed */
  23.  
  24.                                 sec = (curPos % 60000)/1000;
  25.  
  26.                                 /* calculate the minutes elapsed*/
  27.  
  28.                                 min = curPos / 60000;
  29.  
  30.                                 /* print the position in mm:ss format */
  31.  
  32.                                 if(min < 10)
  33.  
  34.                                 {
  35.  
  36.                                         if(sec < 10)
  37.  
  38.                                                 str = "0"+min+":0"+sec;
  39.  
  40.                                         else
  41.  
  42.                                                 str = "0"+min+":"+sec;
  43.  
  44.                                 }
  45.  
  46.                                 else
  47.  
  48.                                 {
  49.  
  50.                                         if(sec < 10)
  51.  
  52.                                                 str = min+":0"+sec;
  53.  
  54.                                         else
  55.  
  56.                                                 str = min+":"+sec;
  57.  
  58.                                 }
  59.  
  60.                                 timePosition.setText(str);
  61.  
  62.                         }
  63.  
  64.                 }, 1000, 1000);
  65.  
  66.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

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 ;)
CiprianU
Freshman
Freshman
 
Posts: 6
Joined: Fri Oct 24, 2008 11:15 am
Location: Bucharest, Romania

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests