Android WebView中的文件没有声音播放MP3

时间:2013-12-31 09:19:14

标签: android audio webview jplayer

我在Android WebView中使用jPlayer播放本地文件时遇到问题。

音频文件位于:src\main\assets\www\audio\

HTML

<a href="audio/sound.mp3" class="sound">Method 1</a>
<a href="/assets/www/audio/sound.mp3" class="sound">Method 2</a>
<a href="/sounds/b.mp3" class="sound">Method 3</a>
<a href="http://example.com/sound.mp3" class="sound">This works</a>
<div id="jquery_jplayer"></div>

jQuery的:

$(document).ready(function(){
    $("#jquery_jplayer").jPlayer({
        swfPath: "scripts/",
        supplied: "mp3",
        solution:"flash,html",
        wmode: "window"
    });

    $('.sound').click(function(e) {
        e.preventDefault();
        $("#jquery_jplayer")
            .jPlayer("setMedia", {mp3: this.href })
            .jPlayer("play");
    });
});

的.java:

package com.example.myprogram;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.media.MediaPlayer;

public class myprogram extends ActionBarActivity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myprogram_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.setWebChromeClient(new WebChromeClient());

        mWebView.loadUrl("file:///android_asset/www/index.html");

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        // Stop local links and redirects from opening in browser instead of WebView
        //mWebView.setWebViewClient(new MyAppWebViewClient());

        // allow local storage
        mWebView.getSettings().setDomStorageEnabled(true);

    }

    @Override
    // Detect when the back button is pressed
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.myprogram, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

manifest.xml中我只给出一个用于更新检查的权限:

<uses-permission android:name="android.permission.INTERNET" />

这是我用方法1得到的错误(参见上面的HTML),我得到了其他方法的类似错误:

/? E/﹕ Failed to open file '/android_asset/www/audio/sound.mp3'. (No such file or directory)
/com.example.myprogram E/MediaPlayer﹕ error (1, -2147483648)
/com.example.myprogram E/MediaPlayer﹕ Error (1,-2147483648)

我认为它正在找到该文件,因为只要我将a标签href更改为incorrect/sound.mp3,我就会得到一个不同的错误(下面)。另外我正在使用images/graphic.png的src加载图像就好了。

com.example.myprogram E/AndroidProtocolHandler﹕ Unable to open asset URL: file:///android_asset/www/incorrect/sound.mp3

同样从网络服务器上加载mp3音频文件也可以正常工作。

更新1:

我尝试了this question中的建议,但后来我收到了这个错误:

"XMLHttpRequest cannot load file:///android_asset/www/audio/sound.mp3. Origin null is not allowed by Access-Control-Allow-Origin.",

0 个答案:

没有答案