2010-12-10 29 views
2

我有2個標籤,每個標籤都包含videoView。但是,當我切換到第二個選項卡時,視頻播放音頻正在工作,但視頻不播放,視圖僅爲黑色。這是Android 2.1 +做工精細,但它在1.6簡化版,這裏是我的代碼:VideoView切換標籤時不顯示視頻

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))); 

} 
} 

視頻1活動:

/** 
* 
*/ 
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> 

回答