在SurfaceView

时间:2015-09-11 23:07:42

标签: android android-mediaplayer surfaceview

我正在尝试让我的应用运行视频,但它不起作用,我认为这是因为表面属性。我不知道我应该在surfacechanged括号中放什么,我所知道的是我希望它的参数覆盖整个屏幕,除了我的按钮。我不确定要为surfaceDestroyed添加什么,我只是希望我的视频每次结束时重新启动。此外,设置数据源我会考虑是否输入正确的代码,因为它与我在互联网上找到的示例有很大不同。

public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {  
 }

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(surfaceHolder);
        mediaPlayer.setDataSource("com.vid.vid_n_vid.vid");
        mediaPlayer.prepare();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
            }
        });


    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {}

1 个答案:

答案 0 :(得分:0)

试试这个,

 @Override
      public void surfaceCreated(SurfaceHolder holder) {
      // TODO Auto-generated method stub
            //set the surface holder to use in displaying the video
           mPlayer.setDisplay(surfaceHolder);
           try {
            //set data source to the video file
                mPlayer.setDataSource(this, Uri.parse("android.resource://com.example.andtip/"+R.raw.pixarforbirds));
                //prepare the MediaPlayer
                mPlayer.prepare();
                //start playing the video
                mPlayer.start();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException 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();
            }

      }