Skip to content

Instantly share code, notes, and snippets.

@webstory
Last active June 27, 2016 07:39
Show Gist options
  • Save webstory/663c71c22055ce786b50c92171045328 to your computer and use it in GitHub Desktop.
Save webstory/663c71c22055ce786b50c92171045328 to your computer and use it in GitHub Desktop.
Fusetools Android MediaPlayer
package com.example;
import android.media.MediaPlayer;
import android.media.AudioManager;
import java.io.IOException;
public class AndroidSound {
private static AndroidSound instance = null;
private static MediaPlayer mediaPlayer = null;
private AndroidSound() {
mediaPlayer = new MediaPlayer();
}
public static AndroidSound getInstance() {
if(instance == null) {
instance = new AndroidSound();
}
return instance;
}
public void play(String uri) {
try {
if(mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(uri);
mediaPlayer.prepare();
mediaPlayer.start();
} catch(IOException e) {
// Do nothing
} catch(Exception e) {
// Do nothing
}
}
}
<App>
<DockPanel>
<StatusBarBackground Dock="Top" />
<BottomBarBackground Dock="Bottom" />
<SystemSounds ux:Global="SystemSounds" />
<JavaScript>
'use strict'
const SystemSounds = require("SystemSounds");
const uri = "https://example.com/1.mp3";
const uri2 = "https://example.com/2.mp3";
function button_pressed() {
SystemSounds.playMusic(uri);
}
function button_pressed2() {
SystemSounds.playMusic(uri2);
}
module.exports = {
button_pressed,
button_pressed2
};
</JavaScript>
<StackPanel>
<Button Text="Press me!" Clicked="{button_pressed}" />
<Button Text="Press me too!" Clicked="{button_pressed2}" />
</StackPanel>
</DockPanel>
</App>
{
"RootNamespace":"",
"Packages": [
"Fuse",
"FuseJS"
],
"Includes": [
"*",
"AndroidSound.java:Java:Android"
]
}
using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Uno.Compiler.ExportTargetInterop;
public class SystemSounds : NativeModule {
public SystemSounds() {
AddMember(new NativeFunction("playNotification", (NativeCallback)PlayNotification));
AddMember(new NativeFunction("playMusic", (NativeCallback)PlayMusic));
}
object PlayNotification(Context c, object[] args) {
playNotification();
return null;
}
object PlayMusic(Context c, object[] args) {
var uri = (string)args[0];
playMusic(uri);
return null;
}
[Foreign(Language.Java)]
static extern(Android) void playNotification()
@{
android.net.Uri uri =
android.media.RingtoneManager.getDefaultUri(
android.media.RingtoneManager.TYPE_NOTIFICATION
);
android.media.Ringtone ringtone =
android.media.RingtoneManager.getRingtone(
com.fuse.Activity.getRootActivity(), uri
);
ringtone.play();
@}
static extern(!Android) void playNotification() {
debug_log("Notification not supported on this platform.");
}
[Foreign(Language.Java)]
static extern(Android) void playMusic(string uri)
@{
com.example.AndroidSound player = com.example.AndroidSound.getInstance();
player.play(uri);
@}
static extern(!Android) void playMusic(string uri) {
debug_log("Mediaplayer not supported on this platform.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment