我试图在我的项目中使用Exoplayer(而不是Android的MediaPlayer)。但是,我有一个问题:获取当前音频会话的ID。使用传统的MediaPlayer,通过调用getAudioSessionId很容易,但Exoplayer没有任何相应的方法。有人帮我吗?
答案 0 :(得分:3)
从ExoPlayer2开始,MediaCodecAudioTrackRenderer
和PlayerControl
不能再用于获取AudioSessionId。相反,您应该在exoPlayer实例上设置AudioRendererEventListener
。
exoPlayer.setAudioDebugListener(new AudioRendererEventListener(){
@Override
public void onAudioEnabled(DecoderCounters counters) {
}
@Override
public void onAudioSessionId(int audioSessionId) {
//TODO Do something with your AudioSessionID here
}
@Override
public void onAudioDecoderInitialized(String decoderName, long initializedTimestampMs, long initializationDurationMs) {
}
@Override
public void onAudioInputFormatChanged(Format format) {
}
@Override
public void onAudioTrackUnderrun(int bufferSize, long bufferSizeMs, long elapsedSinceLastFeedMs) {
}
@Override
public void onAudioDisabled(DecoderCounters counters) {
}
});
答案 1 :(得分:1)
import com.google.android.exoplayer.util.PlayerControl;
通过传递Exoplayer实例来实例化PlayerControl对象。并调用getAudioSessionId。
PlayerControl playerControl=new PlayerControl(Exoplayer.instance);
int audioSessionId=playerControl.getAudioSessionId();
我认为这就是你想要的。
答案 2 :(得分:1)
以下是我用于获取音频会话ID的代码段 -
FrameworkSampleSource sampleSource = new FrameworkSampleSource(getApplicationContext(), builtUri, null);
MediaCodecAudioTrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource, MediaCodecSelector.DEFAULT){
@Override
protected void onAudioSessionId(int audioSessionId) {
// do whatever u want to do with id.
}
};
exoPlayer.prepare(audioRenderer);
exoPlayer.setPlayWhenReady(true);
答案 3 :(得分:0)
Player.AudioComponent audioComponent = exoPlayer.getAudioComponent();
int audioSessionid=audioComponent.getAudioSessionId();