我已经创建了一个从Parsing JSON填充listview的应用程序,当我点击listview时,新活动完全打开,我想要的所有变量从前一个活动转移并显示在当前活动上完美无缺,但是视频无法播放。
这是log Cat;
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
这是我的班级;
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;
}
}
}
答案 0 :(得分:0)
您无法在主线程上拥有网络进程。通过使用runOnUiThread
,您告诉Android在主线程上运行您的playVideo
,但在您的playVideo中,您有一个网络进程。在AsyncTask中运行getDataSource()
。此外,您不需要runOnUiThread
答案 1 :(得分:0)
在getDataSource()函数中,您正在创建Web连接。你在主线程中调用这个函数。必须在后台线程中建立Web连接。尝试在AsyncTask或可运行的线程中执行此操作。