我创建了新的Android应用程序,因为它显示静态信息,我创建了 html5 并将html5文件调用到我的webview我的 html 文件功能之一是单击按钮时播放 mp3 我在 html 和android之间做了我的接口,但mp3没有玩 。
public class WebAppInterface extends Activity {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
public void audioPlayer() {
//set up MediaPlayer
MediaPlayer mp = new MediaPlayer();
try {
AssetFileDescriptor descriptor = getAssets().openFd("Azan.mp3");
mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//Call HTML Files
WebView myWebView = (WebView) findViewById(R.id.x);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/index.html");
//initiate interface
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
<input type="button" value="play mp3" onClick="playmp3()" />
<script type="text/javascript">
function playmp3()
{
Android.audioPlayer();
}
</script>