Create a Local Player Music

Hi guys I’m trying to create a simple music player that reads music from local app folder (Songs), bug I receve some errors. How can I use MediaPlayer obj (I saw Fuse streaming music simple app but it isnt’t for me).

MainView.ux

<App>
    <MusicPlayer ux:Global="MusicPlayer" />
    <JavaScript>
        var MusicPlayer = require("MusicPlayer");

        function play_sound(){
            MusicPlayer.InitPlayer("Songs/sesso_videogiochi.mp3");  // Select song
        }
        
        module.exports={
            play_sound:play_sound
        };
    </JavaScript>
    <ClientPanel>
        <DockPanel>
            <Panel Color="#a1b3c4" Height="150" Clicked="{play_sound}">
                <Text Alignment="Center" Color="#fff">Avvia</Text>
            </Panel>
        </DockPanel>
    </ClientPanel>
</App>

MusicPlayer.uno

using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;

using Uno.Compiler.ExportTargetInterop;

public class MusicPlayer : NativeModule 
{

    public MusicPlayer(){
        AddMember( new NativeFunction("InitPlayer",
            (NativeCallback)InitPlayer ));
    }

    object InitPlayer(Context c, object[] args){

        var arg = args[0] as string;
        
        InitPlayer(c,arg);
        
        return null;
    }

    [Foreign(Language.Java)]
    static extern(Android) void InitPlayer(Context c,string music_file)
    @{
        MediaPlayer mp =  new MediaPlayer();
        mp.setDataSource(music_file);
        mp.prepare();
        mp.start();
    @}

    static extern(!Android) void InitPlayer(Context c,string music_file){
        debug_log("Funzione non disponibile per questo dispositivoooo");
    }

}

Error Log

D:\Documents\Fuse\Music Player Test\build\Android\Debug\app\src\main\java\com\foreign\MusicPlayer.java:28: error: cannot find symbol
        MediaPlayer mp =  new MediaPlayer();
        ^
  symbol:   class MediaPlayer
  location: class MusicPlayer
D:\Documents\Fuse\Music Player Test\build\Android\Debug\app\src\main\java\com\foreign\MusicPlayer.java:28: error: cannot find symbol
        MediaPlayer mp =  new MediaPlayer();
                              ^
  symbol:   class MediaPlayer
  location: class MusicPlayer



Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

(unknown): E0200: Android build failed

Incremental java compilation is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2340Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72340Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2340Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72340Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42340Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2340Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugJavaWithJavac
:app:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
:app:compileDebugJavaWithJavac FAILED

BUILD FAILED

Total time: 11.107 secs
(13.1 s)

Build completed in 21.75 seconds
    1 error

You’re missing a ForeignInclude, as seen here. As a result, the Java code doesn’t know where to look up that MediaPlayer type.

Fixed your Uno code so it at least builds:

using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;

using Uno.Compiler.ExportTargetInterop;

[ForeignInclude(Language.Java,
                "android.media.MediaPlayer",
                "android.media.AudioManager",
                "java.lang.Exception")]
public class MusicPlayer : NativeModule 
{

    public MusicPlayer(){
        AddMember( new NativeFunction("InitPlayer",
            (NativeCallback)InitPlayer ));
    }

    object InitPlayer(Context c, object[] args){

        var arg = args[0] as string;
        
        InitPlayer(c,arg);
        
        return null;
    }

    [Foreign(Language.Java)]
    static extern(Android) void InitPlayer(Context c,string music_file)
    @{
        MediaPlayer mp =  new MediaPlayer();
        try {
            mp.setDataSource(music_file);
            mp.prepare();
            mp.start();
        } catch (Exception e) {
            // should log the exception
        }
    @}

    static extern(!Android) void InitPlayer(Context c,string music_file){
        debug_log("Funzione non disponibile per questo dispositivoooo");
    }

}

Thanks I update it and I add DRM manegment but i receive these errors:

NativeModule Updated

using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;

using Uno.Compiler.ExportTargetInterop;

[ForeignInclude(Language.Java,
                "android.media.MediaPlayer",
                "android.media.AudioManager",
                "java.lang.Exception")]
[Require("AndroidManifest.ApplicationElement", "<service android:name=\"streamingPlayer.android.BackgroundAudioService\" />")]
public class MusicPlayer : NativeModule 
{

    public MusicPlayer(){
        AddMember( new NativeFunction("InitPlayer",
            (NativeCallback)InitPlayer ));
    }

    object InitPlayer(Context c, object[] args){

        var arg = args[0] as string;
        
        InitPlayer(c,arg);
        
        return null;
    }

    [Foreign(Language.Java)]
    static extern(Android) void InitPlayer(Context c,string music_file)
    @{
        debug_log("Inizializzo Media Player\n");
        MediaPlayer mp =  new MediaPlayer();
        try {

            debug_log("Inizializzo Stream e Canzone\n");

            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setDataSource(music_file);

            mp.prepare();
            if (mp.getDrmInfo() != null) {
                
                debug_log("Sistemo i drm\n");
                mp.prepareDrm();
                mp.getKeyRequest();
                mp.provideKeyResponse();
            }

            mp.start();

            debug_log(" ------- \n\nLa musica è partita\n\n -----------     ");
        } catch (Exception e) {
            // should log the exception
        }
    @}

    static extern(!Android) void InitPlayer(Context c,string music_file){
        debug_log("Funzione non disponibile per questo dispositivoooo");
    }

}

Error:

Fuse 1.5.0 (build 15046)
Starting preview for d:\/Documents/Fuse/Music Player Local with Native/Music Player Local with Native.unoproj, target Android
Configuring (0.8 s)
Compiling syntax tree (3.1 s)
Generating code and data (5.2 s)
Building Android app
D:\Documents\Fuse\Music Player Local with Native\build\Android\Preview\app\src\main\java\com\foreign\MusicPlayer.java:43: error: cannot find symbol
            if (mp.getDrmInfo() != null) {
                  ^
  symbol:   method getDrmInfo()
  location: variable mp of type MediaPlayer
D:\Documents\Fuse\Music Player Local with Native\build\Android\Preview\app\src\main\java\com\foreign\MusicPlayer.java:46: error: cannot find symbol
                mp.prepareDrm();
                  ^
  symbol:   method prepareDrm()
  location: variable mp of type MediaPlayer
D:\Documents\Fuse\Music Player Local with Native\build\Android\Preview\app\src\main\java\com\foreign\MusicPlayer.java:47: error: cannot find symbol
                mp.getKeyRequest();
                  ^
  symbol:   method getKeyRequest()
  location: variable mp of type MediaPlayer
D:\Documents\Fuse\Music Player Local with Native\build\Android\Preview\app\src\main\java\com\foreign\MusicPlayer.java:48: error: cannot find symbol
                mp.provideKeyResponse();
                  ^
  symbol:   method provideKeyResponse()
  location: variable mp of type MediaPlayer
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
4 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Incremental java compilation is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2340Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72340Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2340Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72340Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42340Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2340Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugJavaWithJavac
:app:compileDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
:app:compileDebugJavaWithJavac FAILED

BUILD FAILED

Total time: 10.667 secs
(unknown): E0200: Android build failed
?: Error E0200: Android build failed
(12.2 s)

Build completed in 21.30 seconds
    1 error

It’s quite obvious from the errors that you’re including a MediaPlayer that is below API level 26.

You should install/run Android Studio, and make sure that you have the right version for packages installed in SDK Manager.