安卓:打一个资产声音使用的WebView [英] Android: Playing an Asset Sound Using WebView
问题描述
我想,当用户点击我的应用程序的WebView的标签从资产的文件夹播放声音。我发现我可以用一个新的类扩展WebViewClient检测链路的延伸,如果它是一个MP3文件就可以通过默认的音频播放器播放。但我想它的活动中发挥不启动新的活动。
I am trying to play a sound from the assets folder when a user clicks on an tag on my application's WebView. I found that I can use a new class extending WebViewClient to detect the extension of a link and if it is an mp3 file it can play it via the default Audio Player. But I want it to play within the activity without starting a new activity.
我用下面的链接作为参考: 在web视图 声音 和<一href="http://stackoverflow.com/questions/3093562/error-creating-mediaplayer-with-uri-or-file-in-assets">Error创建资产 MediaPlayer的使用URI或文件
I used the following links as references: Sound in Webview and Error creating MediaPlayer with Uri or file in assets
但没有奏效。
我试着用下面的codeS这样做的:
I tried using the following codes to do so:
package com.rangga.test.webview.sound;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyWebViewClient extends WebViewClient{
public MediaPlayer mp;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.endsWith(".mp3")){
mp = new MediaPlayer();
AssetFileDescriptor afd = getAssets().openFd(url);
mp.setDataSource(afd.getFileDescriptor());
mp.start();
return true;
}else{
return true;
}
}
}
我上getAssets一个未定义的方法错误(),我想Googleing它,它说,一些有关传递一个上下文。我很新到Android和面向对象编程,所以我想知道,如果这正确的方式做到这一点,如果有什么上下文是一个简单的解释。谢谢你。
I get an undefined method error on the getAssets(), I tried Googleing it and it says something about passing a Context. I am very new to Android and Object Oriented programming, so I am wondering if this the right way to do it and if there is a simple explanation on what a Context is. Thank you.
下面是我的完整codeS:
Below are my complete codes:
MainActivity.class
MainActivity.class
package com.rangga.test.webview.sound;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView webMain;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webMain = (WebView) findViewById(R.id.webMain);
webMain.loadUrl("file:///android_asset/webpages/test.html");
webMain.setWebViewClient(new MyWebViewClient());
}
}
修改
所以,codeS工作的感谢 http://stackoverflow.com/users/488241/squonk 但现在另外一个问题时,我更新了MyWebViewClient为以下:
So the codes works thanks to http://stackoverflow.com/users/488241/squonk but now another problem occurs, I updated the MyWebViewClient into the following:
package com.rangga.test.webview.sound;
import java.io.IOException;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
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;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.endsWith(".mp3")){
mp = new MediaPlayer();
try {
AssetFileDescriptor afd = context.getAssets().openFd(url);
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;
}
}
}
但我得到java.io.FileNotFoundException,则该路径似乎是正确的,但。我已经把2 mp3文件的资产/网页/声音/ 1.MP3和资产/网页/ 1.MP3并用下面的HTML codeS来测试它:
But I get a java.io.FileNotFoundException, the path seems right though. I've put 2 mp3 files in assets/webpages/sounds/1.mp3 and assets/webpages/1.mp3 and used the following html codes to test it:
<html>
<head>
<title>Test Audio</title>
</head>
<body>
<a href="file:///android_asset/webpages/sounds/1.mp3">File Asset</a><br/>
<a href="1.mp3">Same Folder</a>
</body>
</html>
不过,我得到LogCat中出现以下错误:
But I get the following error on LogCat:
06-11 06:20:37.955: W/System.err(377): java.io.FileNotFoundException: file:///android_asset/webpages/1.mp3
06-11 06:20:37.966: W/System.err(377): at android.content.res.AssetManager.openAssetFd(Native Method)
06-11 06:20:37.974: W/System.err(377): at android.content.res.AssetManager.openFd(AssetManager.java:314)
06-11 06:20:37.974: W/System.err(377): at com.rangga.test.webview.sound.MyWebViewClient.shouldOverrideUrlLoading(MyWebViewClient.java:30)
06-11 06:20:37.985: W/System.err(377): at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:193)
06-11 06:20:38.000: W/System.err(377): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:304)
06-11 06:20:38.005: W/System.err(377): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 06:20:38.005: W/System.err(377): at android.os.Looper.loop(Looper.java:123)
06-11 06:20:38.014: W/System.err(377): at android.app.ActivityThread.main(ActivityThread.java:4363)
06-11 06:20:38.014: W/System.err(377): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 06:20:38.024: W/System.err(377): at java.lang.reflect.Method.invoke(Method.java:521)
06-11 06:20:38.024: W/System.err(377): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-11 06:20:38.024: W/System.err(377): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-11 06:20:38.054: W/System.err(377): at dalvik.system.NativeStart.main(Native Method)
时有什么问题我调用该文件的方式吗?之前的感谢。
Is there something wrong with the way I called the file? Thanks before.
解决
THX到squonk的建议,我尝试以下codeS,使其工作和它的工作好了,谢谢你squonk!
Thx to squonk's suggestions, I tried the following codes to make it work and it worked well, thank you squonk!
package com.rangga.test.webview.sound;
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(".mp3")){
url = url.replace("file:///android_asset/webpages/", "");
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;
}
}
}
和HTML来测试它:
And the html to test it:
<html>
<head>
<title>Test Audio</title>
</head>
<body>
<a href="webpages/1.mp3">File Asset</a><br/>
</body>
</html>
虽然我没有得到一个错误,如果我反复点击它,但这是另外一个问题:D 一个人如何关闭一个人的问题吗? >&LT;
Though I did get an error if I repeatedly click it, but that's another question :D How does one close one's question? >.<
推荐答案
如果你声明你的的MediaPlayer
在 MyWebViewClient
添加以下...
Where you declare your MediaPLayer
in MyWebViewClient
, add the following...
public class MyWebViewClient extends WebViewClient{
public MediaPlayer mp;
private Context context = null; // Add this line
...
}
然后添加一个构造函数 MyWebViewClient
如下...
public MyWebViewClient(Context context) {
this.context = context;
}
然后在 shouldOverrideUrlLoading(...)
让你的资产情况如下...
Then in shouldOverrideUrlLoading(...)
get your assets as follows...
AssetFileDescriptor afd = context.getAssets().openFd(url);
在你的 MainActivity
设置 WebViewClient
通过传递这
(这是活动上下文
)如下...
In your MainActivity
set the WebViewClient
by passing this
(which is the Activity Context
) as follows...
webMain.setWebViewClient(new MyWebViewClient(this));
你也应该返回假
从 shouldOverrideUrlLoading(...)
如果URL以.MP3结束 - 这说明你的的WebView
正在处理的事情,在主机应用程序不应该启动股票Web浏览器的一个实例
Also you should return false
from shouldOverrideUrlLoading(...)
if the url ends with '.mp3' - this shows your WebView
is handling things and the 'host' application should not start an instance of the stock web browser.
编辑::要播放从资产目录中的音频文件,你需要设置不同的数据源 - 见接受的答案为<一个href="http://stackoverflow.com/questions/3289038/play-audio-file-from-the-assets-directory">play-audio-file-from-the-assets-directory.
To play audio files from the assets directory, you need to set the data source differently - see the accepted answer to play-audio-file-from-the-assets-directory.
你也应该调用之前调用 MP。prepare()
mp.start()
。
Also you should call mp.prepare()
before calling mp.start()
.
这篇关于安卓:打一个资产声音使用的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!