如何在SurfaceView中旋转视频* .mp4?

时间:2013-02-13 16:29:00

标签: android media-player surfaceview

友。你能帮帮我吗?

我需要在点击按钮或其他按钮上旋转视频(视频必须始终播放)。 我在SurfaceView中的MediaPlayer中显示视频并试试这个:

@Override
public void surfaceCreated(SurfaceHolder holder) {
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
        surfaceHolder.lockCanvas().rotate(90);
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDisplay(surfaceHolder);

        try {
            mediaPlayer.setDataSource(stringPath);
            mediaPlayer.prepare();

        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        mediaPlayer.start();
    }
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
                           int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}

但视频不想播放。 (我不知道它是否旋转) 帮帮我...任何人.. 非常感谢。

1 个答案:

答案 0 :(得分:-1)

也许你可以试试这个:

 surfaceView = (SurfaceView)findViewById(R.id.surfaceview);     
 DisplayMetrics displaymetrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
 int h = displaymetrics.heightPixels;
 int w = displaymetrics.widthPixels;

 surfaceHolder = surfaceView.getHolder();

 if (w > h) { 
     surfaceHolder.setFixedSize(w,h);
 } else {
     surfaceHolder.setFixedSize(h,w);
 }

 surfaceHolder.addCallback(this);     
 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
 mediaPlayer.setDisplay(surfaceHolder);

另外,也许您可​​能希望将上面的代码放在onCreate()上,而不是SurfaceCreated()。然后放一些从某个按钮调用的play()方法:

protected void play() {
// TODO Auto-generated method stub

      if(mediaPlayer.isPlaying()){
       mediaPlayer.reset();
      }
    //Define here your filename:
    String stringPath = Environment.getExternalStorageDirectory().getPath() + "/BigBuckBunny_320x180.mp4";

      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mediaPlayer.setDisplay(surfaceHolder);

      try {
       mediaPlayer.setDataSource(stringPath); 
       mediaPlayer.prepare();

      } catch (IllegalArgumentException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IllegalStateException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

      mediaPlayer.start();
      }