2013-02-26 24 views
5

Ben android video görünümünde stream video form url istiyorum. Benim iş parçacığı üzerinde bu işlemek için herhangi bir şekilde orada alt sının i video akışı için uithread kullanıyorum görebilirsiniz benim ihtiyaç .Benim kod İşteAndroid Video Başka bir Thread & Sayı android ile görüntü 2.1

public class VideoViewDemo extends Activity { 

private static final String TAG = "VideoViewDemo"; 
private String current; 

/** 
* TODO: Set the path variable to a streaming video URL or a local media 
* file path. 
*/ 
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4"; 
private VideoView mVideoView; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.videoview); 
    mVideoView = (VideoView) findViewById(R.id.surface_view); 
    runOnUiThread(new Runnable() { 
     public void run() { 
      playVideo(); 
     } 
    }); 
} 

private void playVideo() { 
    try { 
     // final String path = path; 
     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.setMediaController(new MediaController(this)); 
      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; 
    } 
} 
} 

olduğunu ulaşmak için bu örnek API kodu ve yapılan küçük bir değişiklik kullanan denedim ne

new Thered(new Runnable() { 
     public void run() { 
      playVideo(); 
     } 
    }).start(); 

olduğunu Ama Android 2.2 İlk kod çalışır planda çalışırken aynı zamanda

ve başarısız ve error(-38,0) gösterir i n Android 2.1 bu hata nedir? This File'u kontrol ettim, ancak bu hangi hatayı bulamadı?

Herkes bana rehberlik edebilir mi ??

+0

Düğme 'Video Oynat' deyin ve tıklama işlevinde Runnable Thread kodunuzu yerleştirin. OnCreate() işlevindeki kendi iş parçacığınızın olması, etkinlik oluşturmanın her zaman iyi olması gerektiğinden uygun şekilde ele alınmadığında dağınık olabilir. Bu arada Android emülatöründe veya Telefonda bunu denediniz mi? –

+0

@RahulSundar Telefonda denedim. Düğmeyi kullanmak istemedim. Etkinliğe girdiğimde videoyu doğrudan oynatmak istiyorum. bu yüzden hiç bir buton kullanmadım – edwin

+0

Tamam. Sadece her şeyin iş parçacığıyla iyi çalışıp çalışmadığını kontrol etmek için bir düğmeden başlatın. Sonra aynı OnCreate() işlevinde kullanın. Etkinlik oluşturma işleminin tamamlanmasından sonra video oluşturma işleminin başladığından emin olmak için, yeterli Gecikme süresini (5 saniye) verin ve her şeyin düzgün çalışıp çalışmadığını kontrol edin. –

cevap

2

Videonun tamamını getirip dosya sistemiyle kaydetmeniz gerekmiyor, daha sonra çalıştırın ... Belirtmiş olduğunuz video 32Mb boyutuna sahip ve ağ üzerinden almak için çok zaman alacak. Bunun yerine, video görüntüsüne doğrudan bağlantı verebilirsiniz, videonun aşamalı olarak getirilmesini/tamponlanmasını ve oynatmasını sağlar. Video verilerini UI dizisinde almaya çalışıyorsunuz, bu kabul edilemez. İşte düzeltilmiş kod, kontrol edebilirsiniz.

public class VideoViewDemo extends Activity { 

private static final String TAG = "VideoViewDemo"; 
private String current; 

/** 
* TODO: Set the path variable to a streaming video URL or a local media 
* file path. 
*/ 
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4"; 
private VideoView mVideoView; 

private Handler handler; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    handler = new Handler(); 
    setContentView(R.layout.videoview); 
    mVideoView = (VideoView) findViewById(R.id.surface_view); 
/* runOnUiThread(new Runnable() { 
     public void run() { 
      playVideo(); 
     } 
    }); 
*/  
    playVideo(); 
    Log.v(TAG, "activity oncreate finished"); 
} 

private void playVideo() { 
    try { 
     // final String path = path; 
     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(path); 
      mVideoView.start(); 
      mVideoView.setMediaController(new MediaController(VideoViewDemo.this)); 
      mVideoView.requestFocus(); 

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

} 
+0

@edwin, cevabım faydalı mıydı? – Suji

+0

Kod için teşekkürler! Başka bir sorum var, videonun tamamen izlendikten sonra cihazda bir yere kaydedilip kaydedilemeyeceğini biliyor musunuz? Orası erişilebilir mi? Ayrıca, bu konuda [soru] (http://stackoverflow.com/questions/23842450/android-where-do-videoview-and-mediacontroller-store-media-downloaded-with-htt) adresini de görebilirsiniz. – nburk