VideoView in eclipse not playing on phone

前端 未结 2 1677
慢半拍i
慢半拍i 2021-01-24 10:40

I put a videoview in eclipse for an android app but the video will not play on my phone.

package test.test;

import android.app.Activity;
import android.net.Uri         


        
2条回答
  •  难免孤独
    2021-01-24 11:14

    I think problem lies either with connectivity (http) or VideoView usage.

    To know if the connectivity is the issue, you can try playing media content local to the phone, i.e. from SD Card.

    Issues also be coming from VideoView usage

    VideoView class uses SurfaceView and MediaPlayer to achieve Video Playback. MediaPlayer has APIs to set url, prepare the media pipeline, start the pipeline etc. But before pipeline can be started; the pipeline has to be ready i.e. in preroll condition. To notify application about this, MediaPlayer provides listeners. In this case it is onPrepareListener. VideoView which interacts with MediaPlayer also (has to ?) provides these listeners as well.

    Have a look below code for VideoPlayer activity which uses VideoView for playback. (Verified for local content only) This activity takes absolute path to file to be played from intent. (passed from list activity)

    import android.app.Activity;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.MediaController;
    import android.widget.VideoView;
    
    
    public class VideoPlayer extends Activity implements OnCompletionListener, OnPreparedListener {
        private static VideoView vView;
        private String filePath;
        public static long clipDurationMS; 
        private View        mLoadingIndicator; 
    
        public void onCreate(Bundle savedInstanceState)     
        {
            super.onCreate(savedInstanceState);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.main);
    
            vView = (VideoView)findViewById(R.id.VideoView01);
            mLoadingIndicator = findViewById(R.id.progress_indicator); 
    
            vView.setBackgroundColor(0x0000); 
    
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();
            filePath = (String)extras.get("URL");
    
            vView.setVideoPath(filePath);  
    
            MediaController mc;
            mc = new MediaController(this);
            vView.setMediaController(mc);
    
            vView.requestFocus();
            vView.setOnCompletionListener(this);
            vView.setOnPreparedListener(this);
        }
    
        public void onCompletion(MediaPlayer arg0) 
        {
            finish();   
        }
    
        public void onPrepared(MediaPlayer arg0) 
        {
            mLoadingIndicator.setVisibility(View.GONE);
            ViewGroup.LayoutParams params;
    
            params        = vView.getLayoutParams();
    
            params.height = arg0.getVideoHeight();
            params.width  = arg0.getVideoWidth();
    
            vView.setLayoutParams(params);
    
            vView.start(); 
        }
    
        public void onStop(){
            super.onStop();
            vView.stopPlayback();
            finish();
        }
    }
    

    Shash

提交回复
热议问题