视频适用于VideoView,而不适用于TextureView

时间:2017-02-15 16:40:38

标签: android streaming video-streaming android-mediaplayer textureview

我需要能够在视图内外制作视频动画,因此我使用了TextureView。我是从Node.js服务器上流式传输的(使用来自express的res.sendFile())。但是,当视频播放时,我得到的不是视频。

enter image description here

这是我的代码。我用谷歌搜索了几个小时,不知道发生了什么。 VideoView播放视频就好了。我在VideoStreaming方面经验不足,所以希望它显而易见,我只是不知道要去寻找。

package com.spectiv.displayer;

import android.animation.LayoutTransition;
import android.app.Application;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Objects;
import android.os.Handler;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class DisplayerActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener{
    private Handler mainHandler = new Handler();
    private FrameLayout layout;
    private TextureView textureView;
    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_displayer);
        layout = (FrameLayout) findViewById(R.id.rootLayout);
        textureView = new TextureView(this);
        textureView.setSurfaceTextureListener(this);
        currentView = textureView;
        layout.addView(textureView);
    }

    //  Trying a texture view with videos
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        Surface s = new Surface(surface);
        try {
            mMediaPlayer= new MediaPlayer();
            mMediaPlayer.setSurface(s);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource("http://192.168.1.211:6543/video");
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                @Override
                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    speak("Buffering");
                }
            });
            mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    speak("Video is Complete!");
                }
            });
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });
            mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                    // Nothing;
                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored
    }

    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mMediaPlayer.stop();
        mMediaPlayer.release();
        return true;
    }

    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Invoked every time there's a new Camera preview frame
    }
}

0 个答案:

没有答案