Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with MediaPlayer, raw resources, stop and start

Tags:

I'm new to Android development and I have a question/problem.

I'm playing around with the MediaPlayer class to reproduce some sounds/music. I am playing raw resources (res/raw) and it looks kind of easy.

To play a raw resource, the MediaPlayer has to be initialized like this:

 MediaPlayer mp = MediaPlayer.create(appContext, R.raw.song); mp.start(); 

Until here there is no problem. The sound is played, and everything works fine. My problem appears when I want to add more options to my application. Specifically when I add the "Stop" button/option.

Basically, what I want to do is...when I press "Stop", the music stops. And when I press "Start", the song/sound starts over. (pretty basic!)

To stop the media player, you only have to call stop(). But to play the sound again, the media player has to be reseted and prepared.

 mp.reset(); mp.setDataSource(params); mp.prepare(); 

The problem is that the method setDataSource() only accepts as params a file path, Content Provider URI, streaming media URL path, or File Descriptor.

So, since this method doesn't accept a resource identifier, I don't know how to set the data source in order to call prepare(). In addition, I don't understand why you can't use a Resouce identifier to set the data source, but you can use a resource identifier when initializing the MediaPlayer.

I guess I'm missing something. I wonder if I am mixing concepts, and the method stop() doesn't have to be called in the "Stop" button. Any help?

Thanks in advance!!!

like image 652
arakn0 Avatar asked Jun 03 '10 19:06

arakn0


People also ask

How do I know if MediaPlayer is paused?

There is no API to check if MediaPlayer is paused or not. So please use any Boolean variable to check and toggle it when you paused using any button .

How do I start Media Player?

To start the playback, start() must be called. After start() returns successfully, the MediaPlayer object is in the Started state. isPlaying() can be called to test whether the MediaPlayer object is in the Started state.

How do I play music through MediaPlayer?

Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method. Similarly, you can pause the playing media file by using the pause() method. Stop the playback: You can stop playback by using the reset() method.


1 Answers

Here is what I did to load multiple resources with a single MediaPlayer:

/**  * Play a sample with the Android MediaPLayer.  *  * @param resid Resource ID if the sample to play.  */ private void playSample(int resid) {     AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);      try     {            mediaPlayer.reset();         mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());         mediaPlayer.prepare();         mediaPlayer.start();         afd.close();     }     catch (IllegalArgumentException e)     {         Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);     }     catch (IllegalStateException e)     {         Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);     }     catch (IOException e)     {         Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);     } 

mediaPlay is a member variable that get created and released at other points in the class. This may not be the best way (I am new to Android myself), but it seems to work. Just note that the code will probably fall trough to the bottom of the method before the mediaPlayer is done playing. If you need to play a series of resources, you will still need to handle this case.

like image 76
adstro Avatar answered Oct 22 '22 15:10

adstro