Problems with MediaPlayer, raw resources, stop and start

Posted by arakn0 on Stack Overflow See other posts from Stack Overflow or by arakn0
Published on 2010-06-03T19:47:32Z Indexed on 2010/06/03 19:54 UTC
Read the original article Hit count: 404

Filed under:
|
|
|

Hello everybody,

I'm new in Android development and I have the next 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 that 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 advanced!!!

© Stack Overflow or respective owner

Related posts about android

Related posts about mediaplayer