Video Streaming not works on Google TV in an Android Application in 3.1

1.3k Views Asked by At

I am new to Android development and i have been asked to create an App in Android for Google TV that will show a user some live channels and will play a live stream when user selects one. I have managed to do so and its working fine. I used MediaPlayer to play my streams and Channels play properly MOST of the time.

I have overridden setOnErrorListener to know if something is wrong. This catches most of the errors if any and i finish() the player activity after showing proper message.

Initially i show a loading message in a progress bar to the user while the stream is getting ready. i dismiss() the progress bar and start() the MediaPlayer after setOnPreparedListener listener is called.

I am unable to run the rtsp:// stream. it hangs after playing a little.

  1. the Progress Bar remains there and the app is kind of stuck.
  2. Pressing Back button on D-Pad does not work either
  3. I receive no errors in setOnErrorListener listener at all
  4. I cannot close the application as whenever i click on my App's icon, it opens the same black "Loading" player page..

Android 3.1 (API 12) is used and all stream URLs start with rtsp://

Following are the xml/java file's code:

NOTE: I copied and altered the VideoPlayer.java & video_player.xml files MediaPlayer_Demo example.

video_player.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000">

    <SurfaceView android:id="@+id/surface"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </SurfaceView>
</LinearLayout>

Erroneous VideoPlayer.Java is as follows:

public class VideoPlayer extends Activity implements OnBufferingUpdateListener,
    OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener,
    SurfaceHolder.Callback, OnErrorListener {

private static final String TAG = "MediaPlayerDemo";
private int mVideoWidth;
private int mVideoHeight;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
private Bundle extras;
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;
ProgressDialog progressDialog;

/**
 * 
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.video_player);

    this.extras = getIntent().getExtras();

    mPreview = (SurfaceView) findViewById(R.id.surface);
    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

private void playVideo() {
    doCleanUp();
    try {

        this.progressDialog = ProgressDialog.show(this, "", "Loading...",
                true);

        // Check Internet Status
        if (!AppStatus.isInternetAvailable(this)) {
            showErrorToUser(getResources().getText(R.string.about_net)
                    .toString(),
                    "Internet connection is not available or Wi-Fi is not enabled.");
        } else {
            // Create a new media player and set the listeners
            String videoStreamUrl = extras.getString("streamUrl");

            if (videoStreamUrl != null) {
                mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setDataSource(videoStreamUrl);
                mMediaPlayer.setDisplay(holder);
                mMediaPlayer.setOnBufferingUpdateListener(this);
                mMediaPlayer.setOnCompletionListener(this);
                mMediaPlayer.setOnPreparedListener(this);
                mMediaPlayer.setOnVideoSizeChangedListener(this);
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setOnErrorListener(this);
                mMediaPlayer.prepareAsync();
            }
            else
            {
                showErrorToUser("Invalid Stream URI",
                        "The Video's URI is not valid. Please provide correct URI.");
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
        errorOccured();
    }
}

public void onBufferingUpdate(MediaPlayer arg0, int percent) {
    Log.d(TAG, "onBufferingUpdate percent:" + percent);

}

public void onCompletion(MediaPlayer arg0) {
    Log.d(TAG, "onCompletion called");
}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.v(TAG, "onVideoSizeChanged called");
    if (width == 0 || height == 0) {
        Log.e(TAG, "invalid video width(" + width + ") or height(" + height
                + ")");
        return;
    }
    mIsVideoSizeKnown = true;
    mVideoWidth = width;
    mVideoHeight = height;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void onPrepared(MediaPlayer mediaplayer) {
    Log.d(TAG, "onPrepared called");
    mIsVideoReadyToBePlayed = true;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
    Log.d(TAG, "surfaceChanged called");

}

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
    Log.d(TAG, "surfaceDestroyed called");
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "surfaceCreated called");
    playVideo();
}

@Override
protected void onPause() {
    super.onPause();
    releaseMediaPlayer();
    doCleanUp();
}

@Override
protected void onStop() {
    super.onStop();
    releaseMediaPlayer();
    doCleanUp();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
    doCleanUp();
}

private void releaseMediaPlayer() {
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

private void doCleanUp() {
    mVideoWidth = 0;
    mVideoHeight = 0;
    mIsVideoReadyToBePlayed = false;
    mIsVideoSizeKnown = false;
}

private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
    holder.setFixedSize(mVideoWidth, mVideoHeight);

    this.progressDialog.dismiss();

    mMediaPlayer.start();
}

public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
    errorOccured();
    return true;
}

private void errorOccured() {
    UserMessage
            .showPopup(
                    this,
                    "Channel not available",
                    "We are sorry for the inconvenience but this channel is not available right now. Please try later.");
}

private void showErrorToUser(String title, String message) {
    // Hide Progress Bar
    this.progressDialog.dismiss();
    releaseMediaPlayer();
    doCleanUp();

    UserMessage.showPopup(this, title, message);
    }
}

I created UserMessage & AppStatus classes for making development fast, I learnt them from SO. These classes are fine and, according to my knowledge & understanding, are not causing any errors. I am providing them just to share and have better understanding of VideoPlayer.java.

These classes are as follows just to share so anyother person could use them:

UserMessage.Java

public class UserMessage {
public static void showPopup(final Activity activity, String title, String message) {

    View view = View.inflate(activity, R.layout.about, null);
    TextView textView = (TextView) view.findViewById(R.id.message);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(message);
    new AlertDialog.Builder(activity)
            .setTitle(title)
            .setView(view)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    activity.finish();
                }
            }).show();
    }
}

AppStatus.Java

public class AppStatus {

public static boolean isInternetAvailable(Context context)  {               

    ConnectivityManager mgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = mgr.getActiveNetworkInfo();

    return (netInfo != null && netInfo.isConnected() && netInfo.isAvailable());
    }
}

Any help is greatly appreciated as i am almost at the end of finishing my app.

Thanks for reading and thinking even if you could not answer me.

1

There are 1 best solutions below

7
On BEST ANSWER

The answer to your problem is that while we do support RTSP, it's done in software, not hardware. When you need to rescale it, that too, is done in in software. When you present a fairly large size, like yours, and it needs to be scaled, it under runs. Giving the behavior your seeing. MP4's are a better solution.