0

I have a class called Player.java which implements MediaPlayer and creates a new instance of it to manage audio playback

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

Now I have another class called Equalizer.java where I want to build an equalizer to control the audio playback. To initialize the equalizer, I need the audioSessionId of the running mediaPlayer instance but I can't seem to get it in the Equalizer class. I also built a function in the Player class to return the audioSessionId but calling it in the Equalizer class gives me a nullObjectReference error.

Here is the onCreate of the Equalizer class

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

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

    setupEqualizerFxAndUI();
}

Also, if I try to create an instance of the Player class, the null object error is gone but the equalizer doesn't work as the instance is a new one and not the one that is actually running.

How do I get the AudioSessionId of the running instance of the mediaPlayer in Player.java to pass to the Equalizer class to make it work?

Neel
  • 111
  • 2
  • 8

0 Answers0