1

我正在尝试使用实时视频流制作一个 android 应用程序,但是每当相关活动(这个)打开时,除了空白屏幕之外什么都不显示。谁能帮帮我吗?

       package guc.edu.iremote;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class Video extends Activity  implements OnTouchListener{

VideoView videoView;
int stopPosition;
boolean touched;
protected WakeLock mWakeLock;
Dialog dialog;



@SuppressWarnings("deprecation")
public void OnCreate(Bundle b) {
    super.onCreate(b);
    this.setContentView(R.layout.video);



    videoView = (VideoView) findViewById(R.id.videoView1);


    PowerManager lPwrMgr = (PowerManager) getSystemService(POWER_SERVICE);
    mWakeLock = lPwrMgr.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,                 "Video");
    mWakeLock.acquire();


    videoView.setVisibility(View.VISIBLE);

    MediaController mc = new MediaController(this){
        @Override
        public void hide() {
            this.show(0);
        }

        @Override
        public void setMediaPlayer(MediaPlayerControl player) {
            super.setMediaPlayer(player);
            this.show();
        }
    };
    videoView.setMediaController(mc);
    mc.setAnchorView(videoView);

      dialog = ProgressDialog.show(Video.this, "", "Loading...",
            true);
    new Thread(new Runnable() {
        public void run() {

             String str = "rtsp://v4.cache2.c.youtube.com/"+
                    "CkELENy73wIaOAng93Xa-"+ 
                        "iQH5xMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoWglDbGlja0xpbmtgg9fFkeTLublGDA==/"+ 
                    "0/0/0/video.3gp";  

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            Uri uri = Uri.parse(str);
            videoView.setVideoURI(uri);
             videoView.setOnTouchListener(this);
            videoView.requestFocus();
            videoView.setZOrderOnTop(false);
            videoView.start();
            dialog.dismiss();
        }
    }).start();





}



@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //videoView.seekTo(stopPosition);
    //videoView.resume();
}


protected void onPause() {
    super.onPause();
    if (videoView != null) {
        stopPosition = videoView.getCurrentPosition();
        videoView.pause();
    }

}



public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    if(v==videoView)
    {
        System.out.println("touched"); 

        if(!touched)
        {
            onPause();
            touched = true;
        }
        else
            onResume();
            touched  = false;
    }
    return false;
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

    // release wake-lock 
    if(mWakeLock != null){
        mWakeLock.release();
    }
}
}

这适用于 XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout> 
4

2 回答 2

0

那个链接是什么:

rtsp://v4.cache2.c.youtube.com/CkELENy73wIaOAng93Xa-iQH5xMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoWglDbGlja0xpbmtgg9fFkeTLublGDA==/0/0/0/video.3gp

?

它似乎不起作用。如果该链接已损坏,则应该修复该链接,然后尝试。但是...要验证这是问题所在,您可以将其替换为指向另一个正常工作的视频文件的链接吗?(如果您需要一些,请告诉我,但谷歌可以提供大量 :)

编辑

正如另一个人(之前的其他答案)评论的那样,您还需要在主线程中访问您的 UI,而不是在后台线程中。所以你应该发布到 UI 线程,如:

Runnable r = new Runnable(){ /* the updating videoView stuff you wanted to do*/ };
ctx.runOnUIThread(r); 

wherectx是对您当前活动的引用。

于 2012-11-19T17:39:49.020 回答
0

还:

videoView.setZOrderOnTop(false); <- wrong
videoView.setZOrderOnTop(true); <- right

于 2015-01-08T08:39:04.207 回答