Note that there are some explanatory texts on larger screens.

plurals
  1. POVideo Streaming not works on Google TV in an Android Application in 3.1
    primarykey
    data
    text
    <p>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 <strong>MediaPlayer</strong> to play my streams and Channels play properly MOST of the time. </p> <p>I have overridden <strong>setOnErrorListener</strong> to know if something is wrong. This catches most of the errors if any and i <strong>finish()</strong> the player activity after showing proper message.</p> <p>Initially i show a <strong>loading</strong> message in a progress bar to the user while the stream is getting ready. i <strong>dismiss()</strong> the progress bar and <strong>start()</strong> the MediaPlayer after <strong>setOnPreparedListener</strong> listener is called.</p> <p>I am unable to run the rtsp:// stream. it hangs after playing a little.</p> <ol> <li>the Progress Bar remains there and the app is kind of stuck. </li> <li>Pressing <strong>Back</strong> button on D-Pad does not work either</li> <li>I receive no errors in <strong>setOnErrorListener</strong> listener at all</li> <li>I cannot close the application as whenever i click on my App's icon, it opens the same black "Loading" player page..</li> </ol> <p><strong>Android 3.1 (API 12) is used and all stream URLs start with rtsp://</strong></p> <p>Following are the xml/java file's code:</p> <p><strong>NOTE: I copied and altered the VideoPlayer.java &amp; video_player.xml files <a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html" rel="nofollow">MediaPlayer_Demo example</a>.</strong></p> <p><strong>video_player.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000000"&gt; &lt;SurfaceView android:id="@+id/surface" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;/SurfaceView&gt; &lt;/LinearLayout&gt; </code></pre> <h2>Erroneous VideoPlayer.Java is as follows:</h2> <pre><code>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 &amp;&amp; mIsVideoSizeKnown) { startVideoPlayback(); } } public void onPrepared(MediaPlayer mediaplayer) { Log.d(TAG, "onPrepared called"); mIsVideoReadyToBePlayed = true; if (mIsVideoReadyToBePlayed &amp;&amp; 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); } } </code></pre> <p>I created <strong>UserMessage</strong> &amp; <strong>AppStatus</strong> classes for making development fast, I learnt them from SO. These classes are fine and, according to my knowledge &amp; understanding, are not causing any errors. I am providing them just to share and have better understanding of <strong>VideoPlayer.java</strong>.</p> <p>These classes are as follows just to share so anyother person could use them:</p> <p><strong>UserMessage.Java</strong></p> <pre><code>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(); } } </code></pre> <p><strong>AppStatus.Java</strong></p> <pre><code>public class AppStatus { public static boolean isInternetAvailable(Context context) { ConnectivityManager mgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = mgr.getActiveNetworkInfo(); return (netInfo != null &amp;&amp; netInfo.isConnected() &amp;&amp; netInfo.isAvailable()); } } </code></pre> <p>Any help is greatly appreciated as i am almost at the end of finishing my app.</p> <p>Thanks for reading and thinking even if you could not answer me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload