我有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活动:
/**
*
*/
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活动:
/**
*
*/
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>