2014-10-28 87 views
0

我已經制作了一個應用程序,它可以從解析JSON中填充listview,當我點擊listview時,新的活動完全打開,我想從前一個活動中獲得的所有變量都被傳輸並顯示目前的活動無懈可擊,但視頻不播放。我無法播放來自URL的視頻

這裏是日誌貓;

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

你不能在主線程上的網絡進程。通過使用runOnUiThread,你告訴android在主線程上運行你的playVideo,但在你的playVideo中,你有一個網絡進程。在AsyncTask中運行您的getDataSource()。此外,您不需要runOnUiThread

+0

你可以用上面的答案編輯我的代碼,我根本不知道Async Task。非常感謝 – raklar 2014-10-28 19:22:22

0

在getDataSource()函數中,您正在創建Web連接。你在主線程中調用這個函數。網絡連接必須在後臺線程中進行。嘗試在AsyncTask或可運行線程中執行此操作。

+0

呃,從來沒有使用AsyncTasks之前,你可以從上面編輯我的代碼?非常感謝 – raklar 2014-10-28 19:21:52

+0

@raklar AsyncTask易於使用。只是讀一點,然後嘗試一下。它將在未來幫助你很多:) – berserk 2014-10-28 19:25:19

+0

我不知道老兄。 – raklar 2014-10-28 19:44:35