Unable to play audio file (.mp3) in Android through Phonegap - android

I am trying to develop an android application which can play audio files, through Phonegap framework.
Below is the code for that, which is not the index.html, but another HTML page in the project.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>A New App Try</title>
<!--
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.1/jquery.mobile-1.2.1.min.js"></script>
-->
<script src="/android_asset/www/js/corodova.js"></script>
<link rel="stylesheet" href="/android_asset/www/css/MyownTry.css"/>
<script type="text/javascript" src="cordova.js"></script>
<script src="/android_asset/www/js/jquery-1.10.2.js"></script>
<script src="/android_asset/www/js/jquery.mobile-1.3.2.js"></script>
<link rel="stylesheet" href="/android_asset/www/css/jquery.mobile.structure-1.3.2.css" />
<script type="text/javascript" charset="utf-8">
//Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
/*
function onDeviceReady() {
playAudio("../assets/files/Roja_Flute.mp3");
}
*/
// Audio player
//
var my_media = null;
var mediaTimer = null;
// Play audio
//
function playAudio(src) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
if (!playing) {
my_media.play();
document.getElementById('play').src = "/android_asset/www/img/pause_icon.jpg";
playing = true;
} else {
my_media.pause();
document.getElementById('play').src = "/android_asset/www/img/Playicon.png";
playing = false;
}
}
// Pause audio
//
/* function pauseAudio() {
if (my_media) {
my_media.pause();
}
}*/
// Stop audio
//
function stopAudio() {
my_media.stop();
playing = false;
document.getElementById('play').src = "/android_asset/www/img/Playicon.png";}
// onSuccess Callback
//
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Set audio position
//
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML = position;
}
</script>
<style>
img{
width=30%;
}
</style> </head>
<body>
<br/><br/>
<img id="play" src="/android_asset/www/img/Playicon.png"/>
<!-- <img id="pause"src="/android_asset/www/img/pause_icon.jpg" width="20%"/> -->
<img id="stop" src="/android_asset/www/img/stop_icon.jpg" />
<!-- <p id="audio_position"></p>-->
<br/><br/>
Go back to Home Page
<div data-role="footer">
<p>Copyright 2013 | Mani C | newtolearn.android</p>
</div>
</div>
</body>
</html>
I am expecting icons to be smaller ones, as I have defined in the style element, for 30% of image size. And, I am expecting when I click on the Play icon, the audio file to be played.
Unfortunately both the expected behaviors are not occuring. I am seeing a larger 'Play icon', smaller 'Stop icon' and getting, 'Uncaught Reference Exception: variable playAudio is undefined in index.html'(when in fact, the above HTML code has nothing to do with index.html).
Can somebody, please help and can possibly provide a workaround code..?
Thanks in advance!
Mani

Related

working samples for using cordova media plugin to play sound in intel xdk

Can anybody please recommend a working example for using cordova media plugin in intel xdk environment to play sound on android phones? I tried this example (http://qnimate.com/create-a-music-player-app-using-intel-xdk/) without luck. The code for that sample is attached. I tried html5 audio tag and Soundjs library but they don't play sound on android native browser. Some online posts suggest the cordova media plugin would be a fix for android browser. However, I googled a lot and still cannot find a working example. Your help will be much appreciated.
<!DOCTYPE html>
<html>
<!--
* Please see the included README.md file for license terms and conditions.
-->
<head>
<title></title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<style>
#-ms-viewport { width: 100vw ; zoom: 100% ; }
#viewport { width: 100vw ; zoom: 100% ; }
#-ms-viewport { user-zoom: fixed ; }
#viewport { user-zoom: fixed ; }
</style>
<script src="js/fastclick.js"></script>
<link rel="stylesheet" href="css/app.css">
</head>
<body>
Play Audio
Pause Playing Audio
Stop Playing Audio
<p id="audio_position"></p>
<script src="intelxdk.js"></script>
<script src="cordova.js"></script>
<script src="xhr.js"></script>
<script src="js/cordova-init.js"></script>
<script src="js/app.js"></script>
<script src="js/init-app.js"></script>
<script src="js/init-dev.js"></script>
<script>
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
//http://labs.qnimate.com/sound.mp3
playAudio("http://labs.qnimate.com/sound.mp3");
}
// Audio player
var my_media = null;
var mediaTimer = null;
// Play audio
//
function playAudio(src) {
if (my_media == null) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
} // else play current audio
// Play audio. playAudioWhenScreenIsLocked make sure the audio is playing even if screen is locked.
my_media.play({ playAudioWhenScreenIsLocked : true });
// Update my_media position every second
if (mediaTimer == null) {
mediaTimer = setInterval(function() {
// get my_media position
my_media.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
setAudioPosition((position) + " sec");
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
}
// Pause audio
//
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
// Stop audio
//
function stopAudio() {
if (my_media) {
my_media.stop();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
// onSuccess Callback
//
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Set audio position
//
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML = position;
}
</script>
</body>
</html>

gap:["Device","getDeviceInfo","Device1231860141"] error on PhoneGap

I have created a phonegap app and I added Android platform to it. But unfortunately, deviceready event is not getting fired. It works fine on ripple emulator but it is not working on chrome (desktop) and on the android phone as well.
If I run it on chrome (desktop) or my Android L phone, I get the error gap:
["Device","getDeviceInfo","Device1231860141"]
and the browser as well as NetBeans hangs.
I have the cordova.js file in my www folder. I tried removing it, and the error goes away, but deviceready is still not fired.
The following is my code:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=320, user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" href="js/libs/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.css"/>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/libs/jquery/jquery.js"></script>
<script type="text/javascript">
function init(){
document.addEventListener("deviceready", onDeviceReady, true);
}
function onDeviceReady() {
startWatch();
}
// Start watching the acceleration
//
function startWatch() {
// Update acceleration every 3 seconds
var options = { frequency: 3000 };
watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
}
// Stop watching the acceleration
//
function stopWatch() {
if (watchID) {
navigator.accelerometer.clearWatch(watchID);
watchID = null;
}
}
// onSuccess: Get a snapshot of the current acceleration
//
function onSuccess(acceleration) {
var element = document.getElementById('accelerometer');
element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
'Acceleration Y: ' + acceleration.y + '<br />' +
'Acceleration Z: ' + acceleration.z + '<br />' +
'Timestamp: ' + acceleration.timestamp + '<br />';
}
// onError: Failed to get the acceleration
//
function onError() {
alert('onError!');
}
</script>
</head>
<body onload="init()">
</body>
</html>
deviceready is a cordova event it will not work in browser.
...
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, true);
function onDeviceReady() {
startWatch();
}
...
</script>
</head>
<body onload="onDeviceReady()">
</body>
</html>

Ripple Emulator and cordova code accessing file system doesn't run

I am buiding an app with Cordova 3.6.3 in netbeans and trying to emulate with Ripple 0.9.15.
Whenever I run the code I keep getting:
Uncaught TypeError: Cannot read property 'fire' of undefined ripple.js:40
deviceready has not fired after 5 seconds. cordova.js:1168
Channel not fired: onPluginsReady cordova.js:1161
Channel not fired: onCordovaReady
The code is supposed to access the file system, create some files and display them in
a listview. Here it is:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Directory Reader</title>
<meta name="viewport" content="user-scalable=no,
initial-scale=1, maximum-scale=1,
minimum-scale=1, width=device-width" />
<link rel="stylesheet" href="css/jquery.mobile-1.4.3.min.css" />
<script type="text/javascript" src="js/libs/jquery/jquery-1.10.0.min.js"></script>
<script src="js/libs/jquery-mobile-1.4.3/jquery.mobile-1.4.3.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {navigator.alert('file access');
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0, onFileSystemSuccess, fail
); navigator.alert('file access');
}
function onFileSystemSuccess(fileSystem) {
// Create some test files
fileSystem.root.getDirectory("myDirectory",
{create: true, exclusive: false},
null, fail);
fileSystem.root.getFile("readthis.txt",
{create: true, exclusive: false},
null, fail);
var directoryReader = fileSystem.root.createReader();
// Get a list of all the entries in the directory
directoryReader.readEntries(success, fail);
}
function success(entries) {
var i;
var objectType;
for (i = 0; i < entries.length; i++) {
if (entries[i].isDirectory === true) {
objectType = 'Directory';
} else {
objectType = 'File';
}
$('#directoryList').append('<li><h3>' + entries[i].name + '</h3><p>' + entries[i].toURI() + '</p><p class="ui-li-aside">Type:<strong>' + objectType + '</strong></p></li>');
}
$('#directoryList').listview("refresh");
}
function fail(error) {
alert("Failed to list directory contents: " + error.code);
}
</script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h2>Directory Reader</h2>
</div>
<div data-role="content">
<ul id="directoryList" data-role="listview"
data-inset="true">
</ul>
</div>
</div>
</body>
</html>
Please what can I do?
Please what could the problem be?

Why is my phonegap camera app not working?

First the code:
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource,
destinationType
document.addEventListener("deviceready",loaded,false);
function loaded() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
function getPhoto(imageData) {
var smallImage = document.getElementById('smallImage');
smallImage.style.display = 'block';
smallImage.src = "data:image/jpeg;base64," + imageData;
}
function capturePhoto() {
navigator.camera.getPicture(getPhoto, onFail, { quality: 50 });
}
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
</body>
</html>
I use BuildTool in combination with phonegap (build.phonegap.com). I have zipped this file as index.zip and uploaded this file in BuildTool. BuildTool then creates an app for me and store this app on phonegap.
When I click the button, nothing happens. I won´t even see the failed alert.
Does someone know what I'm doing wrong here? Do I need to include some library or something?
Note: I am trying to get this to work for Android.
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
the above two line is not complete. it should be like these
pictureSource=navigator.camera.PictureSourceType.PHOTOLIBRARY;
destinationType=navigator.camera.DestinationType.FILE_URI;
after changing still not working then try these html its working...
<!DOCTYPE html>
<html>
<head>
<title>Submit form</title>
<script type="text/javascript" src="js/jquery-2.0.2.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
function getPhoto() {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFailure, {
destinationType: navigator.camera.DestinationType.FILE_URI,
sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
});
}
function onPhotoURISuccess(imageURI) {
$("#smallImage").attr('src',imageURI);
}
function onFailure()
{
alert('failed');
}
</script>
</head>
<body>
<br><br><br>
<button onclick="getPhoto();">Select Photo:</button><br>
<img style="width:200px;height:200px;" id="smallImage" />
</body>
</html>
Try adding the deviceready listener to a function that is called after the body loads, like this:
<body onload="onLoad()">
function onLoad() {
document.addEventListener("deviceready",loaded,false);
}

Open Play Store with PhoneGap

I'm developing a very simple webapp with Phonegapp, and I want to open the Play Store APP (Android), when the user clicks on a specific button. I read some q%a but I don't know how I can do it
I have a index.html like this:
<!DOCTYPE html>
<html>
<head>
<title>Just a development test ->dany_danay</title>
<link href="jquery-mobile/jquery.mobile-1.0a3.min.css" rel="stylesheet" type="text/css"/>
<script src="jquery-mobile/jquery-1.5.min.js" type="text/javascript"></script>
<script src="jquery-mobile/jquery.mobile-1.0a3.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
var watchID = null;
var pictureSource; // Origen de la imagen
var destinationType; // Formato del valor retornado
// Espera a que PhoneGap conecte con el dispositivo.
//
document.addEventListener("deviceready",onDeviceReady,false);
function alertDismissed() {
// do something
}
</script>
</head>
<body>
<div data-role="page" id="page">
<div data-role="header">
<h1>Your SmartPhone info</h1>
</div>
<center><a href="https://play.google.com/store/apps/details?id=com.tusmartphone.dany&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS50dXNtYXJ0cGhvbmUuZGFueSJd" rel="external" /><img src="http://media.go2speed.org/brand/files/offermobi/1981/20130605194659-WARFAL_b01_320X50_04.png"/></a>
</center>
</body>
</html>
I tried adding rel="external" or rel="system" in the
and this js function:
<script type="text/javascript">
$(function() {
updateAndroidMarketLinks();
// some more core here ...
function updateAndroidMarketLinks()
{
var ua = navigator.userAgent.toLowerCase();
if (0 <= ua.indexOf("android")) {
// we have android
$("a[href^='http://play.google.com/']").each(function() {
this.href = this.href.replace(/^http:\/\/play\.google\.com\//,
"market://");
});
}
}
});
</script>
I have to call a intent, but not sure how i can do this with html or js...
Dont know how to prove this: https://stackoverflow.com/questions/15325864/phonegap-android-open-play-store or this: How to Open Google Play store from Phonegap http://tannerburson.com/blog/2012/05/28/IntentChooser-my-first-PhoneGap-Cordova-plugin/
You want to use market links.
market://details?id=<package_name>
http://developer.android.com/distribute/googleplay/promote/linking.html

Categories

Resources