var myAudio = document.getElementById("myAudio");
var isPlaying = false;
function togglePlay() {
if (isPlaying) {
myAudio.pause()
} else {
myAudio.play();
}
};
myAudio.onplaying = function() {
isPlaying = true;
};
myAudio.onpause = function() {
isPlaying = false;
};
I can't really apply the above code anyways because I use the following in mine:
<p align="center" ><button onclick="playSound1()" class="styled">Sound1</button></p>
<script>
player1 = app.CreateMediaPlayer();
player1.SetFile( "SOUNDFILEGOESHERE.mp3" );
function playSound1()
{
player1.Play();
}
</script>
I know its a bit all over the place, but I am still learning to understand it all. Can you see where I made my mistake?
the first one looks like some html element and the second one like some DroidScript code but I don't see the toggle button. Are you having problems with the html toggle or a DS toggle button control?
var player, file = "/Sys/Snd/Poing.ogg";
function OnStart() {
player = app.CreateMediaPlayer();
player.SetFile(file);
player.SetOnComplete(player_OnComplete);
var lay = app.CreateLayout("linear","fillxy,vcenter");
btnToggle = app.CreateButton("Play");
btnToggle.SetOnTouch(btnToggle_OnTouch);
lay.AddChild(btnToggle);
app.AddLayout(lay);
}
function btnToggle_OnTouch() {
switch(btnToggle.GetText()) {
case "Play": case "Resume":
if(!player.IsPlaying()) {
player.Play();
btnToggle.SetText("Pause");
}
break;
case "Pause":
if(player.IsPlaying()) {
player.Pause();
btnToggle.SetText("Resume");
}
break;
}
}
function player_OnComplete() {
btnToggle.SetText("Play");
}
<html>
<head>
<meta name="viewport" content="width=device-width">
<script src='file:///android_asset/app.js'></script>
</head>
<script>
function OnStart() {
app.MakeFolder(app.GetAppPath() + "/Snd")
app.CopyFile("/Sys/Snd/Poing.ogg", app.GetAppPath()+"/Snd/Poing.ogg");
audio.src = "Snd/Poing.ogg";
audio.type = "audio/ogg";
}
</script>
<body onload="app.Start()" style="background-color:#ffffff;">
<audio id="audio" controls></audio>
</body>
</html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<script src='file:///android_asset/app.js'></script>
</head>
<script>
var player;
function OnStart() {
app.MakeFolder(app.GetAppPath() + "/Snd")
app.CopyFile("/Sys/Snd/Poing.ogg", app.GetAppPath()+"/Snd/Poing.ogg");
player = new Audio
player.onended = player_OnComplete;
player.src = "Snd/Poing.ogg";
player.type = "audio/ogg";
}
function btnToggle_OnTouch() {
switch(btnToggle.value) {
case "Play":
case "Resume":
if(player.paused) {
player.play();
btnToggle.value = "Pause";
}
break;
case "Pause":
if(!player.paused) {
player.pause();
btnToggle.value = "Resume";
}
break;
}
}
function player_OnComplete() {
btnToggle.value = "Play";
}
</script>
<body onload="app.Start()" style="background-color:#ffffff;">
<input id="btnToggle" type="button" value="Play" onclick="btnToggle_OnTouch()" onended="player_OnComplete()">
</body>
</html>
In some cases it's also a good idea to reuse the same mediaplayer objects (if there are a lot of sounds to play for exemple).
This post of Steve Garman might interest you as you're in a learning phase:
https://groups.google.com/d/msg/androidscript/MvnGyrAUOAY/Fj3bK-ZbAgAJ
function OnStart()
{
lay = app.CreateLayout( 'Linear', 'FillXY,VCenter' );
btn1 = app.CreateButton( 'Play sound 1' );
btn1.SetOnTouch( btn1_OnTouch );
lay.AddChild( btn1 );
btn2 = app.CreateButton( 'Play sound 2' );
btn2.SetOnTouch( btn2_OnTouch );
lay.AddChild( btn2 );
app.AddLayout( lay );
player = app.CreateMediaPlayer();
player.SetOnReady( player_OnReady );
}
function btn1_OnTouch()
{
player.SetFile( '/Sys/Snd/Poing.ogg' );
}
function btn2_OnTouch()
{
player.SetFile( '/Sys/Snd/Trill.ogg' );
}
function player_OnReady()
{
player.SetLooping( true );
player.Play();
}
function btn1_OnTouch()
{
player.SetFile( '/Sys/Snd/Poing.ogg' );
player.SetVolume( 3.0, 3.0 );
}
function btn2_OnTouch()
{
player.SetFile( '/Sys/Snd/Trill.ogg' );
player.SetVolume( 1.0, 1.0 );
So it becomes:
if( player.IsPlaying() )
{
player.Stop();
}
elle
{
player.Play();
}
function OnStart()
{
lay = app.CreateLayout( 'Linear', 'FillXY,VCenter' );
btn1 = app.CreateButton( 'Play sound 1' );
btn1.SetOnTouch( btn1_OnTouch );
lay.AddChild( btn1 );
btn2 = app.CreateButton( 'Play sound 2' );
btn2.SetOnTouch( btn2_OnTouch );
lay.AddChild( btn2 );
app.AddLayout( lay );
player = app.CreateMediaPlayer();
player.SetOnReady( player_OnReady );
}
function btn1_OnTouch()
{
player.SetFile( '/Sys/Snd/Poing.ogg' );
player.SetLooping( true );
}
function btn2_OnTouch()
{
player.SetFile( '/Sys/Snd/Trill.ogg' );
}
function player_OnReady()
{
if( player.IsPlaying() )
{
player.Stop();
}
else
{
player.Play();
}
Does that make sense?
function player_OnReady()
{
player.Play();
}
function OnStart()
{
lay = app.CreateLayout( 'Linear', 'FillXY,VCenter' );
btn1 = app.CreateButton( 'Play sound 1' );
btn1.SetOnTouch( btn1_OnTouch );
lay.AddChild( btn1 );
btn2 = app.CreateButton( 'Play sound 2' );
btn2.SetOnTouch( btn2_OnTouch );
lay.AddChild( btn2 );
app.AddLayout( lay );
player = app.CreateMediaPlayer();
player.SetOnReady( player_OnReady );
}
function btn1_OnTouch()
{
if( player.IsPlaying() )
{
player.Stop();
}
else
{
player.SetFile( '/Sys/Snd/Poing.ogg' );
}
}
function btn2_OnTouch()
{
if( player.IsPlaying() )
{
player.Stop();
}
else
{
player.SetFile( '/Sys/Snd/Trill.ogg' );
}
}
function player_OnReady()
{
player.SetLooping( true );
player.Play();
}
function OnStart()
{
lay = app.CreateLayout( 'Linear', 'FillXY,VCenter' );
btn1 = app.CreateToggle( 'Play sound 1' );
btn1.SetOnTouch( btn1_OnTouch );
lay.AddChild( btn1 );
btn2 = app.CreateToggle( 'Play sound 2' );
btn2.SetOnTouch( btn2_OnTouch );
lay.AddChild( btn2 );
app.AddLayout( lay );
player = app.CreateMediaPlayer();
player.SetOnReady( player_OnReady );
}
function btn1_OnTouch( isChecked )
{
if( isChecked == true )
{
player.SetFile( '/Sys/Snd/Poing.ogg' );
}
else
{
player.Stop();
}
// Uncheck the other button
btn2.SetChecked( false );
}
function btn2_OnTouch( isChecked )
{
if( isChecked == true )
{
player.SetFile( '/Sys/Snd/Trill.ogg' );
}
else
{
player.Stop();
}
// Uncheck the other button
btn1.SetChecked( false );
}
function player_OnReady()
{
player.SetLooping( true );
player.Play();
}