仅使用ExoPlayer';s控制器,用于在Android中播放音频,无需黑色预览
我想在android中播放音频文件列表。您可能知道,当您有视频文件时,使用仅使用ExoPlayer';s控制器,用于在Android中播放音频,无需黑色预览,android,audio,exoplayer,Android,Audio,Exoplayer,我想在android中播放音频文件列表。您可能知道,当您有视频文件时,使用com.google.android.exoplayer2.ui.PlayerView是有意义的,因为您可以在屏幕上看到视频内容,而底部有播放/暂停等控制器。 但是对于音频文件,如果您允许播放音频文件,则会出现黑屏。 我需要的是只使用没有黑屏的控制器 有办法吗 第一次编辑: 在文档中,我遇到了PlaybackControlView和PlayerControlView。我能在我的情况下使用它们吗?如果是,使用哪一个 第二次编
com.google.android.exoplayer2.ui.PlayerView
是有意义的,因为您可以在屏幕上看到视频内容,而底部有播放/暂停等控制器。
但是对于音频文件,如果您允许播放音频文件,则会出现黑屏。
我需要的是只使用没有黑屏的控制器
有办法吗
第一次编辑:
在文档中,我遇到了PlaybackControlView
和PlayerControlView
。我能在我的情况下使用它们吗?如果是,使用哪一个
第二次编辑:这是我经过一天的研究后得出的结果。我在布局中使用了PlaybackControlView
,如下所示:
<com.google.android.exoplayer2.ui.PlaybackControlView
android:id="@+id/play_back_control_view"
android:layout_width="0dp" <--- I use ConstraintLayout, so this is okay
android:layout_height="wrap_content"
android:background="@color/black"
android:alpha="0.15"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:controller_layout_id="@layout/custom_playback_control_view" />
最后但并非最不重要的一点是,我将ExoPlayer
附加到PlaybackControlView
上,就像在我的片段中这样:
myAudioPlayer = ExoPlayerFactory.newSimpleInstance(context)
binding.playBackControlView.player = myAudioPlayer
尝试创建这样的自定义控制器,并将其命名为media_controller.xml。我在laout中使用了数据绑定
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#44000000"
android:focusableInTouchMode="false">
<RelativeLayout
android:id="@+id/controller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="@+id/play"
android:layout_width="64dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:padding="2dp"
android:src="@mipmap/ic_media_play" />
<ImageView
android:id="@+id/forward"
android:layout_width="64dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/play"
android:adjustViewBounds="true"
android:clickable="true"
android:focusable="true"
android:padding="6dp"
android:src="@mipmap/ic_media_forward" />
<ImageView
android:id="@+id/backward"
android:layout_width="64dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/play"
android:adjustViewBounds="true"
android:clickable="true"
android:focusable="true"
android:padding="6dp"
android:src="@mipmap/ic_media_backward" />
</RelativeLayout>
</FrameLayout>
</layout>
在活动布局中使用ExpoMediaController替换com.google.android.exoplayer2.ui.PlaybackControlView。然后调用ExpoMediaController的setPlayer方法。现在您可以完全控制控制器视图。您能再详细描述一下屏幕截图吗?您想实现什么?我只想使用ExoPlayer的控制器视图来播放音频文件。这就是全部。只有底部的控制栏。您需要实现自定义控制器并将播放器连接到自定义控制器。你能粘贴你当前的实现吗?@crack\u head:当然可以。我将我的进度作为第二次编辑添加到上面的问题部分。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#44000000"
android:focusableInTouchMode="false">
<RelativeLayout
android:id="@+id/controller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="@+id/play"
android:layout_width="64dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:padding="2dp"
android:src="@mipmap/ic_media_play" />
<ImageView
android:id="@+id/forward"
android:layout_width="64dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@id/play"
android:adjustViewBounds="true"
android:clickable="true"
android:focusable="true"
android:padding="6dp"
android:src="@mipmap/ic_media_forward" />
<ImageView
android:id="@+id/backward"
android:layout_width="64dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/play"
android:adjustViewBounds="true"
android:clickable="true"
android:focusable="true"
android:padding="6dp"
android:src="@mipmap/ic_media_backward" />
</RelativeLayout>
</FrameLayout>
</layout>
public class ExpoMediaController extends FrameLayout implements View.OnClickListener, Player.EventListener{
private LayoutInflater inflater;
Player simpleExoPlayer;
private MediaControllerBinding binding;//media_controller.xml
public ExpoMediaController(Context context, AttributeSet attrs) {
super(context, attrs);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
initView();
}
public ExpoMediaController(Context context) {
super(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
initView();
}
private void initView() {
binding = DataBindingUtil.inflate(inflater, R.layout.media_controller, this, true);
binding.play.setOnClickListener(this);
binding.forward.setOnClickListener(this);
binding.backward.setOnClickListener(this);
}
//Use this method to set and unset the player
public void setPlayer(@Nullable Player simpleExoPlayer) {
if (this.simpleExoPlayer == simpleExoPlayer) {
return;
}
Player oldPlayer = this.simpleExoPlayer;//get reference of old player which attached previously
if (oldPlayer != null) {//if old player not null then clear it
oldPlayer.removeListener(this);
}
this.simpleExoPlayer = simpleExoPlayer;
if (this.simpleExoPlayer != null) {
this.simpleExoPlayer.addListener(this);
}
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
switch (playbackState) {
case STATE_BUFFERING:
break;
case STATE_ENDED:
break;
case STATE_IDLE:
break;
case STATE_READY:
if (playWhenReady) {
binding.play.setImageResource(R.mipmap.ic_media_pause);
binding.play.setVisibility(VISIBLE);
} else {
binding.play.setImageResource(R.mipmap.ic_media_play);
binding.play.setVisibility(VISIBLE);
}
break;
default:
break;
}
}
@Override
public void onClick(View v) {
if (v == binding.play && simpleExoPlayer != null) {
if (simpleExoPlayer.isPlaying()) {
simpleExoPlayer.setPlayWhenReady(false);
showControls();
} else {
if (simpleExoPlayer.getPlaybackState() == STATE_ENDED) {
simpleExoPlayer.seekTo(0);
}
simpleExoPlayer.setPlayWhenReady(true);
}
}
}}