Script Audio Interface untuk Webview


Sebelumnya, tambahkan web.addJavascriptInterface(new AudioInterface(this), “AndAud”); di MainActivity.java

Script AudioInterface.java

package com.mzaini30.ngetes;

import java.io.IOException;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.webkit.JavascriptInterface;

public class AudioInterface {
Context mContext;

AudioInterface(Context c) {
mContext = c;
}

//Play an audio file from the webpage
@JavascriptInterface
public void playAudio(String aud) { //String aud – file name passed from the JavaScript function

final MediaPlayer mp;

try {
AssetFileDescriptor fileDescriptor =
mContext.getAssets().openFd(aud);
mp = new MediaPlayer();
mp.setDataSource(fileDescriptor.getFileDescriptor(),
fileDescriptor.getStartOffset(),
fileDescriptor.getLength());
fileDescriptor.close();
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

// Cara Memanggil Audio
// AndAud.playAudio(“halo.mp3”); …… di dalam <script>

Iklan

One thought on “Script Audio Interface untuk Webview

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s