访问另一个类中实现mediaPlayer的Class的运行实例以获取audioSessionID

时间:2015-07-12 11:56:15

标签: java android audio equalizer

我有一个名为Player.java的类,它实现了MediaPlayer并创建了一个新的实例来管理音频播放

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类中获取它。我还在Player类中构建了一个函数来返回audioSessionId,但是在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类以使其正常工作?

0 个答案:

没有答案