0

我正在尝试在 VideoView 上使用 MediaPlayer 播放视频。不幸的是,我只播放了音频。没有视频显示。我需要使用 Mediaplayer,因为我的视频位于受保护的非世界可读的地方(并且在播放前要复制很大)并且需要流式传输。

(视频没问题,可以播放。在像 Acer A210 这样对非世界可读文件不太敏感的 android 平板电脑上,我可以使用 VideoViews setVideoURI 方法直接播放视频,我需要以下代码才能在三星平板电脑上播放视频)

有人可以告诉我我做错了什么吗?提前谢谢。

public class VideoPlayer extends Activity implements SurfaceHolder.Callback {
private VideoView objVideoView;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);       
    this.setContentView(R.layout.activity_videoplayer);
    objVideoView = (VideoView) findViewById(R.id.myVideoView); 

    String strVideoNames = "";

    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras != null) strVideoNames = extras.getString("strVideoNames");
    } else {
        strVideoNames = (String) savedInstanceState.getSerializable("strVideoNames");
    }

    playVideo(getVideoUrl(strVideoNames));
}

ArrayList<String> listVideoNames;

public void playVideo(String strVideoNames) {

    if (strVideoNames.contains(";")) {          
        String[] strAVideoUrls = strVideoNames.split(";");
        listVideoNames = new ArrayList<String>(Arrays.asList(strAVideoUrls));           
    } else {
        listVideoNames = new ArrayList<String>();
        listVideoNames.add(strVideoNames);
    }

    playVideoWithMediaPlayer();

}

MediaPlayer objMediaPlayer;

public void playVideoWithMediaPlayer() {

    if (listVideoNames.size() > 0) {        
        try {           
            SurfaceHolder objSurfaceHolder = objVideoView.getHolder();

            objSurfaceHolder.addCallback(this);
            objSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            objMediaPlayer = new MediaPlayer();
        } catch (Exception e) { 
            alert(e.getMessage()); 
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {

    try {       
        File fileVideo = new File(getVideoUrl(listVideoNames.get(0)));
        FileInputStream instreamVideo = new FileInputStream(fileVideo);
        objMediaPlayer.setDataSource(instreamVideo.getFD());

        objMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer _objMediaPlayer) {
                listVideoNames.remove(0);   
                if (listVideoNames.size() > 0) {
                    playVideoWithMediaPlayer(); 
                } else {
                    _objMediaPlayer.release();
                }
            }
        });

        objMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
            public void onPrepared(MediaPlayer _objMediaPlayer) {
                Log.d("MediaPlayer","Prepared ...");
                objMediaPlayer.start();
            }
        }); 

        objMediaPlayer.prepare();

    } catch (Exception e) { 
        alert(e.getMessage()); 
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
public void surfaceDestroyed(SurfaceHolder holder) { objMediaPlayer.stop(); }
}
4

2 回答 2

12

你有没有尝试使用:

objVideoView.setZOrderMediaOverlay(true);
objVideoView.videoView.setZOrderOnTop(true);

在 onCreate(...) 方法中?

于 2014-01-09T16:49:23.713 回答
0

Create a custom VideoPlayer by extending VideoView class and use it:

public class VideoPlayer extends VideoView {

    public VideoPlayer(Context context) {
        super(context);
        init();
    }

    @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            TyrooLog.i(TAG, "onMeasure");
            int width = getDefaultSize(videoWidth, widthMeasureSpec);
            int height = getDefaultSize(videoHeight, heightMeasureSpec);
            if (videoWidth > 0 && videoHeight > 0) {
                if (videoWidth * height > width * videoHeight) {
                    TyrooLog.i(TAG, "video too tall, correcting");
                    height = width * videoHeight / videoWidth;
                } else if (videoWidth * height < width * videoHeight) {
                    TyrooLog.i(TAG, "video too wide, correcting");
                    width = height * videoWidth / videoHeight;
                } else {
                    TyrooLog.i(TAG, "aspect ratio is correct: " + width+"/"+height+"="+mVideoWidth+"/"+mVideoHeight);
                }
            }
            TyrooLog.i(TAG, "setting size: " + width + 'x' + height);
            setMeasuredDimension(width, height);
        }
    }
}
于 2018-02-27T11:22:03.970 回答