我一直在尝试将视频播放器部署到我的应用上,到目前为止我通过YouTube视频完成了这项工作。但从他所做的,他的视频播放完美。无论出于何种原因,我无法让我的视频播放器启动流。我尝试过不同的格式,但似乎都没有。
代码如下:
package com.example.user.tabfinal;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.VideoView;
public class videoActivity extends AppCompatActivity implements
View.OnClickListener {
ProgressDialog pd ;
VideoView videoView;
ImageButton btnPlay;
String URL = "http://31.210.87.4/film/fullmp4best/Assassin_s_Creed_Official_Trailer_2_2016.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
videoView = (VideoView)findViewById(R.id.videoView);
btnPlay = (ImageButton)findViewById(R.id.btnPlay);
}
@Override
public void onClick(View v) {
pd = new ProgressDialog(mainActivity2.this);
pd.setMessage("Please wait...");
pd.setCanceledOnTouchOutside(false);
pd.show();
try{
if(!videoView.isPlaying()){
Uri uri = Uri.parse(URL);
videoView.setVideoURI(uri);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
btnPlay.setImageResource(R.drawable.ic_play);
}
});
}else{
videoView.pause();
btnPlay.setImageResource(R.drawable.ic_play);
}
}
catch (Exception Ex){
}
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
pd.dismiss();
mp.setLooping(true);
videoView.start();
btnPlay.setImageResource(R.drawable.ic_pause);
}
});
}
}