2013-06-22 40 views
0

我已經看到幾個帖子有同樣的問題,但我仍然無法解決我的問題。我使用surfaceview,fragment和mediaplayer播放mp4視頻。我得到音頻,但沒有視頻。該視頻不會在應用程序之外播放。我嘗試過不同的格式,但沒有運氣。我沒有使用模擬器。我正在使用三星Galaxy Tab2進行測試。我缺少什麼顯示視頻?使用surfaceview和片段來顯示視頻。視頻不顯示,但我有聲音,

這裏是我的XML文件:

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

<SurfaceView 
    android:id="@+id/video_surfaceView" 
    android:layout_weight="1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
> 
</SurfaceView> 

<Button 
    android:id="@+id/playvideoplayer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="- PLAY "/> 

<Button 
    android:id="@+id/pausevideoplayer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="- PAUSE"/> 

<Button 
    android:id="@+id/stopvideoplayer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="- STOP"/> 


</LinearLayout> 

這裏是我的代碼:

public class ChapterVideoFragment extends Fragment { 

private static final String TAG = "ChapterVideoFragment"; 

private static final boolean VERBOSE = true; 

private SurfaceView mSurfaceView; 
private SurfaceHolder mSurfaceHolder; 
private MediaPlayer mp = null; 

private Button mPlayVideo, 
         mPauseVideo, 
         mStopVideo; 

private boolean mPausing = false; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
     Bundle savedInstanceState) { 
    if (VERBOSE) Log.v(TAG, "+++ onCreateView +++"); 
    View v = inflater.inflate(R.layout.fragment_video, parent, false);  

    mSurfaceView = (SurfaceView)v.findViewById(R.id.video_surfaceView);  
    mPlayVideo = (Button)v.findViewById(R.id.playvideoplayer); 
    mPauseVideo = (Button)v.findViewById(R.id.pausevideoplayer); 
    mStopVideo = (Button)v.findViewById(R.id.stopvideoplayer); 

    mp = MediaPlayer.create(getActivity(), R.raw.improvisation);   
    mPlayVideo.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
       mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      //Get the dimensions of the video 
       int videoWidth = mp.getVideoWidth(); 
      int videoHeight = mp.getVideoHeight(); 

       //Get the width of the screen 
     int screenWidth = 
    getActivity().getWindowManager().getDefaultDisplay().getWidth();     

      //Get the SurfaceView layout parameters 
      android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams(); 

      //Set the width of the SurfaceView to the width of the screen 
      lp.width = screenWidth; 

      //Set the height of the SurfaceView to match the aspect ratio of the video 
      //be sure to cast these as floats otherwise the calculation will likely be  
       0 
      lp.height = (int) (((float)videoHeight/(float)videoWidth) * 
       (float)screenWidth); 

      //Commit the layout parameters 
      mSurfaceView.setLayoutParams(lp); 

      //mp.setDisplay(mSurfaceView.getHolder());    
      mp.start(); 

     } 
    });   

這裏是SurfaceView代碼:

  mSurfaceHolder = mSurfaceView.getHolder(); 
    mSurfaceHolder.addCallback(new SurfaceHolder.Callback() { 

     @Override 
     public void surfaceDestroyed(SurfaceHolder arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void surfaceCreated(SurfaceHolder arg0) { 
      if (VERBOSE) Log.v(TAG, "+++ surfaceCreated +++");    
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 
      // TODO Auto-generated method stub 

     } 
    });  
+0

嘗試使用videoview - 有可能您使用的設備顯示視頻,不支持mediaplayer對象中的格式,我使用videoview解決了此問題 –

+0

謝謝,我會盡力而爲。 – sowens

+0

@LenaBru VideoView工作! – sowens

回答

0

嘗試使用videoview - 這是可能的您用來顯示視頻的設備不支持mediaplayer對象中的格式,我解決了這個問題with videoview