ExoPlayer V2
참고 :
https://codelabs.developers.google.com/codelabs/exoplayer-intro/#7
ExoPlayer Media Track 가져오기
재생되고 있는 영상의 비디오 트랙, 오디오 트랙 exoplayer 에서 가져오기
ExoPlayerManager.java
public class ExoPlayerManager {
private Activity mActivity;
private SimpleExoPlayer mPlayer;
private PlayerView mPlayerView;
private boolean isShouldAutoPlay = true;
private DefaultTrackSelector mTrackSelector;
private DataSource.Factory mDataSourceFactory;
private BandwidthMeter mBandwidthMeter;
private Player.EventListener mEventListener = null;
private OnTracksListener mTracksListener = null;
public ExoPlayerManager(Activity activity) {
this.mActivity = activity;
this.mBandwidthMeter = new DefaultBandwidthMeter();
this.mDataSourceFactory = new DefaultDataSourceFactory(mActivity,
Util.getUserAgent(mActivity, APPLICATION_NAME),
(TransferListener<? super DataSource>) mBandwidthMeter
);
}
public void setPlayerView(PlayerView playerView) {
this.mPlayerView = playerView;
}
public void initializePlayer(String url, int start_position) {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
mTrackSelector = null;
}
if (mPlayerView == null) {
Toast.makeText(mActivity, "No Player view", Toast.LENGTH_SHORT).show();
return;
}
mPlayerView.requestFocus();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(mBandwidthMeter);
mTrackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
mPlayer = ExoPlayerFactory.newSimpleInstance(mActivity, mTrackSelector);
mPlayerView.setPlayer(mPlayer);
prepare(url);
if (start_position > 0) {
mPlayer.seekTo(start_position);
}
mPlayer.setPlayWhenReady(isShouldAutoPlay);
}
public void prepare(String url) {
DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(
Uri.parse(url),
mDataSourceFactory, extractorsFactory, null, null
);
if (mEventListener != null) {
mPlayer.addListener(mEventListener);
}
mPlayer.prepare(mediaSource);
}
public void setEventListener(Player.EventListener mEventListener) {
this.mEventListener = mEventListener;
}
public void setShouldAutoPlay(boolean shouldAutoPlay) {
this.isShouldAutoPlay = shouldAutoPlay;
}
public void setPlayWhenReady(boolean isReady) {
mPlayer.setPlayWhenReady(isReady);
}
public void pausePlayer(){
mPlayer.setPlayWhenReady(false);
mPlayer.getPlaybackState();
}
public void startPlayer(){
mPlayer.setPlayWhenReady(true);
mPlayer.getPlaybackState();
}
public boolean getPlayWhenReady() {
return mPlayer.getPlayWhenReady();
}
public void stop() {
if (mPlayer != null) {
mPlayer.stop();
mPlayerView.setPlayer(null);
}
}
public SimpleExoPlayer getPlayer() {
return mPlayer;
}
public void releasePlayer() {
if (mPlayer != null) {
isShouldAutoPlay = mPlayer.getPlayWhenReady();
mPlayer.release();
mPlayer = null;
mTrackSelector = null;
}
}
public void setTracksListener(OnTracksListener mTracksListener) {
this.mTracksListener = mTracksListener;
}
/* '오디오 트랙 가져오는 부분' 참조 */
/* '트랙 선택 부분' 참조 */
private MediaSource buildMediaSource(Uri uri) {
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mActivity,
Util.getUserAgent(mActivity, APPLICATION_NAME), bandwidthMeter);
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new HlsMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
// Prepare the player with the source.
return videoSource;
}
/* */
public interface OnTracksListener {
void onTracks(String log);
void onAudioTracks(ArrayList<String> tracks);
}
}
오디오 트랙 가져오는 부분
public void getTracks() {
MappingTrackSelector.MappedTrackInfo mappedTrackInfo = mTrackSelector.getCurrentMappedTrackInfo();
ArrayList<String> audioTracks = new ArrayList<>();
StringBuilder sb = new StringBuilder();
String logInfos = "[onTracksChanged] "+mappedTrackInfo;
Log.i(MainActivity.TAG, logInfos);
sb.append(logInfos+"\n\n");
if (mappedTrackInfo != null) {
Log.i(MainActivity.TAG, "mappedTrackInfo.length : "+mappedTrackInfo.length);
for(int i=0; i<mappedTrackInfo.length; i++) {
TrackGroupArray array = mappedTrackInfo.getTrackGroups(i);
String logArray = "[TrackGroupArray : "+i+"] array.length : "+array.length;
Log.i(MainActivity.TAG, logArray);
sb.append(logArray+"\n");
for (int j=0; j<array.length; j++) {
TrackGroup group = array.get(j);
// String logGroup = "[TrackGroup : "+j+"] TrackGroup : "+group.getFormat(j);
String logGroup = "[TrackGroup : "+j+"] TrackGroup : "+group.length;
Log.i(MainActivity.TAG, logGroup);
sb.append(logGroup+"\n");
for (int z=0; z<group.length; z++) {
Format format = group.getFormat(z);
// String logFormat = "[logFormat : "+j+"] getFormat : "+format.toString()
// +"\n language : "+format.language
// +"\n codecs : "+format.codecs
// +"\n containerMimeType : "+format.containerMimeType
// +"\n sampleMimeType : "+format.sampleMimeType
// ;
// Log.i(MainActivity.TAG, logFormat);
// sb.append(logFormat+"\n");
// 오디오 트랙만 저장한다
if (format.sampleMimeType.startsWith("audio/") ) {
audioTracks.add(format.language);
}
}
}
}
if (mappedTrackInfo.getTrackTypeRendererSupport(C.TRACK_TYPE_VIDEO)
== MappingTrackSelector.MappedTrackInfo.RENDERER_SUPPORT_UNSUPPORTED_TRACKS) {
Toast.makeText(mActivity, "Unsupported", Toast.LENGTH_SHORT).show();
}
if (mappedTrackInfo.getTrackTypeRendererSupport(C.TRACK_TYPE_AUDIO)
== MappingTrackSelector.MappedTrackInfo.RENDERER_SUPPORT_UNSUPPORTED_TRACKS) {
Toast.makeText(mActivity, "Unsupported", Toast.LENGTH_SHORT).show();
}
}
if (mTracksListener != null) {
mTracksListener.onTracks(sb.toString());
mTracksListener.onAudioTracks(audioTracks);
}
}
트랙 선택 부분
public int selectTrack(MediaExtractor extractor, boolean audio) {
int tracks = extractor.getTrackCount();
for (int i=0; i<tracks; i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (audio) {
if (mime.startsWith("audio/")) {
return i;
}
} else {
if (mime.startsWith("video/")) {
return i;
}
}
}
return -1;
}
public void selectedTrack(String language) {
if (mPlayer != null) {
DefaultTrackSelector.ParametersBuilder builder
= new DefaultTrackSelector.ParametersBuilder();
builder = builder.setPreferredAudioLanguage(language);
mTrackSelector.setParameters(builder.build());
}
}
'Study > Android' 카테고리의 다른 글
[Android] Zoom Layout (0) | 2019.06.14 |
---|---|
[Android] Too many open file 에러 해결 (0) | 2019.06.14 |
[Android] Scale Animation (0) | 2019.06.14 |
[Android] wifi 원격 디버깅 (0) | 2019.06.14 |
[Android] GestureDetector Top/Bottom/Up/Down (0) | 2019.06.14 |