3.1 中的 Android 应用程序中的视频流无法在 Google TV 上运行
我是 Android 开发新手,我被要求在 Android 中为 Google TV 创建一个应用程序,该应用程序将向用户显示一些直播频道,并在用户选择一个频道时播放直播。我已经成功做到了并且工作正常。我使用 MediaPlayer 来播放我的流,并且频道在大多数情况下都能正常播放。
我已经重写了 setOnErrorListener 以了解是否有问题。这会捕获大多数错误(如果有),并在显示正确的消息后 finish() 玩家活动。
最初,当流准备就绪时,我在进度栏中向用户显示正在加载消息。在调用 setOnPreparedListener 侦听器后,我 dismiss() 进度条和 start() MediaPlayer 。
我无法运行 rtsp:// 流。玩一会儿后就挂了。
- 进度条仍然在那里,应用程序有点卡住了。
- 按 D-Pad 上的 后退 按钮不起作用,
- 我在 setOnErrorListener 侦听器中根本没有收到任何错误
- 我无法关闭应用程序,因为每当我单击应用程序的图标时,它就会打开相同的黑色“正在加载”播放器页面。
使用 Android 3.1 (API 12),所有流 URL 均以 rtsp:// 开头
以下是 xml/java 文件的代码:
注意:I复制并更改了VideoPlayer.java & video_player.xml 文件 MediaPlayer_Demo 示例< /a>.
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>
错误的VideoPlayer.Java如下:
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);
}
}
我创建了UserMessage & AppStatus 类用于加快开发速度,我从 SO 那里学到了它们。这些课程很好,根据我的知识和经验,这些课程很好。理解,不会造成任何错误。我提供它们只是为了分享并更好地理解 VideoPlayer.java。
这些类如下只是为了分享,以便任何其他人都可以使用它们:
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());
}
}
非常感谢任何帮助,因为我即将完成我的工作应用程序。
感谢您的阅读和思考,即使您无法回答我。
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.
- the Progress Bar remains there and the app is kind of stuck.
- Pressing Back button on D-Pad does not work either
- I receive no errors in setOnErrorListener listener at all
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题的答案是,虽然我们确实支持 RTSP,但它是通过软件而不是硬件完成的。当您需要重新缩放它时,这也可以在软件中完成。当您提供相当大的尺寸(例如您的尺寸)并且需要缩放时,它会运行不足。让你看到的行为。 MP4 是更好的解决方案。
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.