VideoView切换标签时不显示视频

发布于 2024-10-07 07:18:01 字数 6646 浏览 0 评论 0原文

我有 2 个选项卡,每个选项卡都包含 videoView。但是,当我切换到第二个选项卡时,视频播放,音频正常工作,但视频不播放,视图只是黑色。这在 Android 2.1 + 中工作正常,但在 1.6 中则不然 这是我的代码:

TabActivity:

package com.example.tab;


import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class main extends TabActivity {
    /** Called when the activity is first created. */

 private TabHost tabHost;

 public void onCreate( Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 tabHost = getTabHost();

    Intent intent1 = new Intent(this, Video1.class);
   Intent intent2 = new Intent(this, Video2.class);


     tabHost.addTab(tabHost.newTabSpec("Tab1")
        .setIndicator("Video1")
        .setContent(intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)));

     tabHost.addTab(tabHost.newTabSpec("Tab2")
    .setIndicator("Video2")
    .setContent(intent2.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)));

 }
}

Video1 Activity:

/**
 * 
 */
package com.example.tab;

import android.app.Activity;

import android.content.Intent;
import android.content.res.Configuration;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.View;

import android.widget.MediaController;
import android.widget.VideoView;

/**
 * @author lyubomir.todorov
 * 
 */
public class Video1 extends Activity implements SurfaceHolder.Callback {

 private String path = "http://xxx.xxx";

 private VideoView mVideoView;

 private SurfaceHolder holder;
 private static final int INSERT_ID = Menu.FIRST;
 private static final String TAG = "Tab";

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.videoview1);

  mVideoView = (VideoView) findViewById(R.id.surface_view1);
//  holder = mVideoView.getHolder();
//  holder.addCallback(this);
//  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

 }

      public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0, INSERT_ID, 0, "FullScreen");

  return true;
 }

 public boolean onOptionsItemSelected(MenuItem item) {
  Log.d(TAG, "Player re-started after device configuration change");

  return true;
 }

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
 }

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();

  PlayVideo();

  Log.d(TAG, "onResume Video1");

 }

 protected void onPause() {
  super.onDestroy();
  Log.d(TAG, "onPause Video1");
  if (mVideoView != null) {
   mVideoView.stopPlayback();
   mVideoView.setVisibility(View.GONE);
   mVideoView=null;
   Log.d(TAG, "onPause1 Video1");
  }

 }



 public void surfaceDestroyed(SurfaceHolder surfaceholder) {
  holder.isCreating();
  Log.d(TAG, "video1 surfaceDestroyed called");
 }

 public void surfaceCreated(SurfaceHolder holder) {
  Log.d(TAG, "Current 1 surfaceCreated called");
  Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
  // playVideo(extras.getInt(MEDIA));

 }

 public void PlayVideo() {

  mVideoView.setVideoPath(path);
  mVideoView.start();
  mVideoView.setMediaController(new MediaController(this));
  mVideoView.setVisibility(View.VISIBLE);


 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
  Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
  // TODO Auto-generated method stub
  Log.d(TAG, "Current video surfaceChanged called");

 }

}

videoview1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">


  <VideoView android:id="@+id/surface_view1"
   android:layout_width="320dip" android:layout_height="240dip"
    /> 
</LinearLayout>

Video2 Activity:

/**
 * 
 */
package com.example.tab;

import android.app.Activity;

import android.os.Bundle;
import android.util.Log;

import android.view.SurfaceHolder;
import android.view.View;

import android.widget.MediaController;
import android.widget.VideoView;



/**
 * @author lyubomir.todorov
 * 
 */
public class Video2 extends Activity implements SurfaceHolder.Callback {


 private String path = "http://xxx.xxx";

 private VideoView mVideoView;

 private SurfaceHolder holder;

 private static final String TAG = "Tab";



 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.videoview2);

  mVideoView = (VideoView) findViewById(R.id.surface_view2);
//  holder= mVideoView.getHolder();
//  holder.addCallback(this);
//  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);





 }




 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();


      PlayVideo();




  Log.d(TAG, "onResume Video1");

 }

 protected void onPause() {
  super.onPause();
  Log.d(TAG, "onPause Video2");
  if (mVideoView!=null){
   mVideoView.stopPlayback();
   mVideoView.setVisibility(View.GONE);
   Log.d(TAG, "onPause1 Video2");
  }


 }

 public void surfaceDestroyed(SurfaceHolder surfaceholder) {
  holder.isCreating();
  Log.d(TAG, "video1 surfaceDestroyed called");
 }

 public void surfaceCreated(SurfaceHolder holder) {
  Log.d(TAG, "Current 1 surfaceCreated called");
  Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());



 }



 public void PlayVideo() {

       mVideoView.setVisibility(View.VISIBLE);


       mVideoView.bringToFront();
       mVideoView.setVideoPath(path);

       mVideoView.start();
       mVideoView.setMediaController(new MediaController(this));



}


 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
  Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
  // TODO Auto-generated method stub
  Log.d(TAG, "Current video surfaceChanged called");

 }

}

videoview2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">

  <VideoView android:id="@+id/surface_view2"
   android:layout_width="320dip" android:layout_height="240dip"
    /> 

</LinearLayout>

I have 2 tabs each containing videoView. But when I switch to the second tab the video plays the audio is working, but the video doesn't plays and the view is only black. This works fine in Android 2.1 + , but it does't in 1.6 Here is my code:

TabActivity:

package com.example.tab;


import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class main extends TabActivity {
    /** Called when the activity is first created. */

 private TabHost tabHost;

 public void onCreate( Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 tabHost = getTabHost();

    Intent intent1 = new Intent(this, Video1.class);
   Intent intent2 = new Intent(this, Video2.class);


     tabHost.addTab(tabHost.newTabSpec("Tab1")
        .setIndicator("Video1")
        .setContent(intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)));

     tabHost.addTab(tabHost.newTabSpec("Tab2")
    .setIndicator("Video2")
    .setContent(intent2.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)));

 }
}

Video1 activity:

/**
 * 
 */
package com.example.tab;

import android.app.Activity;

import android.content.Intent;
import android.content.res.Configuration;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.View;

import android.widget.MediaController;
import android.widget.VideoView;

/**
 * @author lyubomir.todorov
 * 
 */
public class Video1 extends Activity implements SurfaceHolder.Callback {

 private String path = "http://xxx.xxx";

 private VideoView mVideoView;

 private SurfaceHolder holder;
 private static final int INSERT_ID = Menu.FIRST;
 private static final String TAG = "Tab";

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.videoview1);

  mVideoView = (VideoView) findViewById(R.id.surface_view1);
//  holder = mVideoView.getHolder();
//  holder.addCallback(this);
//  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

 }

      public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0, INSERT_ID, 0, "FullScreen");

  return true;
 }

 public boolean onOptionsItemSelected(MenuItem item) {
  Log.d(TAG, "Player re-started after device configuration change");

  return true;
 }

 @Override
 public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
 }

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();

  PlayVideo();

  Log.d(TAG, "onResume Video1");

 }

 protected void onPause() {
  super.onDestroy();
  Log.d(TAG, "onPause Video1");
  if (mVideoView != null) {
   mVideoView.stopPlayback();
   mVideoView.setVisibility(View.GONE);
   mVideoView=null;
   Log.d(TAG, "onPause1 Video1");
  }

 }



 public void surfaceDestroyed(SurfaceHolder surfaceholder) {
  holder.isCreating();
  Log.d(TAG, "video1 surfaceDestroyed called");
 }

 public void surfaceCreated(SurfaceHolder holder) {
  Log.d(TAG, "Current 1 surfaceCreated called");
  Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
  // playVideo(extras.getInt(MEDIA));

 }

 public void PlayVideo() {

  mVideoView.setVideoPath(path);
  mVideoView.start();
  mVideoView.setMediaController(new MediaController(this));
  mVideoView.setVisibility(View.VISIBLE);


 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
  Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
  // TODO Auto-generated method stub
  Log.d(TAG, "Current video surfaceChanged called");

 }

}

videoview1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">


  <VideoView android:id="@+id/surface_view1"
   android:layout_width="320dip" android:layout_height="240dip"
    /> 
</LinearLayout>

Video2 activity:

/**
 * 
 */
package com.example.tab;

import android.app.Activity;

import android.os.Bundle;
import android.util.Log;

import android.view.SurfaceHolder;
import android.view.View;

import android.widget.MediaController;
import android.widget.VideoView;



/**
 * @author lyubomir.todorov
 * 
 */
public class Video2 extends Activity implements SurfaceHolder.Callback {


 private String path = "http://xxx.xxx";

 private VideoView mVideoView;

 private SurfaceHolder holder;

 private static final String TAG = "Tab";



 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.videoview2);

  mVideoView = (VideoView) findViewById(R.id.surface_view2);
//  holder= mVideoView.getHolder();
//  holder.addCallback(this);
//  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);





 }




 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();


      PlayVideo();




  Log.d(TAG, "onResume Video1");

 }

 protected void onPause() {
  super.onPause();
  Log.d(TAG, "onPause Video2");
  if (mVideoView!=null){
   mVideoView.stopPlayback();
   mVideoView.setVisibility(View.GONE);
   Log.d(TAG, "onPause1 Video2");
  }


 }

 public void surfaceDestroyed(SurfaceHolder surfaceholder) {
  holder.isCreating();
  Log.d(TAG, "video1 surfaceDestroyed called");
 }

 public void surfaceCreated(SurfaceHolder holder) {
  Log.d(TAG, "Current 1 surfaceCreated called");
  Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());



 }



 public void PlayVideo() {

       mVideoView.setVisibility(View.VISIBLE);


       mVideoView.bringToFront();
       mVideoView.setVideoPath(path);

       mVideoView.start();
       mVideoView.setMediaController(new MediaController(this));



}


 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
  Log.d(TAG, Boolean.valueOf(holder.isCreating()).toString());
  // TODO Auto-generated method stub
  Log.d(TAG, "Current video surfaceChanged called");

 }

}

videoview2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">

  <VideoView android:id="@+id/surface_view2"
   android:layout_width="320dip" android:layout_height="240dip"
    /> 

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文