- Joined
- Aug 24, 2007
I'm porting an html hymnal to Android for my mom's church. I got it running just fine, although it needs some tweaking of things like back button, etc. That should be easy.
It just opens a webview to index.html when you run the app. Navigation within the html files works fine, as do the .css and javascript. I have the whole thing stored in /assets. The one catch is the .mid files which give the tune for each hymn. I want them to play when the user clicks on play, and it's not working.
Here is the mainactivity:
So far so good, works fine. I thought if I made a new class that extends WebViewClient, and includes a url override, it would grab the .mid file and use MediaPlayer to play it, while staying on the same page. It's just not happening. This is the one I added, found it on stackoverflow:
I'm just starting to learn about Android and Java, so I have no idea if that MyWebViewClient class just needs to exist, and WebViewClient will alter its behavior according to what I put in the override, or if I'm just doing this completely wrong. Be gentle if I'm doing something totally stupid.
Here is an example of one of the hymn html files:
The part with the ../../midi/e0008.mid is clickable as 'play,' but it doesn't do anything. If I take out the ../../midi/ part of the url, and change the MyWebViewClient class (it's called a class, right?) url.replace to point directly to file:///android_asset/Hymnal/midi/ it says it can't find the file at all.
I don't know if the url override is working at all, and if it is working, I don't know why the mediaplayer isn't playing the .mid file.
Help?
edit: OK, I figured out that I need to (I think) import com.lsmhymnal.MyWebViewClient into mainactivity, and I did. Also changed the new WebViewClient to new MyWebViewClient. Now I get constructor is undefined regarding MyWebViewClient.
It just opens a webview to index.html when you run the app. Navigation within the html files works fine, as do the .css and javascript. I have the whole thing stored in /assets. The one catch is the .mid files which give the tune for each hymn. I want them to play when the user clicks on play, and it's not working.
Here is the mainactivity:
Code:
package com.lsmhymnal;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
WebView webview = new WebView(this);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setAllowFileAccess(true);
webview.loadUrl("file:///android_asset/Hymnal/index.html");
webview.setWebViewClient(new WebViewClient() {
}
);
this.setContentView(webview);
}
}
So far so good, works fine. I thought if I made a new class that extends WebViewClient, and includes a url override, it would grab the .mid file and use MediaPlayer to play it, while staying on the same page. It's just not happening. This is the one I added, found it on stackoverflow:
Code:
package com.lsmhymnal;
import java.io.IOException;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient{
public MediaPlayer mp;
private Context context = null;
public MyWebViewClient(Context c){
this.context = c;
mp = new MediaPlayer();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.endsWith(".mid")){
url = url.replace("file:///android_asset/Hymnal/", "");
Log.i("MyWebViewClient", url);
try {
AssetFileDescriptor afd = context.getAssets().openFd(url);
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}else{
return true;
}
}
}
I'm just starting to learn about Android and Java, so I have no idea if that MyWebViewClient class just needs to exist, and WebViewClient will alter its behavior according to what I put in the override, or if I'm just doing this completely wrong. Be gentle if I'm doing something totally stupid.
Here is an example of one of the hymn html files:
Code:
<html>
<head>
<title>Hymn 8</title>
<link type=text/css rel=stylesheet href=../a.css></link>
<link type=text/css rel=stylesheet href=../real.css></link>
<script src=../js/a.js></script>
</head>
<body><a style=visibility:hidden name=top></a>
<script>go_hymn_box()</script>
<table cellpadding=0 cellspacing=0 class=title>
<tr><td><div class=titlebox>
<h2><a href=../index.html><span class=white>Hymns, #8</span></a></h2>
<h3><a href=../categories/Blessing_of_the_Trinity.html#His_Praise><span class=white>Blessing of the Trinity - His Praise</span></a></h3>
</div>
</td></tr>
</table>
<script>embed_music("../../midi/e0008.mid")</script>
<div class=metabox>
<p class=nums>Eng:<span class=nums>8</span> Chin:<span class=nums>5</span> Kor:<span class=nums>5</span> Span:<span class=nums>4</span> Tag:<span class=nums>8</span> Fra:<span class=nums>-</span> Por:<span class=nums>2</span> </p>
<table cellspacing=0 cellpadding=0 style=margin:0px><tr><td class=lmm><span class=link>Lyrics</span></td><td class=colon>:</td><td class=small><b><a href=../authors/list/Thomas_Ken.html><span class=link>Thomas Ken</span></a></b></td></tr>
<tr><td class=lmm><span class=link>Music</span></td><td class=colon>:</td><td class=small><b>From <a href=../authors/list/_i_Genevan_Psalter__i___1551.html><span class=link><i>Genevan Psalter</i>, 1551</span></a></b></td></tr>
<tr><td class=lmm><span class=link>Meter</span></td><td class=colon>:</td><td class=small><b><a href="../meters/8.8.8.8..html#hymn8"><span class=link>8.8.8.8.</span></a></b></td></tr>
</table>
<p class=sametune><a href=../sametunes/1.html#link8><span class=link>This hymn has a unique tune</span></a></p>
</div>
<div class=linksbox><p class=links><a href="../index.html"><span class=link>Home</span></a><script>document.write(' | <a href="../../midi/e0008.mid"><span class=link>Play</span></a>')</script> | <a href="../browsehymns/1.html#hymn8"><span class=link>Browse</span></a> | <a href="7.html"><span class=link>Prev</span></a> | <a href="9.html"><span class=link>Next</span></a></p></div>
<div class=vlinksbox>
<p class=boxlinks><a class=boxheader name=0 href=#top><span class=link>Verses:</span></a>
<a class=greenbox href=#1><span class=wbox> 1 </span></a>
<a class=greenbox href=#2><span class=wbox> 2 </span></a>
</p></div>
<a class=br name=1> </a>
<div class=verse>
<p class=line><a class=greenbox href=#0><span class=wbox> 1 </span></a> Praise God, from whom all blessings flow;</p>
<p class=line><a class=unseenbold> </a> Praise Him, all creatures here below;</p>
<p class=line><a class=unseenbold> </a> Praise Him above, ye heav'nly host;</p>
<p class=line><a class=unseenbold> </a> Praise Father, Son, and Holy Ghost!</p>
</div>
<a class=br name=2> </a>
<div class=verse>
<p class=line><a class=greenbox href=#0><span class=wbox> 2 </span></a> Praise God the Father who's the source;</p>
<p class=line><a class=unseenbold> </a> Praise God the Son who is the course;</p>
<p class=line><a class=unseenbold> </a> Praise God the Spirit who's the flow;</p>
<p class=line><a class=unseenbold> </a> Praise God, our portion here below!</p>
</div>
<a class=br> </a>
<div class=vlinksbox>
<p class=boxlinks><a class=boxheader href=#0><span class=link>Verses</span></a>:
<a class=greenbox href=#1><span class=wbox> 1 </span></a>
<a class=greenbox href=#2><span class=wbox> 2 </span></a>
</p></div>
<div class=linksbox><p class=links><a href="../index.html"><span class=link>Home</span></a> | <a href="#top"><span class=link>Top</span></a><script>document.write(' | <a href="../../midi/e0008.mid"><span class=link>Play</span></a>')</script> | <a href="../browsehymns/1.html#hymn8"><span class=link>Browse</span></a> | <a href="7.html"><span class=link>Prev</span></a> | <a href="9.html"><span class=link>Next</span></a></p></div>
<br><br><br><br><br>
<table height=60%><tr><td></td></tr></table>
</body>
</html>
The part with the ../../midi/e0008.mid is clickable as 'play,' but it doesn't do anything. If I take out the ../../midi/ part of the url, and change the MyWebViewClient class (it's called a class, right?) url.replace to point directly to file:///android_asset/Hymnal/midi/ it says it can't find the file at all.
I don't know if the url override is working at all, and if it is working, I don't know why the mediaplayer isn't playing the .mid file.
Help?
edit: OK, I figured out that I need to (I think) import com.lsmhymnal.MyWebViewClient into mainactivity, and I did. Also changed the new WebViewClient to new MyWebViewClient. Now I get constructor is undefined regarding MyWebViewClient.
Last edited: