1

リモートURLからビデオを再生しようとしましたが、正常に動作します。問題は、ビデオが自動的に再生を開始することです。中央に再生ボタンがあり、クリックするとビデオが再生されるYouTubeビデオのように表示されます。 。

public class Tabs extends Activity {
TabHost th;
VideoView video;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Create tabs using XML 

    video=(VideoView) findViewById(R.id.VideoView);
    String path1="http://www.w3schools.com/html5/movie.mp4";
    MediaController mc = new MediaController(this);
    mc.setAnchorView(video);
    mc.setMediaPlayer(video);
    Uri uri=Uri.parse(path1);
    video.setMediaController(mc);
    video.setVideoURI(uri);
    video.start();
}
}

これは完全に正常に動作する私のコードですが、ビデオは自動的に再生を開始します。中央に再生ボタンがあり、クリックするとビデオの再生が開始されるYouTubeビデオのような最初のシーンを表示する必要があります。また、ビデオのサムネイルを表示する必要があります

4

4 に答える 4

2

でビデオプレーヤーとURLを設定し、onCreateを実装して実際の再生を行いますonClickListener

あなたの例では(不足しているパッケージをインポートし、IDがのボタンを作成することを忘れないでくださいbuttonStart):

public class Test extends Activity {
    TabHost th;
    VideoView video;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Create tabs using XML

        video = (VideoView) findViewById(R.id.VideoView);
        String path1 = "http://www.w3schools.com/html5/movie.mp4";
        MediaController mc = new MediaController(this);
        mc.setAnchorView(video);
        mc.setMediaPlayer(video);
        Uri uri = Uri.parse(path1);
        video.setMediaController(mc);
        video.setVideoURI(uri);
        Button buttonStart = (Button) findViewById(R.id.buttonStart);
        buttonStart.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                video.start();
            }

        });

    }
}

サムネイルの表示に関しては、そのURLがあれば、リモート画像を取得してビデオプレーヤーにオーバーレイする非同期タスクを開始できます(FrameLayoutオーバーレイビューを実現するために使用します)。次に、オーバーレイ画像にonClickListenerを設定します(ボタンに対してどのように行われるかについてのコードを参照してください。同じロジックに従うことができますImageView)。ImageViewがクリック可能に設定されていることを確認してください。

于 2012-10-15T09:11:46.603 に答える
0

ColorWPの助けを借りてこれを動作させたので(どうもありがとう)私は自分がしたことを投稿すると思いました:

これが私のxmlで、フレームレイアウトを使用してサムネイルと再生画像を重ね合わせています。

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


    <TextView
        android:id="@+id/textView1"
        android:layout_width="292dp"
        android:layout_height="100dp"
        android:layout_x="7dp"
        android:layout_y="16dp"
        android:text="" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="281dp"
        android:layout_x="1dp"
        android:layout_y="138dp"
         >
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <VideoView
            android:id="@+id/svid"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />



        <ImageView
            android:id="@+id/simg"
            android:layout_width="187dp"
            android:layout_height="137dp"
            android:src="@drawable/ic_action_search"
            android:layout_centerInParent="true" />
        <ImageView
            android:id="@+id/playimg"
            android:layout_width="100dp"
            android:layout_height="87dp"
            android:src="@drawable/animated_loading"
            android:layout_centerInParent="true" 
          />
       </RelativeLayout>
    </FrameLayout>

コードでは、再生画像にClickListenerを設定するだけだったので、ユーザーが画面で再生画像を押すと、video.start()を使用しました。それは完璧に機能しました:)

これが私のコードの抜粋です:

 protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single);
        vid=(VideoView) findViewById(R.id.svid);
        iv=(ImageView) findViewById(R.id.simg);
        ip=(ImageView) findViewById(R.id.playimg);
        t=(TextView) findViewById(R.id.textView1);
        final ProgressDialog pd=new ProgressDialog(SingleItem.this);

        link="http://www.w3schools.com/html5/movie.mp4";
         String path1=link;
            MediaController mc = new MediaController(this);
            mc.setAnchorView(vid);
            mc.setMediaPlayer(vid);
            uri=Uri.parse(path1);
            vid.setMediaController(mc);
            vid.setVideoURI(uri);
            vid.requestFocus();

            loadImage(thumb);

            ip.setClickable(true);
            ip.setImageResource(R.drawable.play);
            // ip.setVisibility(ImageView.INVISIBLE);
            vid.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    //ip.setVisibility(ImageView.VISIBLE);
                    pd.dismiss();

                }
            });
            ip.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                     vid.start();
                     pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        pd.setMessage("Loading Video...");
                        pd.setIndeterminate(false);
                        pd.setCancelable(true);
                        pd.show();

                        if(vid.isPlaying()){
                             iv.setVisibility(ImageView.INVISIBLE);
                             ip.setVisibility(ImageView.INVISIBLE);
                         }else{
                             iv.setVisibility(ImageView.VISIBLE);
                             ip.setVisibility(ImageView.VISIBLE);
                         }

                }
            });

お役に立てれば :)

于 2012-10-20T03:43:25.913 に答える
0

メソッドでストリーミングする代わりにonCreate、ボタンを追加してリスナーを追加し、クリックイベントでビデオをストリーミングすることができます

于 2012-10-15T09:04:31.487 に答える
0

video.start();を書く あなたのボタンonClickリスナー。

ありがとう。

于 2012-10-15T09:04:01.483 に答える