Sorry for my English.
Everything works if I pass an external route, an Internet URL. It does not work if I pass a local file.
MainView.ux
<App>
<Audio ux:Global="Audio" />
<JavaScript>
var Audio = require('Audio');
function play(){
Audio.Play("1.mp3");
}
function stop(){
Audio.Stop();
}
module.exports = {
play: play,
stop: stop
};
</JavaScript>
<StackPanel>
<Switch />
<Button Text="Play" Clicked="{play}" />
<Button Text="Stop" Clicked="{stop}" />
</StackPanel>
</App>
Audio.uno
using Uno;
using Uno.Collections;
using Uno.Compiler.ExportTargetInterop;
using Fuse;
using Fuse.Scripting;
using Uno.IO;
public class Audio : NativeModule
{
public Audio()
{ AddMember(new NativeFunction("Play", (NativeCallback)Play));
AddMember(new NativeFunction("Stop", (NativeCallback)Stop));
}
static object Play(Context c, object[] args)
{
string url = (string)args[0];
Play(url);
return null;
}
static object Stop(Context c, object[] args)
{
Stop();
return null;
}
[Foreign(Language.Java)]
static extern(Android) void Play(String url)
@{
debug_log("Uno -> Play");
com.my.Audio mc = new com.my.Audio();
mc.start(url);
@}
[Foreign(Language.Java)]
static extern(Android) void Stop()
@{
debug_log("Uno -> Stop - ");
com.my.Audio mc = new com.my.Audio();
mc.stop();
@}
static extern(!Android) void Play(String url){ debug_log "fail Play"; }
static extern(!Android) void Stop(){ debug_log "fail Stop"; }
}
Audio.java
package com.my;
import android.app.Application;
import android.media.MediaPlayer;
import android.media.AudioManager;
import java.io.IOException;
public class Audio
{
public MediaPlayer player;
public Audio()
{ player = new MediaPlayer();
}
public void start(String url) {
try {
player.setDataSource(url);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
try {
player.prepare();
} catch (IOException e) {
e.printStackTrace();
}
player.start();
}
public void stop() {
player.reset();
player.stop();
player.release();
}
}
.unoproj
{
"RootNamespace":"",
"Packages": [
"Fuse",
"FuseJS"
],
"Includes": [
"*",
"Audio.java:Java:Android",
"1.mp3:Bundle",
]
}
this helps ?
var filepath = Path.Combine(Directory.GetUserDirectory(UserDirectory.Data), url);