【问题标题】:is it possible to display video information from an rtsp stream in an android app UI是否可以在 android 应用程序 UI 中显示来自 rtsp 流的视频信息
【发布时间】:2010-03-23 08:54:57
【问题描述】:

我已经设法获得一个可以流式传输 rtsp 链接的工作视频播放器,但是我不确定如何在 UI 中显示视频当前时间位置,我使用了 getDuration 和 getCurrentPosition 调用,将此信息存储在一个字符串中并试图在 UI 中显示它,但它似乎不起作用

**in main.xml:**
    TextView android:id="@+id/player"
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:layout_margin="1px"
           android:text="@string/cpos"
          />

**in strings.xml:** 

string name="cpos">"" /string>


**in Player.java**

private void playVideo(String url) {
  try {
   media.setEnabled(false);

   if (player == null) {
    player = new MediaPlayer();
    player.setScreenOnWhilePlaying(true);
   } else {
    player.stop();  
    player.reset();
   }

   player.setDataSource(url);
   player.getCurrentPosition();
   player.setDisplay(holder);
   player.setAudioStreamType(AudioManager.STREAM_MUSIC);
   player.setOnPreparedListener(this);
   player.prepareAsync();
   player.setOnBufferingUpdateListener(this);
   player.setOnCompletionListener(this);

  } catch (Throwable t) {
   Log.e(TAG, "Exception in media prep", t);
   goBlooey(t);
   try {
    try {
     player.prepare();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    Log.v(TAG, "Duration: ===> " + player.getDuration());
   } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

   }
  }
 }


private Runnable onEverySecond = new Runnable() {
  public void run() {
   if (lastActionTime > 0
    && SystemClock.elapsedRealtime() - lastActionTime > 3000) {
    clearPanels(false);
   }

   if (player != null) {
    timeline.setProgress(player.getCurrentPosition());
    //stores getCurrentPosition as a string
    cpos = String.valueOf(player.getCurrentPosition());
    System.out.print(cpos);

   }

   if (player != null) {
    timeline.setProgress(player.getDuration());
    //stores getDuration as a string
    cdur = String.valueOf(player.getDuration());
    System.out.print(cdur);
   }

   if (!isPaused) {
    surface.postDelayed(onEverySecond, 1000);
   }
  }
 };

【问题讨论】:

    标签: android timestamp video-streaming


    【解决方案1】:

    您的代码 sn-p 看起来很像我的 vidtry 示例。 getCurrentPosition()getDuration() 适用于 HTTP 流,例如用于更新进度条。

    我没有尝试使用 RTSP 视频流进行 vidtry,主要是因为我不知道。

    【讨论】:

      【解决方案2】:

      检查来自服务器的 SDP 响应,以确保它正在发送响应中的持续时间(实时流没有可识别的时间,这可能导致客户端不提供此信息。)

      例如实时提要如下所示:

      a=range:npt=0-
      

      VoD 剪辑应如下所示:

      a=range:npt=0-399.1680
      

      如果getCurrentPosition() 不起作用,但您知道持续时间(getDuration() 起作用,或者您有其他获取此信息的方法;您可以通过观察缓冲事件并自行跟踪来计算它。您的方法是比这种方法更理想的方法。

      【讨论】:

        【解决方案3】:

        如果我没听错,你想在 TextView 中显示经过的时间,例如hh:mm:ss?

        如果是这样,我将向您介绍如何做到这一点。

        私有 TextView mElapsedTimeText; 私有视频视图 mVideoView; 私有线程 mThread; @覆盖 公共无效 onCreate(Bundle savedInstanceState) { /* 这里是你的代码 */ // 假设您的 ID 是 elapsedId 和 videoId mElapsedTimeText = (TextView) findViewById(R.id.elapsedId); mVideoView = (VideoView) findViewById(R.id.videoId); mThread = 新线程() { @覆盖 公共无效运行(){ mElapsedTime.setText(getNiceString()); mVideoView.postDelayed(mThread, 1000); } } /* 这里是你的代码 */ } 公共字符串 getNiceString() { 字符串结果 = ""; int 位置 = mVideoView.getCurrentPosition(); /* 这里是你的代码 */ //结果是 hh:mm:ss 格式的字符串 返回结果; } @覆盖 公共无效onPrepared(MediaPlayer mp){ /* 这里是你的代码 */ // 你必须在某处触发进程 mVideoView.postDelayed(mThread, 1000); /* 这里是你的代码 */ }

        还有一件事我忘了提。为了完成这项工作,您的活动类必须实现 OnPreparedListener 接口。

        我希望你或其他人会发现这篇文章有用。

        最好的问候,

        伊戈尔

        【讨论】:

          猜你喜欢
          • 2013-03-04
          • 2018-11-03
          • 2012-12-21
          • 2015-11-06
          • 2018-01-14
          • 2010-11-23
          • 1970-01-01
          • 1970-01-01
          • 2022-10-21
          相关资源
          最近更新 更多