0

MediaPlayer を実装し、その新しいインスタンスを作成してオーディオ再生を管理する Player.java というクラスがあります。

public class Player implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, AudioManager.OnAudioFocusChangeListener {

public static final String UPDATE_BROADCAST = "marverenic.jockey.player.REFRESH"; // Sent to refresh views that use up-to-date player information
public static final String UPDATE_EXTRA = "extra"; // An extra which acts as a snapshot of the current player status when an UPDATE broadcast is sent
private static final String TAG = "Player";
private static final String QUEUE_FILE = ".queue";

public static final String PREFERENCE_SHUFFLE = "prefShuffle";
public static final String PREFERENCE_REPEAT = "prefRepeat";

// Instance variables
public ManagedMediaPlayer mediaPlayer;
private Context context;
private MediaSession mediaSession;
private RemoteControlClient remoteControlClient;
private SystemListener headphoneListener;

// Queue information
private ArrayList<Song> queue;
private ArrayList<Song> queueShuffled = new ArrayList<>();
private int queuePosition;
private int queuePositionShuffled;
public int mediaSessionId;

// MediaFocus variables
private boolean active = false; // If we currently have audio focus
private boolean shouldResumeOnFocusGained = false; // If we should play music when focus is returned

// Shufle & Repeat options
private boolean shuffle; // Shuffle status
public static final short REPEAT_NONE = 0;
public static final short REPEAT_ALL = 1;
public static final short REPEAT_ONE = 2;
private short repeat; // Repeat status

private Bitmap art; // The art for the current song

/**
 * Create a new Player Object, which manages a {@link MediaPlayer}
 * @param context A {@link Context} that will be held for the lifetime of the Player
 */
public Player(Context context) {
    this.context = context;

    // Initialize the media player
    mediaPlayer = new ManagedMediaPlayer();
    //mediaPlayer.getAudioSessionId();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnCompletionListener(this);
   // mediaSessionId = mediaPlayer.getAudioSessionId();

    // Initialize the queue
    queue = new ArrayList<>();
    queuePosition = 0;

    // Load preferences
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    shuffle = prefs.getBoolean(PREFERENCE_SHUFFLE, false);
    repeat = (short) prefs.getInt(PREFERENCE_REPEAT, REPEAT_NONE);

    // Initialize the relevant media controller
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        initMediaSession();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        initRemoteController();
    }

    // Attach a SystemListener to respond to headphone events
    headphoneListener = new SystemListener(this);
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MEDIA_BUTTON);
    filter.addAction(Intent.ACTION_HEADSET_PLUG);
    context.registerReceiver(headphoneListener, filter);
}

ここで、オーディオの再生を制御するイコライザーを作成する Equalizer.java という別のクラスがあります。イコライザーを初期化するには、実行中の mediaPlayer インスタンスの audioSessionId が必要ですが、Equalizer クラスで取得できないようです。また、AudioSessionId を返す関数を Player クラスに作成しましたが、Equalizer クラスで呼び出すと nullObjectReference エラーが発生します。

これが Equalizer クラスの onCreate です

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_equalizer);

    mEqualizer = new Equalizer(0, player.AudioSessionID());
    mEqualizer.setEnabled(true);

    setupEqualizerFxAndUI();
}

また、Player クラスのインスタンスを作成しようとすると、null オブジェクト エラーはなくなりますが、インスタンスは新しいインスタンスであり、実際に実行されているインスタンスではないため、イコライザーは機能しません。

Player.java の mediaPlayer の実行中のインスタンスの AudioSessionId を取得して Equalizer クラスに渡して動作させるにはどうすればよいですか?

4

0 に答える 0