【问题标题】:Android MediaPlayer (using VideoView) no Video, only Audio playsAndroid MediaPlayer(使用 VideoView)没有视频,只有音频播放
【发布时间】:2018-02-27 19:22:03
【问题描述】:

我正在尝试在 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(); }
}

【问题讨论】:

    标签: android audio android-mediaplayer android-videoview


    【解决方案1】:

    你试过用吗:

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

    在 onCreate(...) 方法中?

    【讨论】:

    • 我不知道为什么没有人赞成这个答案。效果很好。
    【解决方案2】:

    通过扩展 VideoView 类创建自定义 VideoPlayer 并使用它:

    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);
            }
        }
    }
    

    【讨论】:

    • 您是否注意到您回答了一个近 5 年的问题并且已经得到充分回答?
    • 是的,但我发布这个答案是为了帮助某人:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多