Skip to content

Commit

Permalink
AudioClip: Assorted improvements to playback and state handling
Browse files Browse the repository at this point in the history
The player has to be prefetched once it's created, otherwise making
volume changes on play() won't work. Also resume() should only
actually resume playing if the player is paused, AND it has not
reached the end of the media. play() will also behave like Nokia
Sound and always play from the beginning.
  • Loading branch information
AShiningRay committed Dec 16, 2024
1 parent 4d61048 commit 007f5af
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/com/samsung/util/AudioClip.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public AudioClip(int clipType, byte[] audioData, int audioOffset, int audioLengt

}

try { player = Manager.createPlayer(new ByteArrayInputStream(audioData, audioOffset, audioLength), formatMIMEType[clipType-1]); }
try
{
player = Manager.createPlayer(new ByteArrayInputStream(audioData, audioOffset, audioLength), formatMIMEType[clipType-1]);
player.prefetch();
}
catch (Exception e) {Mobile.log(Mobile.LOG_ERROR, AudioClip.class.getPackage().getName() + "." + AudioClip.class.getSimpleName() + ": " + "AudioClip: Failed to create player:" + e.getMessage()); }
}

Expand All @@ -80,7 +84,11 @@ public AudioClip(int clipType, String filename)

InputStream stream = null; // TODO: Actually load this stream from the locator provided

try { player = Manager.createPlayer(stream, formatMIMEType[clipType-1]); }
try
{
player = Manager.createPlayer(stream, formatMIMEType[clipType-1]);
player.prefetch();
}
catch (Exception e) {Mobile.log(Mobile.LOG_ERROR, AudioClip.class.getPackage().getName() + "." + AudioClip.class.getSimpleName() + ": " + "AudioClip: Failed to create player:" + e.getMessage()); }
}

Expand All @@ -94,16 +102,20 @@ public void play(int loop, int volume)

try
{
if (loop == 0) { loop = -1; }
if (player.getState() == Player.STARTED) { player.stop(); }
player.setLoopCount(loop);
player.setMediaTime(0); // play() should always play media from the beginning, like Nokia Sound
player.setLoopCount((loop == 0 || loop == 255) ? -1 : loop); // Treat 0 loops, and the max allowed value as infinite looping
((VolumeControl) player.getControl("VolumeControl")).setLevel(volume * 20); // Received volume varies from 1 to 5, so adapt
player.start();
}
catch (Exception e) {Mobile.log(Mobile.LOG_ERROR, AudioClip.class.getPackage().getName() + "." + AudioClip.class.getSimpleName() + ": " + "AudioClip: Failed to play():" + e.getMessage()); }
}

public void resume() { player.start(); }
public void resume()
{
/* Resume only restarts the player if it is paused, AND its current saved position is not at the end of the media. Otherwise, this results in infinite playback loops */
if(player.getState() == Player.PREFETCHED && (player.getMediaTime() < player.getDuration())) { player.start(); }
}

public void stop() { player.close(); }

Expand Down

0 comments on commit 007f5af

Please sign in to comment.