I am having trouble playing local files with jPlayer in an Android WebView.
The audio files are located in: 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;
}
}
}
In manifest.xml
I am only giving one permission which is for update checking:
<uses-permission android:name="android.permission.INTERNET" />
This is the error I get with Method 1 (see HTML above), and I get similar errors for the other methods:
/? 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)
I think that it's finding the file, because as soon as I change the a tags href to incorrect/sound.mp3
I get a different error (below). Also I'm loading images with a src of images/graphic.png
just fine.
com.example.myprogram E/AndroidProtocolHandler﹕ Unable to open asset URL: file:///android_asset/www/incorrect/sound.mp3
Also loading a mp3 audio file off of a web server works fine as well.
Update 1:
I tried what was suggested in this question, but then I get this error:
"XMLHttpRequest cannot load file:///android_asset/www/audio/sound.mp3. Origin null is not allowed by Access-Control-Allow-Origin.",