Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with MediaPlayer, raw resources, stop and start
    text
    copied!<p>I'm new to Android development and I have a question/problem. </p> <p>I'm playing around with the MediaPlayer class to reproduce some sounds/music. I am playing raw resources (<code>res/raw</code>) and it looks kind of easy.</p> <p>To play a raw resource, the MediaPlayer has to be initialized like this:</p> <p><pre><code> MediaPlayer mp = MediaPlayer.create(appContext, R.raw.song); mp.start(); </pre></code></p> <p>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.</p> <p>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!)</p> <p>To stop the media player, you only have to call <code>stop()</code>. But to play the sound again, the media player has to be reseted and prepared.</p> <p><pre><code> mp.reset(); mp.setDataSource(params); mp.prepare(); </pre></code></p> <p>The problem is that the method <code>setDataSource()</code> only accepts as params a file path, Content Provider URI, streaming media URL path, or File Descriptor.</p> <p>So, since this method doesn't accept a resource identifier, I don't know how to set the data source in order to call <code>prepare()</code>. 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.</p> <p>I guess I'm missing something. I wonder if I am mixing concepts, and the method <code>stop()</code> doesn't have to be called in the "Stop" button. Any help?</p> <p>Thanks in advance!!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload