Android - 为什么我的应用程序不断弹出“视频无法播放屏幕”
代码有问题吗?有什么原因不能播放吗?
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.widget.VideoView;
public class Video extends Activity implements OnCompletionListener, OnPreparedListener {
static private final String pathToFile ="com.chich.res.drawable- hdpi.troopers.3gp"; // Video source file
private VideoView videoPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
videoPlayer = (VideoView) findViewById(R.id.videoPlayer);
videoPlayer.setOnPreparedListener(this);
videoPlayer.setOnCompletionListener(this);
videoPlayer.setKeepScreenOn(true);
videoPlayer.setVideoPath(pathToFile);
}
/** This callback will be invoked when the file is ready to play */
@Override
public void onPrepared(MediaPlayer vp) {
if(videoPlayer.canSeekForward()) videoPlayer.seekTo(videoPlayer.getDuration()/5);
videoPlayer.start();
}
/** This callback will be invoked when the file is finished playing */
@Override
public void onCompletion(MediaPlayer mp) {
// Statements to be executed when the video finishes.
this.finish();
}
/** Use screen touches to toggle the video between playing and paused. */
@Override
public boolean onTouchEvent (MotionEvent ev){
if(ev.getAction() == MotionEvent.ACTION_DOWN){
if(videoPlayer.isPlaying()){
videoPlayer.pause();
} else {
videoPlayer.start();
}
return true;
} else {
return false;
}
}
}
is there something wrong with the code? Some reason it won't be played?
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.widget.VideoView;
public class Video extends Activity implements OnCompletionListener, OnPreparedListener {
static private final String pathToFile ="com.chich.res.drawable- hdpi.troopers.3gp"; // Video source file
private VideoView videoPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
videoPlayer = (VideoView) findViewById(R.id.videoPlayer);
videoPlayer.setOnPreparedListener(this);
videoPlayer.setOnCompletionListener(this);
videoPlayer.setKeepScreenOn(true);
videoPlayer.setVideoPath(pathToFile);
}
/** This callback will be invoked when the file is ready to play */
@Override
public void onPrepared(MediaPlayer vp) {
if(videoPlayer.canSeekForward()) videoPlayer.seekTo(videoPlayer.getDuration()/5);
videoPlayer.start();
}
/** This callback will be invoked when the file is finished playing */
@Override
public void onCompletion(MediaPlayer mp) {
// Statements to be executed when the video finishes.
this.finish();
}
/** Use screen touches to toggle the video between playing and paused. */
@Override
public boolean onTouchEvent (MotionEvent ev){
if(ev.getAction() == MotionEvent.ACTION_DOWN){
if(videoPlayer.isPlaying()){
videoPlayer.pause();
} else {
videoPlayer.start();
}
return true;
} else {
return false;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的视频是应用程序资源的一部分,我认为 setVideoPath 不起作用。使用 URI:使用 URI 引用 Android 资源。有一个使用 VideoView 的示例。
I don't think setVideoPath will work if your video is part of your apps resources. Use a URI: Referring to Android resources using URIs. There's an example of using the VideoView.