Search code examples
androidandroid-videoviewgoogle-tvaudiovideoplayback

Google TV ― video playback stops working


I created a Google TV application that plays two local videos stored in the res/raw folder. The application starts running fine, but then locks up at random times. After it locks up, I get the "Sorry, cannot play this video." message when I try to restart the application. And not only is my application affected, but all other application video playback seems to be affected like the YouTube app. The error persists even after restarting the system. Has anybody else encountered anything like this, or has an idea on what may be causing it?

Here is my code, running on SonyNSZ-GS7:

The 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;
        }
    }
}

And the layout 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>

Solution

  • In switchVideo(), add at the beginning:

    videoView.setVisibility(View.GONE);
    

    and at the end:

    videoView.setVisibility(View.VISIBLE);
    

    so that it encloses the setVideoURI(). I know, it seems odd, but give it a try and say if it makes a difference.