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

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

Postby bdodson » Fri Oct 31, 2008 6:46 pm

Thanks for the tutorial.. Does anyone know the best way to get a playlist (m3u) file to play? I'm hoping to avoid dealing with playback issues like fwd/rew/pause etc, I just want to send the media player a list of files to play...

By default, the web browser doesn't seem to be able to handle an m3u file correctly.
bdodson
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Oct 31, 2008 6:43 pm

Top

Re: regarding sound in musicplayer

Postby VoiceTech » Sun Dec 21, 2008 1:25 pm

mike_lee wrote: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.



In your run configuration make sure you have:

-audio oss

According to the documentation the -useaudio parameter is enabled by default so you shouldn't need it.

Also, what threw me was the exception when using prepare(). Just use:

try {

mp = MediaPlayer.create(PlayMusic.this, R.raw.groovytune);
mp.start();

} catch (IllegalStateException e) {
e.printStackTrace();
}

You will need to call prepare() if you are looking to play the media file again.

Instructions for -useaudio and -audio here:
http://code.google.com/android/referenc ... up-options

Instructions on how to use the media player here:
http://code.google.com/android/toolbox/apis/media.html

I'm running CentOS 5.2 with SDK 1.0 sr2.

Hope this helps!
VoiceTech
Freshman
Freshman
 
Posts: 3
Joined: Tue Dec 02, 2008 4:35 pm

Postby andreleitao » Fri Apr 03, 2009 7:32 pm

Hi, why when I call mediaplayer.stop() or mediaplayer.pause() is continues playing the sound?

E.g.:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. mediaplayer = MediaPlayer.create(MainActivity.getInstance(), R.raw.musicmp3);
  3.  
  4. try {
  5.  
  6.         mp.prepare();
  7.  
  8. } catch (IllegalStateException e) {
  9.  
  10.         e.printStackTrace();
  11.  
  12. } catch (IOException e) {
  13.  
  14.         e.printStackTrace();
  15.  
  16. }
  17.  
  18. mp.start();
  19.  
  20.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


If I call mediaplayer.stop() the music is not stopped. Why that?

Thanks!
andreleitao
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Mar 10, 2009 9:56 pm
Location: Recife - Brazil

Postby lostInTransit » Sat May 23, 2009 6:24 am

Reviving an old thread. I needed some help with the MediaPlayer class

I am using a MediaPlayer instance in order to stream audio files from an Internet location. The audio player is in a separate activity. The user can select from a list of audio files and come to this activity to play the audio.

Now the user might go back to the previous activity (with the list) and select another audio file. In this case, I want to stop any other audio that is playing and start playing the new audio which was selected.

I did find out how to know whether an audio is playing. We can do it by using an object of AudioManager and calling isAudioPlaying(). This will return a true if any audio is playing.

How do I stop an audio currently playing? I do not have an instance of the MediaPlayer object which was created to start the audio (coz the user has already left the activity once and has come back with a new object of the activity and thus a new instance of the MediaPlayer). Is there any way I can stop any audio playing without having to hold on to the MediaPlayer object?

Thanks.
lostInTransit
Developer
Developer
 
Posts: 26
Joined: Thu Feb 12, 2009 10:37 am
Location: India

Postby exospy » Mon Jun 08, 2009 11:20 pm

When I try to use the -useaudio at the command line in eclipse, i continue getting an error:
[2009-06-08 17:16:26 - Emulator] unknown option: -useaudio
[2009-06-08 17:16:26 - Emulator] please use -help for a list of valid options

Anyone with suggestions? i have android 1.5 with eclipse but i get this error. really frustrated.
exospy
Freshman
Freshman
 
Posts: 4
Joined: Mon Jun 08, 2009 3:40 am
Location: San Francisco

Postby exospy » Tue Jun 09, 2009 12:10 am

my issue is this...

-useaudio i assume is not recognized anymore? someone confirm that for me? i used "-audio <name of sound sytem>", in my cause "-audio winaudio"

hope this helps others who have the same problem.
exospy
Freshman
Freshman
 
Posts: 4
Joined: Mon Jun 08, 2009 3:40 am
Location: San Francisco

Top

how to progress on Seekbar

Postby vaibhavm » Wed Jul 01, 2009 9:53 am

Hi,
Dose any one know how to show progress of audio file which is being played on a seekbar or progress bar
thanks in advance
vaibhavm
Freshman
Freshman
 
Posts: 3
Joined: Wed Jul 01, 2009 8:27 am

Postby llt1214 » Tue Aug 11, 2009 10:08 am

lostInTransit wrote:Reviving an old thread. I needed some help with the MediaPlayer class

I am using a MediaPlayer instance in order to stream audio files from an Internet location. The audio player is in a separate activity. The user can select from a list of audio files and come to this activity to play the audio.

Now the user might go back to the previous activity (with the list) and select another audio file. In this case, I want to stop any other audio that is playing and start playing the new audio which was selected.

I did find out how to know whether an audio is playing. We can do it by using an object of AudioManager and calling isAudioPlaying(). This will return a true if any audio is playing.

How do I stop an audio currently playing? I do not have an instance of the MediaPlayer object which was created to start the audio (coz the user has already left the activity once and has come back with a new object of the activity and thus a new instance of the MediaPlayer). Is there any way I can stop any audio playing without having to hold on to the MediaPlayer object?

Thanks.


Hi lostInTransit! Have you already found the solution to this problem? I was also trying to stop a currently playing audio from a different application.

Thanks.
llt1214
Freshman
Freshman
 
Posts: 5
Joined: Tue May 05, 2009 2:50 am

Postby shishir.bobby » Fri Sep 04, 2009 7:27 am

andreleitao wrote:Hi, why when I call mediaplayer.stop() or mediaplayer.pause() is continues playing the sound?

E.g.:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mediaplayer = MediaPlayer.create(MainActivity.getInstance(), R.raw.musicmp3);
  2. try {
  3.         mp.prepare();
  4. } catch (IllegalStateException e) {
  5.         e.printStackTrace();
  6. } catch (IOException e) {
  7.         e.printStackTrace();
  8. }
  9. mp.start();
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


If I call mediaplayer.stop() the music is not stopped. Why that?






Thanks!







here is my code, in this, u can play,pause and stop d song,its for a single song,no playlist implemented yet ...remember to change the path of song...and try my code...and try to implement itin ur code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package com.shishir;
  5.  
  6.  
  7.  
  8. import android.app.Activity;
  9.  
  10. import android.app.AlertDialog;
  11.  
  12. import android.content.DialogInterface;
  13.  
  14. import android.media.MediaPlayer;
  15.  
  16. import android.media.MediaPlayer.OnCompletionListener;
  17.  
  18. import android.os.Bundle;
  19.  
  20. import android.view.View;
  21.  
  22. import android.view.View.OnClickListener;
  23.  
  24. import android.widget.Button;
  25.  
  26.  
  27.  
  28.  
  29.  
  30. public class MusicPlayer extends Activity {
  31.  
  32.         /** Called when the activity is first created. */
  33.  
  34.         @Override
  35.  
  36.         public void onCreate(Bundle icicle) {
  37.  
  38.                 super.onCreate(icicle);
  39.  
  40.                 setContentView(R.layout.main);
  41.  
  42.  
  43.  
  44.                 final MediaPlayer mplayer = MediaPlayer.create(MusicPlayer.this,
  45.  
  46.                                 R.raw.linkinpark);
  47.  
  48.  
  49.  
  50.                 // PLAY button
  51.  
  52.                 Button play = (Button) this.findViewById(R.id.play);
  53.  
  54.                 play.setOnClickListener(new OnClickListener() {
  55.  
  56.                         @Override
  57.  
  58.                         public void onClick(View arg0) {
  59.  
  60.  
  61.  
  62.                                 // mp.prepare();
  63.  
  64.                                 mplayer.start();
  65.  
  66.  
  67.  
  68.                                 mplayer.setOnCompletionListener(new OnCompletionListener() {
  69.  
  70.                                         public void onCompletion(MediaPlayer arg0) {
  71.  
  72.                                                 // End of file
  73.  
  74.                                         }                                      
  75.  
  76.                                 });
  77.  
  78.                         }
  79.  
  80.                 });
  81.  
  82.  
  83.  
  84.                 // PAUSE button
  85.  
  86.                 Button pause = (Button) this.findViewById(R.id.pause);
  87.  
  88.                 pause.setOnClickListener(new Button.OnClickListener() {
  89.  
  90.                         public void onClick(View v) {
  91.  
  92.                                 mplayer.pause();
  93.  
  94.                         }
  95.  
  96.                 });
  97.  
  98.  
  99.  
  100.                 // STOP button
  101.  
  102.                 Button stop = (Button) this.findViewById(R.id.stop);
  103.  
  104.                 stop.setOnClickListener(new Button.OnClickListener() {
  105.  
  106.                         public void onClick(View v) {
  107.  
  108.                                 mplayer.stop();
  109.  
  110.                         }
  111.  
  112.                 });
  113.  
  114.                
  115.  
  116.         }
  117.  
  118.        
  119.  
  120.        
  121.  
  122. }
  123.  
  124.  
  125.  
  126.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
:(





my main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <AbsoluteLayout
  5.  
  6. android:id="@+id/widget35"
  7.  
  8. android:layout_width="fill_parent"
  9.  
  10. android:layout_height="fill_parent"
  11.  
  12. xmlns:android="http://schemas.android.com/apk/res/android"
  13.  
  14. >
  15.  
  16. <Button
  17.  
  18. android:id="@+id/play"
  19.  
  20. android:layout_width="64px"
  21.  
  22. android:layout_height="wrap_content"
  23.  
  24. android:text="PLAY"
  25.  
  26. android:layout_x="0px"
  27.  
  28. android:layout_y="0px"
  29.  
  30. >
  31.  
  32. </Button>
  33.  
  34. <Button
  35.  
  36. android:id="@+id/pause"
  37.  
  38. android:layout_width="64px"
  39.  
  40. android:layout_height="wrap_content"
  41.  
  42. android:text="PAUSE"
  43.  
  44. android:layout_x="125px"
  45.  
  46. android:layout_y="5px"
  47.  
  48. >
  49.  
  50. </Button>
  51.  
  52. <Button
  53.  
  54. android:id="@+id/stop"
  55.  
  56. android:layout_width="65px"
  57.  
  58. android:layout_height="wrap_content"
  59.  
  60. android:text="STOP"
  61.  
  62. android:layout_x="255px"
  63.  
  64. android:layout_y="2px"
  65.  
  66. >
  67.  
  68. </Button>
  69.  
  70. </AbsoluteLayout>
  71.  
  72.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
:)


thanks and regards
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Postby marcjc » Wed Jan 06, 2010 7:34 pm

Your code is great.... thanks.... 1 question though..

After stopping a song from playing, how do you restart the song? I click play again and it does not start. I have to exit and then restart to play.

thank you again.
marcjc
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Jan 05, 2010 3:16 pm

Postby perrot » Fri Jan 22, 2010 3:27 am

marcjc wrote:Your code is great.... thanks.... 1 question though..

After stopping a song from playing, how do you restart the song? I click play again and it does not start. I have to exit and then restart to play.

thank you again.


play again.
try this:

MediaPlayer mp=new MediaPlayer();

//stop media player
mp.stop();
mp.reset();

//start media player
mp.start();

//pause media player
mp.pause();
:D
perrot
Junior Developer
Junior Developer
 
Posts: 15
Joined: Fri Jan 22, 2010 3:21 am
Location: China

Postby Anders » Mon Jan 25, 2010 6:49 pm

Hi, dont know if you guys made it work allready, but when you use MediaPlayer.Create();

you cant use prepare();, it is allready loaded on create....

so the right one is :

MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
mp.start();

Hope its helps.....
Anders
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 25, 2010 6:46 pm

Postby marcjc » Wed Feb 10, 2010 8:47 pm

all good now...

but now trying to add more than one song to play when the play button is pressed again....

example:

press button song plays, press button again, song 2 plays, press button again, song 3 plays, etc....
marcjc
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Jan 05, 2010 3:16 pm

Postby cfultz » Sun Feb 14, 2010 1:44 am

The code fails for me because it can't find id in the R.java file

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.findViewById(R.id.FAIL);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


here's a screenshot of it

Image

Any thoughts on why this is happening?
User avatar
cfultz
Freshman
Freshman
 
Posts: 2
Joined: Fri Feb 12, 2010 10:25 am
Location: Virginia

Postby ropiemasta » Tue Mar 02, 2010 6:29 pm

package com.shishir;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
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);

final MediaPlayer mplayer = MediaPlayer.create(MusicPlayer.this,
R.raw.linkinpark);

// PLAY button
Button play = (Button) this.findViewById(R.id.play);
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

// mp.prepare();
mplayer.start();

mplayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
// End of file
}
});
}
});

// PAUSE button
Button pause = (Button) this.findViewById(R.id.pause);
pause.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mplayer.pause();
}
});

// STOP button
Button stop = (Button) this.findViewById(R.id.stop);
stop.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mplayer.stop();
}
});

}


}

[/syntax] :(



So I used your code and everything works fine.. except when I want to play the file again... What do I need to add/change to make it so when I press "Play" after pressing "Stop" to get the mp3 to play from the begining again? The only way I've been able to is to restart. I'm guessing it's because the mediaplayer is created during onCreate... Thanks for the help.
ropiemasta
Freshman
Freshman
 
Posts: 4
Joined: Sat Feb 27, 2010 2:02 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 10 guests