我正在Android中开发一个示例媒体播放器应用程序来播放在线视频,我已经开发了一些代码来播放视频。它很好地播放SD卡的视频,但我在这方面面临两个问题。
1-当我打开在线视频的应用程序时,它会显示视频,但它不播放视频,过了一会儿它会显示一条警告对话框,上面写着“抱歉,此视频无法播放”。
2-我有“进度条”的代码,但我不知道从互联网下载视频时将代码放在何处。
这是我可以检查的代码。提前谢谢。
myVideoView = (VideoView) findViewById(R.id.surface_view);
try
{
myVideoView.setVideoURI(Uri.parse("http://www.MY_DOMAIN_NAME.com/videos/video1.mp4"));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "No Media found", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:14)
试试这个
VideoView v;
MediaController mediaController;
ProgressDialog progressDialog;
然后
public void playvideo(String videopath) {
Log.e("entered", "playvide");
Log.e("path is", "" + videopath);
try {
progressDialog = ProgressDialog.show(VideoPlay.this, "",
"Buffering video...", false);
progressDialog.setCancelable(true);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
mediaController = new MediaController(VideoPlay.this);
Uri video = Uri.parse(videopath);
v.setMediaController(mediaController);
v.setVideoURI(video);
v.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
v.start();
}
});
} catch (Exception e) {
progressDialog.dismiss();
System.out.println("Video Play Error :" + e.getMessage());
}
}
如果仍有问题“此视频无法播放” 尝试改变视频格式,希望它能帮到你。
答案 1 :(得分:2)
在Android中使用视频视图播放实时视频非常容易。我在这里粘贴代码,试试这个代码它会对你有用。
public class PlayVideo extends Activity
{
//private String videoPath ="Url";
private static ProgressDialog progressDialog;
String videourl;
VideoView videoView ;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play_video);
videoView = (VideoView) findViewById(R.id.videoView);
progressDialog = ProgressDialog.show(PlayVideo.this, "", "Buffering
video...",true);
progressDialog.setCancelable(true);
PlayVideo();
}
private void PlayVideo()
{
try
{
getWindow().setFormat(PixelFormat.TRANSLUCENT);
MediaController mediaController = new MediaController(PlayVideo.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videourl);
videoView.setMediaController(mediaController);
videoView.setVideoURI(videoPath);
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
progressDialog.dismiss();
videoView.start();
}
});
}
catch(Exception e)
{
progressDialog.dismiss();
System.out.println("Video Play Error :"+e.toString());
finish();
}
}
}
您可以从以下链接中找到代码:
http://hasmukhbhadani.blogspot.in/search/label/Video-Play%20Live%20Streaming%20in%20Android.
答案 2 :(得分:0)
不建议使用ProgressDialog,因此我使用“警报对话框”在文件正在缓冲时显示加载对话框,然后开始播放:
private fun playVideo(videopath: String) {
Log.e("Playing Video File: ", "" + videopath);
try {
//Show Loader
val builder: AlertDialog.Builder = AlertDialog.Builder(this@ScreenCaptureImageActivity);
builder.setCancelable(false); // if you want user to wait for some process to finish,
builder.setView(R.layout.layout_loading_dialog);
progressDialog = builder.create();
//add Controller
val mediaController = MediaController(this@ScreenCaptureImageActivity);
videoView.setMediaController(mediaController)
//Add URI
//Uncomment to play from local path
//videoView.setVideoURI(Uri.parse(videopath))
//Example Play from Internet
videoView.setVideoPath("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")
videoView.setOnPreparedListener {
progressDialog!!.dismiss();
//Start Playback
videoView.start()
Log.e(TAG, "Video Started");
}
} catch (e: Exception) {
progressDialog!!.dismiss();
Log.e(TAG, "Video Play Error :" + e.localizedMessage);
}
}
加载程序XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<ProgressBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="Please wait! This may take a moment." />
</LinearLayout>
要进行网络访问,请在清单中添加网络配置,这是从Android P上获得的
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
>
在res / xml中添加network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>