0
在我的项目中,我想下载Youtube网址并在媒体播放器中播放它,但就我搜索的内容而言,我有一个想法,即Youtube网址只有在它们被转换为RTSP文件时才能播放。我不知道如何做到这一点。如果你给我一个样本项目,这将是非常有帮助的。如何将YouTube网址转换为RTSP网址?
public class Video_Media_PlayerActivity 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;
String tempPath;
private VideoMediaViewApplication appObj;
private int mVideoQuality = 80;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
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(Video_Media_PlayerActivity.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");
//prepareDirectory();
//File file1 = new File(path);
File file1 = File.createTempFile("mediaplayertmp", "dat");
file1.deleteOnExit();
tempPath = file1.getAbsolutePath();
FileOutputStream out = new FileOutputStream(file1);
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;
}
}
}
我可以给你一个名字:JDownloader是关于一个下载管理器的开源项目,也是用Java编写的,并从Youtube下载视频时,它完美的作品,也许你可以下载源代码并搜索正确的模块或者在他们的论坛中提问。 jdownloader.org – Micro