Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我无法从URL播放视频_Android - Fatal编程技术网

Android 我无法从URL播放视频

Android 我无法从URL播放视频,android,Android,我制作了一个应用程序,通过解析JSON来填充listview,当我点击listview时,新的活动会完美地打开,我想要从以前的活动中获得的所有变量都会被传输并完美地显示在当前的活动中,但视频不会播放 这是原木猫 10-28 15:01:01.547: V/VideoViewDemo(18257): path: http://www.example.com/mov.wmv 10-28 15:01:01.597: E/VideoViewDemo(18257): error: null 10-28 1

我制作了一个应用程序,通过解析JSON来填充listview,当我点击listview时,新的活动会完美地打开,我想要从以前的活动中获得的所有变量都会被传输并完美地显示在当前的活动中,但视频不会播放

这是原木猫

10-28 15:01:01.547: V/VideoViewDemo(18257): path: http://www.example.com/mov.wmv
10-28 15:01:01.597: E/VideoViewDemo(18257): error: null
10-28 15:01:01.597: E/VideoViewDemo(18257): android.os.NetworkOnMainThreadException
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at info.androidhive.jsonparsing.VideoViewDemo.getDataSource(VideoViewDemo.java:143)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at info.androidhive.jsonparsing.VideoViewDemo.playVideo(VideoViewDemo.java:124)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at info.androidhive.jsonparsing.VideoViewDemo.access$0(VideoViewDemo.java:108)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at info.androidhive.jsonparsing.VideoViewDemo$5.run(VideoViewDemo.java:101)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.app.Activity.runOnUiThread(Activity.java:4755)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at info.androidhive.jsonparsing.VideoViewDemo.onCreate(VideoViewDemo.java:99)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.app.Activity.performCreate(Activity.java:5177)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.os.Looper.loop(Looper.java:137)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at android.app.ActivityThread.main(ActivityThread.java:4950)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at java.lang.reflect.Method.invoke(Method.java:511)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
10-28 15:01:01.597: E/VideoViewDemo(18257):     at dalvik.system.NativeStart.main(Native Method)
10-28 15:01:01.627: D/dalvikvm(18257): GC_CONCURRENT freed 292K, 15% free 9978K/11651K, paused 15ms+35ms, total 179ms
10-28 15:01:01.727: I/Adreno200-EGLSUB(18257): <ConfigWindowMatch:2087>: Format RGBA_8888.
10-28 15:01:01.897: E/SpannableStringBuilder(18257): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
10-28 15:01:01.897: E/SpannableStringBuilder(18257): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
这是我的班级


主线程上不能有网络进程。通过使用runOnUiThread,您告诉android在主线程上运行playVideo,但在playVideo中,您有一个网络进程。在异步任务中运行getDataSource。另外,您不需要getDataSource函数中的runOnUiThread,您正在创建一个web连接。您正在主线程中调用此函数。web连接必须在后台线程中进行。尝试在AsyncTask或可运行线程中执行此操作。

Ugh,以前从未使用过AsyncTasks,您能从上面编辑我的代码吗??很appreciated@raklarAsyncTask易于使用。读一点,然后试试看。这将在将来对您有很大帮助:您能用上面的答案编辑我的代码吗?我根本不知道异步任务。非常感谢
package info.androidhive.jsonparsing;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoViewDemo extends Activity {
    private static final String TAG = "VideoViewDemo";

private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE_MOBILE = "mobile";
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    mVideoView = (VideoView) findViewById(R.id.surface_view);
    // getting intent data
    Intent in = getIntent();

    // Get JSON values from previous intent
    String name = in.getStringExtra(TAG_NAME);
    String email = in.getStringExtra(TAG_EMAIL);
    String mobile = in.getStringExtra(TAG_PHONE_MOBILE);

    // Displaying all values on the screen
   // TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblEmail = (TextView) findViewById(R.id.email_label);
    TextView lblMobile = (TextView) findViewById(R.id.mobile_label);

   // lblName.setText(name);
    lblEmail.setText(email);
    lblMobile.setText(mobile);

    mPath = (EditText) findViewById(R.id.path);
    mPath.setText(name);

   // mPath.setText("http://daily3gp.com/vids/747.3gp");

    mPlay = (ImageButton) findViewById(R.id.play);
    mPause = (ImageButton) findViewById(R.id.pause);
    mReset = (ImageButton) findViewById(R.id.reset);
    mStop = (ImageButton) findViewById(R.id.stop);

    mPlay.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            playVideo();
        }
    });
    mPause.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                mVideoView.pause();
            }
        }
    });
    mReset.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                mVideoView.seekTo(0);
            }
        }
    });
    mStop.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            if (mVideoView != null) {
                current = null;
                mVideoView.stopPlayback();
            }
        }
    });
    runOnUiThread(new Runnable(){
        public void run() {
            playVideo();

        }

    });
}

private void playVideo() {
    try {
        final String path = mPath.getText().toString();
        Log.v(TAG, "path: " + path);
        if (path == null || path.length() == 0) {
            Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();

        } else {
            // If the path has not changed, just start the media player
            if (path.equals(current) && mVideoView != null) {
                mVideoView.start();
                mVideoView.requestFocus();
                return;
            }
            current = path;
            mVideoView.setVideoPath(getDataSource(path));
            mVideoView.start();
            mVideoView.requestFocus();

        }
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }
}

private String getDataSource(String path) throws IOException {
    if (!URLUtil.isNetworkUrl(path)) {
        return path;
    } else {
        URL url = new URL(path);
        URLConnection cn = url.openConnection();
        cn.connect();
        InputStream stream = cn.getInputStream();
        if (stream == null)
            throw new RuntimeException("stream is null");
        File temp = File.createTempFile("mediaplayertmp", "dat");
        temp.deleteOnExit();
        String tempPath = temp.getAbsolutePath();
        FileOutputStream out = new FileOutputStream(temp);
        byte buf[] = new byte[128];
        do {
            int numread = stream.read(buf);
            if (numread <= 0)
                break;
            out.write(buf, 0, numread);
        } while (true);
        try {
            stream.close();
        } catch (IOException ex) {
            Log.e(TAG, "error: " + ex.getMessage(), ex);
        }
        return tempPath;
    }
}
}