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
bdodson
Once Poster
Once Poster


Joined: 31 Oct 2008
Posts: 1

PostPosted: Fri Oct 31, 2008 6:46 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
VoiceTech
Freshman
Freshman


Joined: 02 Dec 2008
Posts: 3

PostPosted: Sun Dec 21, 2008 1:25 pm    Post subject: Re: regarding sound in musicplayer Reply with quote

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/reference/emulator.html#startup-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!
Back to top
View user's profile Send private message
andreleitao
Experienced Developer
Experienced Developer


Joined: 10 Mar 2009
Posts: 55
Location: Recife - Brazil

PostPosted: Fri Apr 03, 2009 7:32 pm    Post subject: Reply with quote

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

E.g.:

Java:

mediaplayer = MediaPlayer.create(MainActivity.getInstance(), R.raw.musicmp3);
try {
     mp.prepare();
} catch (IllegalStateException e) {
     e.printStackTrace();
} catch (IOException e) {
     e.printStackTrace();
}
mp.start();


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

Thanks!
Back to top
View user's profile Send private message Send e-mail MSN Messenger
lostInTransit
Developer
Developer


Joined: 12 Feb 2009
Posts: 26
Location: India

PostPosted: Sat May 23, 2009 6:24 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
exospy
Freshman
Freshman


Joined: 08 Jun 2009
Posts: 4
Location: San Francisco

PostPosted: Mon Jun 08, 2009 11:20 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
exospy
Freshman
Freshman


Joined: 08 Jun 2009
Posts: 4
Location: San Francisco

PostPosted: Tue Jun 09, 2009 12:10 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
vaibhavm
Freshman
Freshman


Joined: 01 Jul 2009
Posts: 3

PostPosted: Wed Jul 01, 2009 9:53 am    Post subject: how to progress on Seekbar Reply with quote

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
Back to top
View user's profile Send private message
llt1214
Freshman
Freshman


Joined: 05 May 2009
Posts: 5

PostPosted: Tue Aug 11, 2009 10:08 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
shishir.bobby
Experienced Developer
Experienced Developer


Joined: 09 Apr 2009
Posts: 59

PostPosted: Fri Sep 04, 2009 7:27 am    Post subject: Reply with quote

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

E.g.:

Java:

mediaplayer = MediaPlayer.create(MainActivity.getInstance(), R.raw.musicmp3);
try {
     mp.prepare();
} catch (IllegalStateException e) {
     e.printStackTrace();
} catch (IOException e) {
     e.printStackTrace();
}
mp.start();


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


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

Sad





my main.xml
XML:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget35"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<Button
android:id="@+id/play"
android:layout_width="64px"
android:layout_height="wrap_content"
android:text="PLAY"
android:layout_x="0px"
android:layout_y="0px"
>

</Button>
<Button
android:id="@+id/pause"
android:layout_width="64px"
android:layout_height="wrap_content"
android:text="PAUSE"
android:layout_x="125px"
android:layout_y="5px"
>

</Button>
<Button
android:id="@+id/stop"
android:layout_width="65px"
android:layout_height="wrap_content"
android:text="STOP"
android:layout_x="255px"
android:layout_y="2px"
>

</Button>
</AbsoluteLayout>
Smile


thanks and regards
Back to top
View user's profile Send private message
marcjc
Junior Developer
Junior Developer


Joined: 05 Jan 2010
Posts: 11

PostPosted: Wed Jan 06, 2010 7:34 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
perrot
Junior Developer
Junior Developer


Joined: 22 Jan 2010
Posts: 10
Location: China

PostPosted: Fri Jan 22, 2010 3:27 am    Post subject: Reply with quote

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();
Very Happy
Back to top
View user's profile Send private message MSN Messenger
Anders
Once Poster
Once Poster


Joined: 25 Jan 2010
Posts: 1

PostPosted: Mon Jan 25, 2010 6:49 pm    Post subject: Reply with quote

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.....
Back to top
View user's profile Send private message
marcjc
Junior Developer
Junior Developer


Joined: 05 Jan 2010
Posts: 11

PostPosted: Wed Feb 10, 2010 8:47 pm    Post subject: Reply with quote

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....
Back to top
View user's profile Send private message
cfultz
Once Poster
Once Poster


Joined: 12 Feb 2010
Posts: 1
Location: Virginia

PostPosted: Sun Feb 14, 2010 1:44 am    Post subject: Reply with quote

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

Java:
this.findViewById(R.id.FAIL);


here's a screenshot of it



Any thoughts on why this is happening?
Back to top
View user's profile Send private message Visit poster's website AIM Address
ropiemasta
Once Poster
Once Poster


Joined: 27 Feb 2010
Posts: 1

PostPosted: Tue Mar 02, 2010 6:29 pm    Post subject: Reply with quote

Quote:

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] Sad



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