2014-10-08 23 views
6

Używam MediaPlayer do odtwarzania wideo w TextureView. TextureView ma stały rozmiar, a rozmiary filmów są różne. Odtwarza wideo, ale problem polega na tym, że fragmenty wideo znajdują się na zewnątrz, a wideo nie jest przeskalowane, aby pasowało do TextureView.Dopasuj wideo do TextureView (jego części znajdują się na zewnątrz (niewidoczne)) za pomocą MediaPlayer

Czarne prostokąty + zdjęcie (wideo) to TextureView. Czerwony prostokąt jest rzeczywistym wideo, narysowałem go oczywiście, aby pokazać problem.

Pokazuje więc tylko część filmu i nie pokazuje pozostałej części.

Chcę, aby cały film był widoczny i zachował oryginalny współczynnik skali.

Dzięki.

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

pokaż, co zrobiłeś do tej pory – pskink

+0

pskink - dodano mały kod, nic specjalnego. –

+0

czy wypróbowałeś setTransform? – pskink

Odpowiedz

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

I wywołać tę funkcję w następujący sposób.

adjustAspectRatio(m_MediaPlayer.getVideoWidth(), m_MediaPlayer.getVideoHeight());