4

朋友们。你可以帮帮我吗?

我需要在按钮单击或其他时旋转视频(视频必须始终播放)。我在 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) {
}

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

4

1 回答 1

-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();
      }
于 2014-04-25T19:27:00.593 回答