androidunity-game-engineandroid-unity-plugin

Making a Unity Android Native Media Player plugin


I'm Making a native android mediaplayer plugin for unity,everything works fine,the plugin is loading correctly and the parameters are passing correctly to the plugin,even the player starts,but it seems that nothing is playing , no sounds come out.the source code is:

package com.bluedreamcatcher.nativemediaplayerplugin;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.util.Log;


import java.io.IOException;

public class NativeMediaPlayer {

public MediaPlayer mediaPlayer;
public void StreamMedia(String URL) {
    Log.d("Unity",URL);

     mediaPlayer = new MediaPlayer();

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    try {

        mediaPlayer.setDataSource(URL);

        mediaPlayer.prepareAsync();

        try {

            mediaPlayer.start();

        }catch (Exception ex){
            Log.d("Unity",ex.getMessage());
        }

        if(mediaPlayer.isPlaying()){
            Log.d("Unity","playing");
        }
    } catch (Exception eX) {
        String error;
        error = eX.toString();
        Log.d("Unity",error);
    }
}
        }

Solution

  • Alright,after some modifications I changed my codes and now it's working as a basic unity android native mediaplayer plugin.here's the code :

    package com.bluedreamcatcher.nativemediaplayerplugin;
    
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.util.Log;
    
    public class NativeMediaPlayer {
    public MediaPlayer mediaPlayer;
    public void StreamMedia(String URL) {
        Log.d("Unity",URL);
         mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(URL);
            mediaPlayer.prepareAsync();
    
        } catch (Exception eX) {
            String error;
            error = eX.toString();
            Log.d("Unity",error);
    
        }
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
    
    }
    
    }