Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid app crashes on playing sound
    text
    copied!<p>I'm developing a free alphabet application but I'm not a Java developer. I've made an HTML page where there are about 150 <code>.png</code> pictures and <code>.mp3</code> sound file pairs. For example, <code>apple.png</code> and <code>apple.mp3</code> would be a pair, and there are going to be more.</p> <p>I'm using webview to display the webpage with pictures and to know when the user is trying to hear the sound. Here is the code I am currently using:</p> <pre><code>index.html: ...&lt;a href="mp3/apple.mp3"&gt;&lt;img src="apple.png"&gt;&lt;/a&gt;... alphabetActivity.java: ...public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.endsWith(".mp3")){ MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(url)); Toast.makeText(HelloWebviewActivity.this, url, Toast.LENGTH_SHORT).show(); mediaPlayer.start(); } else { Toast.makeText(HelloWebviewActivity.this, "not mp3", Toast.LENGTH_SHORT).show(); } return true; }... </code></pre> <p>All sounds are stored at <code>assets/www/mp3</code>.</p> <p>.</p> <p>But there is a problem: every time I click on a picture, my application crashes with a <code>Forced close...</code> message. Is there any way to make it work?</p> <h2>Found my own solution for this problem.</h2> <p>I've copied all the sounds to res/raw folder, changed links in index.html from "mp3/apple.mp3" to "apple" and used this code:</p> <pre><code>if(mPlayer!=null){ mPlayer.stop(); mPlayer.release();} int id = getResources().getIdentifier(url.substring(26), "raw", getPackageName());; mPlayer = MediaPlayer.create(getApplicationContext(), id); mPlayer.start(); </code></pre> <p>Right now this code is working. Thanks for help =)</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