2014-10-08 5 views
6

का उपयोग कर एक टेक्स्टचर व्यू में एक वीडियो फिट करें (इसके भाग बाहर हैं (दिखाई नहीं दे रहे हैं) मैं TextureView में वीडियो चलाने के लिए मीडियाप्लेयर का उपयोग कर रहा हूं। TextureView का एक निश्चित आकार है, और वीडियो के आकार अलग हैं। यह वीडियो चलाता है, लेकिन समस्या यह है कि वीडियो के कुछ हिस्सों बाहर हैं और वीडियो को TextureView फिट करने के लिए नीचे स्केल नहीं किया गया है।MediaPlayer

काले आयताकार + फोटो (वीडियो) TextureView हैं। लाल आयत वास्तविक वीडियो है, मैंने समस्या को दिखाने के लिए इसे आकर्षित किया।

तो यह केवल वीडियो का एक हिस्सा दिखाता है और बाकी को दिखाता नहीं है।

मैं चाहता हूं कि सभी वीडियो दृश्यमान हों और इसे मूल स्केल-अनुपात रखें।

धन्यवाद।

enter image description here

// The class implements SurfaceTextureListener 

// Set TextureView to MediaPlayer 
m_TextureView.setSurfaceTextureListener(this); 

@Override 
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { 
    m_MediaPlayer = new MediaPlayer(); 
    m_MediaPlayer.setSurface(new Surface(surfaceTexture)); 
} 

// Play video 
m_MediaPlayer.reset(); 
m_MediaPlayer.setDataSource(videoFilePath); 
m_MediaPlayer.prepare(); 
m_MediaPlayer.start(); 
+0

दिखाएं कि आपने अभी तक क्या किया है – pskink

+0

पस्किंक - थोड़ा कोड जोड़ा, इसके बारे में कुछ खास नहीं। –

+0

क्या आपने सेट ट्रांसफॉर्म को आजमाया है? – pskink

उत्तर

5
private void adjustAspectRatio(int videoWidth, int videoHeight) { 
     int viewWidth = m_TextureView.getWidth(); 
     int viewHeight = m_TextureView.getHeight(); 
     double aspectRatio = (double) videoHeight/videoWidth; 

     int newWidth, newHeight; 
     if (viewHeight > (int) (viewWidth * aspectRatio)) { 
      // limited by narrow width; restrict height 
      newWidth = viewWidth; 
      newHeight = (int) (viewWidth * aspectRatio); 
     } else { 
      // limited by short height; restrict width 
      newWidth = (int) (viewHeight/aspectRatio); 
      newHeight = viewHeight; 
     } 
     int xoff = (viewWidth - newWidth)/2; 
     int yoff = (viewHeight - newHeight)/2; 
     Log.v(TAG, "video=" + videoWidth + "x" + videoHeight + 
       " view=" + viewWidth + "x" + viewHeight + 
       " newView=" + newWidth + "x" + newHeight + 
       " off=" + xoff + "," + yoff); 

     Matrix txform = new Matrix(); 
     m_TextureView.getTransform(txform); 
     txform.setScale((float) newWidth/viewWidth, (float) newHeight/viewHeight); 
     //txform.postRotate(10);   // just for fun 
     txform.postTranslate(xoff, yoff); 
     m_TextureView.setTransform(txform); 
    } 

और इस प्रकार इस समारोह कहते हैं।

adjustAspectRatio(m_MediaPlayer.getVideoWidth(), m_MediaPlayer.getVideoHeight()); 
संबंधित मुद्दे