2016-11-25 211 views

回答

1

你提的問題是非常通用的,但如果你想從公共URL播放視頻,你可以做這樣的事情(從Android文檔中提取:https://developer.android.com/guide/topics/media/mediaplayer.html):

String url = "http://........"; // your URL here 
MediaPlayer mediaPlayer = new MediaPlayer(); 
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
mediaPlayer.setDataSource(url); 
mediaPlayer.prepare(); // might take long! (for buffering, etc) 
mediaPlayer.start(); 

如果你需要的東西更先進的,你應該嘗試ExoPlayer:https://developer.android.com/guide/topics/media/exoplayer.html

但如果你只是想下載的視頻數據以某種方式操縱,你可以使用純HttpURLConnection的(而不是在主線程):

URL url = new URL("http://....."); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.connect(); 
OutputStream output = null; // whatever is your destination (file, memory, network) 
InputStream input = conn.getInputStream(); 
byte data[] = new byte[4096]; 
int count; 
while ((count = input.read(data)) != -1) { 
    output.write(data, 0, count); 
} 
input.close(); 
output.close(); 
conn.disconnect(); 
相關問題