0

我正在为统一制作一个原生 android 媒体播放器插件,一切正常,插件加载正确,参数正确传递给插件,即使播放器启动,但似乎什么都没有播放,没有声音出来。源代码是:

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);
    }
}
        }
4

1 回答 1

0

好吧,经过一些修改,我改变了我的代码,现在它作为一个基本的统一 android 原生媒体播放器插件工作。这是代码:

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();
        }
    });

}

}
于 2020-01-04T13:49:18.307 回答