我创建了一个Google TV应用程序,可播放存储在res/raw
文件夹中的两个本地视频。应用程序开始正常运行,但随后会随机锁定。锁定后,当我尝试重新启动应用程序时,我收到“抱歉,无法播放此视频。”消息。我的应用程序不仅受到影响,而且所有其他应用程序视频播放似乎都会像YouTube应用程序一样受到影响。重启系统后,错误仍然存在。有没有其他人遇到这样的事情,或者对可能导致它的原因有所了解?
这是我的代码,在SonyNSZ-GS7上运行:
MainActivity
:
public class MainActivity extends Activity {
int currentVideo = R.raw.ahwvideo;
VideoView videoView;
@SuppressLint("DefaultLocale")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Code to Display Video
videoView = (VideoView)findViewById(R.id.VideoView);
videoView.setVideoURI(Uri.parse(
"android.resource://" + getPackageName() +"/"+R.raw.ahwvideo1));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
switchVideo(videoView);
videoView.start();
}
});
videoView.start();
}
public void switchVideo(VideoView videoView) {
if (currentVideo == R.raw.ahwvideo1) {
videoView.setVideoURI(Uri.parse("android.resource://"
+ getPackageName() +"/"+R.raw.ahwvideo2));
currentVideo = R.raw.ahwvideo2;
} else {
videoView.setVideoURI(Uri.parse("android.resource://"
+ getPackageName() +"/"+R.raw.ahwvideo1));
currentVideo = R.raw.ahwvideo1;
}
}
}
布局activity_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<VideoView
android:id="@+id/VideoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
答案 0 :(得分:0)
不知道它是否真的重要但似乎视频名称中有错误:
videoView.setVideoURI(Uri.parse(“android.resource://”+ getPackageName()+“/”+ R.raw.video2));
应该是:R.raw。* ahw * video2
答案 1 :(得分:0)
在switchVideo()中,在开头添加:
videoView.setVisibility(View.GONE);
最后:
videoView.setVisibility(View.VISIBLE);
以便它包含setVideoURI()。 我知道,这看起来很奇怪,但试试看它是否有所作为。