如何防止嵌入式YouTube视频在WebView,Android的背景上播放

How to Prevent embedded YouTube Video from playing on Background on WebView, Android

本文关键字:Android 背景 播放 WebView 嵌入式 何防止 YouTube 视频      更新时间:2023-09-26

我搜索了类似的主题,关于如何防止嵌入式YouTube视频在android上的后台播放。然而,我就是不能让它发挥作用。下面的片段是我尝试的方法,我从这里得到了输入链接描述这里

我尝试的第一个解决方案是

@Override
public void onResume()
{
    super.onResume();
    webView.onResume();
}
@Override
public void onPause()
{
    super.onPause();
    webView.onPause();
}

我尝试的第二个解决方案是

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(keyCode==event.KEYCODE_BACK)
    {
        mWebview.loadUrl("");
        mWebview.stopLoading();
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

然而,他们两个都不起作用。它是编译的,但由于我是编码的新手,我不知道我是否在正确的地方应用了代码。

任何帮助都将不胜感激。

这是我未修改的java脚本,在修改上面提到的代码之前。

public class VideoDetailActivity extends ActionBarActivity {
	
	private FavDbAdapter mDbHelper;
    private Toolbar mToolbar;
	String date;
	String id;
	String title;
	String description;
	String favorite;
	String image;
	
	ImageLoader imageLoader;
	private TextView mPresentation;
	int mImageHeight;
	int latestAlpha;
	ImageView mImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.activity_youtube_detail);
	  
	  mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
      setSupportActionBar(mToolbar);
      getSupportActionBar().setDisplayShowHomeEnabled(true);
	  //getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.argb(128, 0, 0, 0)));
	  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	  mToolbar.getBackground().setAlpha(0);
	  getSupportActionBar().setDisplayShowTitleEnabled(false);
		
	  mPresentation = (TextView)findViewById(R.id.youtubetitle);
	  TextView detailsDescription = (TextView)findViewById(R.id.youtubedescription);
	  TextView detailsPubdate = (TextView)findViewById(R.id.youtubeurl);
	  
	  imageLoader = Helper.initializeImageLoader(VideoDetailActivity.this);
	  Bundle bundle = this.getIntent().getExtras();
    
	  mPresentation.setText(bundle.getString("keyTitle"));
      	detailsDescription.setText(bundle.getString("keyDescription"));
      	detailsPubdate.setText(bundle.getString("keyDate"));
      		title = (bundle.getString("keyTitle"));
      		id = (bundle.getString("keyId"));
      		date = (bundle.getString("keyDate"));
      		description = (bundle.getString("keyDescription"));
      		favorite = (bundle.getString("keyFavorites"));
      		image = (bundle.getString("keyImage"));
      		
      if ((getResources().getString(R.string.ad_visibility).equals("0"))){
	        	// Look up the AdView as a resource and load a request.
	        	AdView adView = (AdView) findViewById(R.id.adView);
	        	AdRequest adRequest = new AdRequest.Builder().build();
	        	adView.loadAd(adRequest);
	  }
      
       mImage = (ImageView) findViewById(R.id.image);
       imageLoader.displayImage(image, mImage);
	   mImageHeight = mImage.getLayoutParams().height;
		((TrackingScrollView) findViewById(R.id.scroller)).setOnScrollChangedListener(
				new TrackingScrollView.OnScrollChangedListener() {
					@Override
					public void onScrollChanged(TrackingScrollView source, int l, int t, int oldl, int oldt) {
						handleScroll(source, t);
					}
				}
		);
      
      ImageButton btnPlay= (ImageButton) findViewById(R.id.playbutton);
      btnPlay.bringToFront();
      //Listening to button event
      btnPlay.setOnClickListener(new View.OnClickListener() {
    	  public void onClick(View arg0) {
    		  //Intent intent = new Intent(Intent.ACTION_VIEW);
    		  //intent.setData(Uri.parse("http://sherdle.com/id/"+ video.getId()));
    		  //startActivity(intent);  
    		  Intent intent = new Intent(VideoDetailActivity.this, YouTubePlayerActivity.class);
    		  intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_ID, id);
    		  intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    		  startActivity(intent);
    	  }
     });
      
      Button btnOpen = (Button) findViewById(R.id.openbutton);
      //Listening to button event
      btnOpen.setOnClickListener(new View.OnClickListener() {
    	  public void onClick(View arg0) {
    		       try {  
    		         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
    	             startActivity(intent);                 
    	           }catch (ActivityNotFoundException ex){
    	             Intent intent=new Intent(Intent.ACTION_VIEW, 
    	             Uri.parse("http://www.youtube.com/watch?v="+id));
    	             startActivity(intent);
    	         }
    	  }
     });
      
      Button btnFav = (Button) findViewById(R.id.favoritebutton);
      
    //Listening to button event
      btnFav.setOnClickListener(new View.OnClickListener() {
    	  public void onClick(View arg0) {
    		  mDbHelper = new FavDbAdapter(VideoDetailActivity.this);
              mDbHelper.open();
              if(mDbHelper.checkEvent(title, description, date, id, "", "", "youtube")) {
                  // Item is new
           	   mDbHelper.addFavorite(title, description, date, id, "", "", "youtube");
           	    Toast toast = Toast.makeText(VideoDetailActivity.this, getResources().getString(R.string.favorite_success), Toast.LENGTH_LONG);
                  toast.show();
              } else {
                  Toast toast = Toast.makeText(VideoDetailActivity.this, getResources().getString(R.string.favorite_duplicate), Toast.LENGTH_LONG);
                  toast.show();
              }
    	  }
     });
   }
    
    private void handleScroll(TrackingScrollView source, int top) {
		int scrolledImageHeight = Math.min(mImageHeight, Math.max(0, top));
		ViewGroup.MarginLayoutParams imageParams = (ViewGroup.MarginLayoutParams) mImage.getLayoutParams();
		int newImageHeight = mImageHeight - scrolledImageHeight;
		if (imageParams.height != newImageHeight) {
			// Transfer image height to margin top
			imageParams.height = newImageHeight;
			imageParams.topMargin = scrolledImageHeight;
			// Invalidate view
			mImage.setLayoutParams(imageParams);
		}
		
		final int imageheaderHeight = mImage.getHeight() - getSupportActionBar().getHeight();
    	//t=how far you scrolled
    	//ratio is from 0,0.1,0.2,...1
    	final float ratio = (float) Math.min(Math.max(top, 0), imageheaderHeight) / imageheaderHeight;
    	//setting the new alpha value from 0-255 or transparent to opaque
    	final int newAlpha = (int) (ratio * 255);
    	
    	if (newAlpha != latestAlpha){
    		mToolbar.getBackground().setAlpha(newAlpha);
    	}
    	
    	latestAlpha = newAlpha;
	}
	
	@Override
	public void onPause(){
		super.onPause();
		mToolbar.getBackground().setAlpha(255);
	}
	
	@Override
	public void onResume(){
		super.onPause();
		mToolbar.getBackground().setAlpha(latestAlpha);
	}
    
   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
	 	case android.R.id.home:
            finish();
            return true;
        case R.id.share:
        	String applicationName = getResources().getString(R.string.app_name);
  		    Intent sendIntent = new Intent();
  		    sendIntent.setAction(Intent.ACTION_SEND);
  		    
  		    String urlvalue = getResources().getString(R.string.video_share_begin);
  		    String seenvalue = getResources().getString(R.string.video_share_middle);
  		    String appvalue = getResources().getString(R.string.video_share_end);
  		                                           //this is the text that will be shared
  		    sendIntent.putExtra(Intent.EXTRA_TEXT, (urlvalue+"http://youtube.com/watch?"+id+seenvalue+applicationName+appvalue));
  		    sendIntent.putExtra(Intent.EXTRA_SUBJECT, title); //you can replace title with a string of your choice
  		    sendIntent.setType("text/plain");
  		    startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.share_header)));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
 	
 	@Override
 	public boolean onCreateOptionsMenu(Menu menu) {
 	    MenuInflater inflater = getMenuInflater();
 	    inflater.inflate(R.menu.youtube_detail_menu, menu);
 	    return true;
 	}
 	
}

您是否尝试加载伪URL而不是空URL?

mWebview.loadUrl("data://dummy");

这对我很有效;)