I want to mute volume of YouTube Android Player API I have used YouTubePlayerFragment and while i put custom ImageView on it it shows me error `
YouTubeAndroidPlayerAPI
: YouTube video playback stopped due to unauthorized overlay on top of player.` So how i can put button to mute audio Is there any one who can help me?
youTubePlayerSupportFragment.initialize(API_KEY, new com.google.android.youtube.player.YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(final com.google.android.youtube.player.YouTubePlayer.Provider provider, com.google.android.youtube.player.YouTubePlayer youTubePlayer, boolean b) {
mTubePlayer = youTubePlayer;
mTubePlayer.setPlayerStyle(com.google.android.youtube.player.YouTubePlayer.PlayerStyle.MINIMAL);
mTubePlayer.setManageAudioFocus(true);
if (ytInit.size() - 1 > linearLayoutManager.findFirstVisibleItemPosition())
mTubePlayer.cueVideo(strings.get(linearLayoutManager.findFirstVisibleItemPosition()), ytInit.get(linearLayoutManager.findFirstVisibleItemPosition()));
else
mTubePlayer.cueVideo(strings.get(linearLayoutManager.findFirstVisibleItemPosition()));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (mTubePlayer != null) {
showEpisodeThumbnail.setVisibility(View.GONE);
prg.setVisibility(View.GONE);
mTubePlayer.play();
frm.setVisibility(View.VISIBLE);
}
}
}, 1000);
}
#Override
public void onInitializationFailure(com.google.android.youtube.player.YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
I got the answer youtube doesn't provide any facility to mute audio in API I have used custom iframe player
Related
I understand that there are other similar questions asked. But they either have no solutions or the solutions provided are not working.
The player works fine in fullscreen mode but as a part of the layout contents, it keeps pausing and rebuffering.
Here is my code:
public class PlayerActivity extends YouTubeBaseActivity implements MediaPlayer.OnPreparedListener, YouTubePlayer.OnInitializedListener {
...
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.player_youtube);
Button yt_player_button = (Button) findViewById(R.id.player_yt_play);
yt_player_button.setText("Click to play a Youtube video");
yt_player_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
youTubePlayerView.initialize(getString(R.string.youtube_api_key), PlayerActivity.this);
}
});
}
...
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (youTubePlayer == null)
return;
if (!b)
youTubePlayer.cueVideo("63kmMcHBQlA");
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
YoutubePlayerView cannot be nested inside a ScrollView for whatever the reason.
It just doesn't work. I suggest that you create a fragment which pops up during the request for the Youtube video or creating a new intent with just the YoutubePlayerView (maybe fullscreen).
I couldn't find a reason as to why this is the case.
(Please do add it as an answer if you know why)
Using exo-player is a very good alternative too (If you can get the DASH Url's in a dynamic app. Then best of luck)
I developed an app using YouTube DATA API. I play single video but i want to use the next button to next the video. I use YouTube player to play video & i pass the video id to the video player.
Now How can i next the video?
here is my code what i am using to play single video
public class PlayerViewDemoActivity extends YouTubeFailureRecoveryActivity {
private String vedio_id;
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
requestWindowFeature(1);
setContentView(R.layout.playerview_demo);
this.vedio_id = getIntent().getStringExtra("videoId");
((YouTubePlayerView) findViewById(R.id.youtube_view)).initialize("AIzaSyCojCp66RLS9OY8hOwnW0UWLNdC56z24Os", this);
}
public void onInitializationSuccess(YouTubePlayer.Provider paramProvider, YouTubePlayer paramYouTubePlayer, boolean paramBoolean) {
if (!paramBoolean) {
paramYouTubePlayer.loadVideo(this.vedio_id);
paramYouTubePlayer.play();
}
}
}
if you only pass the video id to the video player you have to pass the new video id to play new.
paramYouTubePlayer.loadVideo(idVideo);
In your example you have to create a new attribute private YoutubePlayer paramYoutubePlayer;.
Next, you can create a new method like this:
public void nextVideo (String idVideo)
{
paramYouTubePlayer.loadVideo(idVideo);
paramYouTubePlayer.play();
}
But you can load an array of videos with:
loadVideos(List<String> videoIds)
and once you have loaded the List, you can call the next() and previous() methods on the youtubePlayer object.
Hope it helps!
I'm creating app for tablet screen so my video list and youtube player both are in same activity. When first time app is start every thing work great.
But when i click back button and then start again app so if I am not manage state when activity come bake in to foreground at that time fragment is not showing it's just show white screen and when I click in any video from list it's throw NullPointException.
If I manage state than it's throw IllegalStateException.
#Override
protected void onResume() {
super.onResume();
if(Singleton.getInstance().getVideoUrl().length()!=0) {
VideoUrl = utils.getYouTubeVideoId(Singleton.getInstance().getKidsDataGetterSetter().get(Singleton.getInstance().getPosition()).getVideo_url());
YPlayer = Singleton.getInstance().getYPlayer();
youTubePlayerFragment = (YouTubePlayerSupportFragment) this.getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.getView().setVisibility(View.INVISIBLE);
youTubePlayerFragment.initialize(KidsConstantClass.API_KEY, new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
YPlayer = youTubePlayer;
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
});
YPlayer.loadVideo(VideoUrl); <=(Line 313)
}
}
If any one know please help me!
Thank you
I have a problem in running a video in Samsung S3(Android 4.1.1), the issue seems to be because the videoview is on a fragment because if I put it on and activity, it works.
Also I found out that if I turn on the GPU hardware acceleration on, the video works.
I have also a game made by drawing on a SurfaceView and that view doesn't work as well(only with GPU on)... The rest of the app content is displayed as it supposed to (buttons and other layouts).
I tested the app on Nexus S and on the emulator and it works fine, also on other devices..
Does anyone know what the problem could be?
Thank you!
And here is the code:
public class VideoFragment extends Fragment implements MediaPlayer.OnCompletionListener,
MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {
private Video mVideo;
private VideoView mVideoView;
// The video position
private int mPosition;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.screen_video, container, false);
mVideoView = (VideoView) fragmentView.findViewById(R.id.VideoView);
return fragmentView;
}
#Override
public void onPause() {
super.onPause();
// Pause the video if it is playing
if (mVideoView.isPlaying()) {
mVideoView.pause();
}
// Save the current video position
mPosition = mVideoView.getCurrentPosition();
}
#Override
public void onResume() {
super.onResume();
mVideoView.setOnCompletionListener(this);
mVideoView.setOnPreparedListener(this);
mVideoView.setOnErrorListener(this);
mVideoView.setKeepScreenOn(true);
// Initialize the media controller
MediaController mediaController = new MediaController(getActivity());
mediaController.setMediaPlayer(mVideoView);
// Set-up the video view
mVideoView.setMediaController(mediaController);
mVideoView.requestFocus();
mVideoView.setVideoPath(mVideo.getUrl());
if (mVideoView != null) {
// Restore the video position
mVideoView.seekTo(mPosition);
mVideoView.requestFocus();
}
}
#Override
public void onDestroy() {
super.onDestroy();
// Clean-up
if (mVideoView != null) {
mVideoView.stopPlayback();
mVideoView = null;
}
}
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
Log.e("VIDEO PLAY", "end video play");
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// Start the video view
mediaPlayer.start();
Log.e("VIDEO PLAY", "video ready for playback");
}
#Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
Log.e("VIDEO PLAY", "error: " + i);
return true;
}
}
I don't think it's something related to context(Application or Activity).. because on all other devices the Video and the games are displayed..
Thanks for the help!
I have a similar problem, and i solved it changing:
MediaController mediaController = new MediaController(getActivity().getApplicationContext());
to this (In a fragmet):
MediaController mediaController = new MediaController(getActivity());
Hope this helps,...
EDIT
Look at this class
http://code.google.com/p/sinaweibo-honeycomb/source/browse/branches/sinaweibo-merged/src/com/lenovo/dll/SinaWeibo/VideoFragment.java?r=71
If hardware acceleration fixes your issue then I would enable it for that view/window on that device.
In general I've found that when code works on one device but not another it is typically caused by one of the following problems:
Bug in the manufacturer's API implementation
Different interpretation of the API by the manufacturer
Concurrency problem (e.g. race condition, improper synchronization) in your own code that happens to trigger more frequently on a particular device
As far as I can tell you seem to be using the UI thread appropriately so I would imagine your issue falls into one of the first two categories and you'll just need to work around it.
Move MediaController mediaController = new MediaController(getActivity()) from onResume() to onCreateView.
Hi I am creating an app which will play livestream.com's rtsp live channel.
I am launching the player using intent within my app as following:
iPlayer = new Intent(Intent.ACTION_VIEW);
//iPlayer.setType("video/*");
iPlayer.setData(Uri.parse(videoUrl));
startActivity(iPlayer);
When the media player is launched through my Application, the video performance is very poor. It stops for buffering every few seconds, plays for few seconds and pauses for buffering again.
ON the other hand, If I open the url in android browser (eg. http://m.livestream.com/abcalbania) it has a video tag on that page and triggers video player. THIS time, the video runs very smooth.
Any Idea why this might happen? And how this can be fixed?
I do not want to launch browser URL as intent.
This is done on Atmel cortex A9 chipset with Android 2.3.4
The problem is caused by the codecs that probably are not supported by your player.
for example i have a video created with MPEG Audio codec along with the H.264 video codec.
if i launch the video through my Application the video runs smoothly, but if i launch a video in Ooyala Hook Player it has a very poor performance, it plays the video every 3 seconds, the reason is that the stream use MPEG audio codec instead of AAC Audio codec that is supported.
You will find the answer with:
what codecs are used to create de video, and what are supported by
your player?
Use this code for smooth STREAM
String videoUrl = "rtmp://mystream";
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse(videoUrl), "video/*");
startActivity(i);
Why not you play this in your own activity, create activity and render the video view like
private String path2 = "rtsp://...";
Uri video = Uri.parse(path2);
mVideoView.setVideoURI(video);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.postInvalidateDelayed(100);
mVideoView.start();
Also you can buffer before start playing maybe 5 secs and than successive buffering will be fast. you can control more thing by your own.
Android's MediaPlayer handle very well RTSP - don't open an external app - it's not necessary and product-wise wrong.
About your question - the browser might send another parameters to the video player that help the video play smooth, I didn't check it but it sounds like the only possible option for what you're describing. Example for the extra param might be the video resolution / encoding / size .. you can get all of them easily using MediaMetaDataRetriever.
If you don't want to use the native VideoView or MediaPlayer you can always add external player to your
app, like libVLC or Vitamio.
I recommend of using Vitamio, is really easy to use and integrate. LibVLC is in native code, it means you'll have to build it using ndk and add its libs to your project.
You can find here how to do that.
Android video view support RTSP urls well no need to pass intent to other application.Try out with this code, pass xml with declaration of video view and find it inside this activity.
public class VideoPlayer extends Activity
{
private VideoView mVideoView;
String videoURL="";
static Utility utility;
static Context context;
//MediaController mediaController;
//int iCurrentpostion=0;
int counter=0;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_video_player);
setupViews();
}
private void setupViews()
{
context=VideoPlayer.this;
//utility=new Utility(VideoPlayer.this);
showProgressDialog("Please wait", "Loading video..");
//videoURL=getIntent().getExtras().getString("url");
mVideoView=(VideoView)findViewById(R.id.xvdvwTab);
// mediaController=new MediaController(context);
// mVideoView.setMediaController(mediaController);
mVideoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
utility.hideProgressDialog();
mVideoView.start();
mVideoView.requestFocus();
}
});
mVideoView.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
finish();
}
});
mVideoView.setOnErrorListener(new OnErrorListener()
{
#Override
public boolean onError(MediaPlayer mp, int what, int extra)
{
utility.hideProgressDialog();
return false;
}
});
playVideoFile();
}
private void playVideoFile()
{
try
{
mVideoView.setVideoURI(Uri.parse("your url"));
}
catch (Exception e)
{
utility.hideProgressDialog();
if (mVideoView != null)
{
mVideoView.stopPlayback();
}
}
}
#Override
protected void onResume()
{
/*if(mVideoView!=null)
{
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mVideoView.requestFocus();
if(iCurrentpostion!=0)
mVideoView.seekTo(iCurrentpostion);
mVideoView.start();
}
super.onResume();*/
}
#Override
protected void onDestroy()
{
try
{
if (mVideoView != null)
{
mVideoView.stopPlayback();
mVideoView=null;
}
super.onDestroy();
} catch (Exception e)
{}
}
public void showProgressDialog(String title,String Message)
{
hideProgressDialog();
progressDialog=new ProgressDialog(mActivity);
progressDialog.setTitle(title);
progressDialog.setMessage(Message);
if(Constant.isActivityisRunning)
progressDialog.show();
}
public void hideProgressDialog()
{
if (progressDialog != null)
{
if (progressDialog.isShowing())
{
progressDialog.dismiss();
progressDialog = null;
}
}
}
}
I think play video by Asynchronously for better performance. My code is:
private class myAsync extends AsyncTask<Void, Integer, Void> {
int duration = 0;
//int current = 0;
#Override
protected Void doInBackground(Void... params) {
videoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.seekTo(check);
videoView.start();
duration = videoView.getDuration();
}
});
do {
current = videoView.getCurrentPosition();
System.out.println("duration - " + duration + " current- "
+ current);
}
if (sync.isCancelled())
break;
} while (current != duration || current == 0);
return null;
}
}