我已经创建了一个工作正常的Android应用程序,但问题是我想播放YouTube视频静音,因为我们将在后台播放歌曲。那么可以在我的应用中播放静音的YouTube视频吗? 并隐藏所有YouTube播放器控件和视频是自动播放。
答案 0 :(得分:1)
您可以使用这个奇妙的android-youtube-player库。基本上,此库不允许您设置音量,因此您必须创建自己的布局(并自行管理音量),从而使youtube播放器的用户界面膨胀。 这是我创建的自定义UI。
遵循Quick start guide后,您就可以创建自定义UI。
layout_custom_youtube_player.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="@+id/viewYoutubeCustomUI"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:background="@android:color/black" />
<TextView
android:id="@+id/textViewYoutubeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:visibility="gone"
android:textSize="20sp"
android:textColor="@android:color/white"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" />
<ImageButton
android:id="@+id/imageButtonMuteUnMute"
android:layout_width="@dimen/_30sdp"
android:layout_height="@dimen/_30sdp"
android:paddingTop="@dimen/_5sdp"
android:paddingRight="@dimen/_5sdp"
android:scaleType="fitCenter"
android:tint="@color/colorPrimary"
app:srcCompat="@drawable/ic_volume_off_white_24dp"
android:background="@color/transparent"
android:layout_alignParentRight="true" />
<ProgressBar
android:id="@+id/progressbarYoutube"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerVertical="true"
android:layout_centerInParent="true" />
<ImageButton
android:id="@+id/imageButtonPlayPause"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_70sdp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_play_circle_outline_white_24dp"
android:background="@color/transparent"
android:tint="@color/colorPrimary"
android:layout_centerVertical="true"
android:layout_centerInParent="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:weightSum="3">
<com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.views.YouTubePlayerSeekBar
android:id="@+id/youTubePlayerSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
app:fontSize="@dimen/_12sdp"
app:color="@color/colorPrimary" />
<ImageButton
android:id="@+id/imageButtonEnterExitFullscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent"
app:srcCompat="@drawable/ic_enter_fullscreen_white_24dp"
android:layout_gravity="center"
android:tint="@color/colorPrimary"
android:paddingLeft="@dimen/_5sdp"
android:paddingRight="@dimen/_5sdp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
CustomYoutubePlayerUIController.java
package com.mcsoft.custom_youtube_handler;
import android.content.Context;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import com.mcsoft.R;
import com.mcsoft.helper.SharedPreferencesHandler;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.PlayerConstants;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.YouTubePlayer;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.AbstractYouTubePlayerListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.listeners.YouTubePlayerFullScreenListener;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.utils.YouTubePlayerTracker;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.views.YouTubePlayerSeekBar;
import com.pierfrancescosoffritti.androidyoutubeplayer.core.ui.views.YouTubePlayerSeekBarListener;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
public class CustomYoutubePlayerUIController extends AbstractYouTubePlayerListener implements YouTubePlayerFullScreenListener
{
private final View playerUi;
public static final int DELAY_TIME_SHOW_PLAY_PAUSE_BUTTON = 3000;
private Context context;
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
// viewYoutubeCustomUI is used to intercept clicks on the WebView, I don't want the user to be able to click the WebView directly.
private View viewYoutubeCustomUI;
private View progressbarYoutube;
private YouTubePlayerSeekBar youTubePlayerSeekBar;
private final YouTubePlayerTracker playerTracker;
private boolean fullscreen = false;
private boolean isMuted = true;
private SharedPreferencesHandler sharedPreferencesHandler;
public CustomYoutubePlayerUIController (Context context, View customPlayerUi, YouTubePlayer youTubePlayer, YouTubePlayerView youTubePlayerView) {
this.playerUi = customPlayerUi;
this.context = context;
this.youTubePlayer = youTubePlayer;
this.youTubePlayerView = youTubePlayerView;
sharedPreferencesHandler = new SharedPreferencesHandler(context);
playerTracker = new YouTubePlayerTracker();
this.youTubePlayer.addListener(playerTracker);
initViews(customPlayerUi);
}
private void initViews(View playerUi)
{
youTubePlayerSeekBar = playerUi.findViewById(R.id.youTubePlayerSeekBar);
youTubePlayer.addListener(youTubePlayerSeekBar);
viewYoutubeCustomUI = playerUi.findViewById(R.id.viewYoutubeCustomUI);
progressbarYoutube = playerUi.findViewById(R.id.progressbarYoutube);
final ImageButton imageButtonPlayPause = playerUi.findViewById(R.id.imageButtonPlayPause);
ImageButton imageButtonEnterExitFullscreen = playerUi.findViewById(R.id.imageButtonEnterExitFullscreen);
final ImageButton imageButtonMuteUnMute = playerUi.findViewById(R.id.imageButtonMuteUnMute);
TextView textViewYoutubeTitle = playerUi.findViewById(R.id.textViewYoutubeTitle);
textViewYoutubeTitle.setVisibility(View.GONE);
viewYoutubeCustomUI.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
imageButtonPlayPause.setVisibility(View.VISIBLE);
return true;
}
if(event.getAction() == MotionEvent.ACTION_UP)
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// hide your button here
imageButtonPlayPause.setVisibility(View.INVISIBLE);
}
}, DELAY_TIME_SHOW_PLAY_PAUSE_BUTTON);
return true;
}
return false;
}
});
// Manage volume
imageButtonMuteUnMute.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (!isMuted)
{
youTubePlayer.setVolume(0);
imageButtonMuteUnMute.setImageDrawable(context.getDrawable(R.drawable.ic_volume_off_white_24dp));
}
else
{
youTubePlayer.setVolume(100);
imageButtonMuteUnMute.setImageDrawable(context.getDrawable(R.drawable.ic_volume_up_white_24dp));
}
isMuted = !isMuted;
}
});
youTubePlayerSeekBar.setYoutubePlayerSeekBarListener(new YouTubePlayerSeekBarListener() {
@Override
public void seekTo(float time) {
youTubePlayer.seekTo(time);
}
});
imageButtonPlayPause.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if (isMuted)
{
youTubePlayer.setVolume(0);
}
else
{
youTubePlayer.setVolume(100);
}
if (playerTracker.getState() == PlayerConstants.PlayerState.PLAYING)
{
youTubePlayer.pause();
imageButtonPlayPause.setImageDrawable(context.getDrawable(R.drawable.ic_play_circle_outline_white_24dp));
}
else
{
youTubePlayer.play();
imageButtonPlayPause.setImageDrawable(context.getDrawable(R.drawable.ic_pause_circle_outline_white_24dp));
}
}
});
imageButtonEnterExitFullscreen.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if (fullscreen)
{
youTubePlayerView.exitFullScreen();
}
else
{
youTubePlayerView.enterFullScreen();
}
fullscreen = !fullscreen;
}
});
}
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
progressbarYoutube.setVisibility(View.GONE);
}
@Override
public void onStateChange(@NonNull YouTubePlayer youTubePlayer, @NonNull PlayerConstants.PlayerState state) {
if(state == PlayerConstants.PlayerState.PLAYING || state == PlayerConstants.PlayerState.PAUSED || state == PlayerConstants.PlayerState.VIDEO_CUED)
viewYoutubeCustomUI.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
else
if(state == PlayerConstants.PlayerState.BUFFERING)
viewYoutubeCustomUI.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
}
@Override
public void onYouTubePlayerEnterFullScreen() {
ViewGroup.LayoutParams viewParams = playerUi.getLayoutParams();
viewParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
viewParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
playerUi.setLayoutParams(viewParams);
}
@Override
public void onYouTubePlayerExitFullScreen() {
ViewGroup.LayoutParams viewParams = playerUi.getLayoutParams();
viewParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
viewParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
playerUi.setLayoutParams(viewParams);
}
}
现在,您必须在活动中添加YouTube布局
View customPlayerUi = youTubePlayerView.inflateCustomPlayerUi(R.layout.layout_custom_youtube_player);
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer)
{
CustomYoutubePlayerUIController customPlayerUiController = new CustomYoutubePlayerUIController(context, holder.customPlayerUi, youTubePlayer, youTubePlayerView);
youTubePlayer.addListener(customPlayerUiController);
youTubePlayerView.addFullScreenListener(customPlayerUiController);
if(sharedPreferencesHandler.getAutoplayYoutubeVideo())
{
youTubePlayer.loadVideo(videoId, 0);
}
else
{
youTubePlayer.cueVideo(videoId, 0);
}
}
});
注意
如果您想从链接中提取YouTube的视频ID,可以在this link进行检查。
重要
要使用android-youtube-player,您需要迁移dependencies
(跟随this guide),因为您需要将YouTubePlayerView
添加为LifecycleObserver
答案 1 :(得分:0)
请看一下:
https://github.com/TheFinestArtist/YouTubePlayerActivity
正如您所看到的,您有一个功能: 媒体量支持
观看YouTube播放器时,用户应该可以设置媒体音量