2014-10-08 5 views
6

Я использую MediaPlayer для воспроизведения видео в 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

pskink - добавлен небольшой код, ничего особенного. –

+0

Вы попробовали setTransform? – 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()); 

 Смежные вопросы

  • Нет связанных вопросов^_^