In my project I want to dowload a Youtube URL and play it in media player but as far I have searched I got an idea that Youtube URLs can be played only if they are converted into a RTSP file. I don't have any idea of how to do that. It would be very helpful if you send me a sample project.
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;
}
}
}
Use http://gdata.youtube.com/feeds/mobile/videos/T9W4jgq9RfQ api
public class MainActivity extends Activity {
String video_id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String link = "http://www.youtube.com/watch?v=T9W4jgq9RfQ&feature=related";
String pattern = "(?:videos\\/|v=)([\\w-]+)";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(link);
if(matcher.find()){
System.out.println(matcher.group().substring(2));
video_id=matcher.group().substring(2);
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc1=db.parse("http://gdata.youtube.com/feeds/mobile/videos/"+video_id);
Element rsp = (Element)doc1.getElementsByTagName("media:content").item(1);
String anotherurl=rsp.getAttribute("url");
System.out.println("my "+anotherurl);
// Element rsp = (Element)doc1.getElementsByTagName("media:content").item(1);
} catch (Exception e) {
System.out.println("Pasing Excpetion = " + e);
}
}
Related
i had developed one application to live streaming video, but it does not support android version 2.2 ,2.3 or etc. only play video on android version 4.1 (Samsung Galaxy Grand).
I am using videoview in my project.
So can you tell me the exact reason.
My code is as follow:
mPath.setText("http://iptvshqip.dyndns.tv:8090");
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(MainActivity.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
}
else {
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;
}
}
Android started HTTP Live Stream(HLS) support from 3.0. Read the following link.
http://www.longtailvideo.com/blog/31646/the-pain-of-live-streaming-on-android/
They have explained with workaround.
As stated, the Android VideoView has existed since API level 1.
Except:
flags STATUS_BAR_HIDDEN and STATUS_BAR_VISIBLE - deprecated in API 14
the function setBackgroundDrawable(Drawable background) deprecated in API 16
the Public Methods - canPause(), canSeekBackward() and canSeekForward() added in API 5
resume() and suspend() added in API 8
onInitializeAccessibilityEvent (AccessibilityEvent event) and onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info) added in API 14
setOnInfoListener (MediaPlayer.OnInfoListener l) added in API 17
Do you use any of these? Your problem might be elsewhere anyway.
Please paste some code and describe with more details how is this not working.
I am trying to run Android's MediaPlayer using runOnUiThread. I did not caught any exception with setDataSource. But after that, nothing happens with MediaPlayer. It should give callback as surface changed and onPrepared.
It seems MediaPlayer doesn't support this way.
If it is true, are there any workarounds ?
I need this kind of logic because I need to get info with network query which is blocked. I need to run onSuccess from that.
What is your suggestion for this? Thanks very much!
onResume()
{
getInfo(xxx);
}
void getInfo(url, new DataListener() {
#Override
public void onDataSuccess(xxx) {
playVideoOnSuccess(xxx);
}
}
public void playVideoOnSuccess(xxx)
{
myBaseActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
try {
mPlayerListener = new VideoPlayerListener(null, content);
// create new mediaplayer
mVideoPlayer = VideoPlayer.getInstance();
mVideoPlayer.setVideoPlayerListener(mPlayerListener);
// setDataSource
mVideoPlayer.consumeContent(content);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Here is a sample code that i used to play video hope this might help you in some way
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;
}
}
}
This work for me
I'm working with VideoView to stream a video with rtsp. I want to obtain details about it so I want to use android.net.rtp. When I call any RtpStream class's method like getLocalPort() the application doesn't work. Here is the code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.edit_message);
text= "rtsp://192.168.1.37/sample_300kbit.mp4";
mPath.setText(text);
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) {
reproducirVideo();
}
});
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() {
reproducirVideo();
}
});
}
public void reproducirVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(MainActivity.this, "Campo URL vacío",
Toast.LENGTH_LONG).show();
} else {
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
TextView texto= (TextView) findViewById(R.id.mensaje);
InetAddress address = null;
mRtpStream.associate(InetAddress.getByAddress(new byte[] {(byte)192, (byte)168, (byte)1, (byte)37 }), 1220);
address=mRtpStream.getRemoteAddress();
String address_string = address.getHostAddress();
texto.setText(" La dirección local es "+(address_string)+" ");
}
} 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;
}
}
}
I would like to develop a android app with video player that renders diffrent qualities of same video just like in youtube change quality feature.
Proir to that i am curious to know whether the source should contain videos of all the qulity options provided to render them dynamically.
Any help is sincerly appreciated.
My code to dtream video from URL.
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;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
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(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;
}
}
}
Thanks,
Ajay
Hi got it solved by below methods on setOnClickListener event get the current postion of videow view and change the video source to HD and use SeekTo method with current position and then start HD video so that HD video cans tart from current postion of previous SD video.
I am making an android application to record sound. I have been using class MediaRecorder for that.
The recording is done successfully when the recording is done for the first time on any device. But it doesn't work next time.
If I try the code on another machine, recording is done successfully. Is it so because MediaRecorder instance is not getting released by the app even if I am calling method release().
This is my code-
public class NewClass extends Activity {
private static String sFileName = null;
private static String sFileNameMSD = null;
private static String sPlayFile = null;
public MediaRecorder mRecorder = null;
private String mPn_id;
private String mDescription;
private String mTask_flag;
private String mPatient_name;
private String mPatient_id;
private String mIs_upload = "N";
private Context mContext;
Button btnStart;
Button btnStop;
private int mReuqestcode;
NewClass myActivity;
SeekBar seekBar;
int totalTime;
private final Handler handler = new Handler();
private final String FILE_EXTENTION_AMR = ".amr";
private final String FILE_EXTENTION_MSD = ".msd";
protected boolean recodeFlag = false;;
private static final String TAG = GridAllActivity.class.getName();
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.transcriptionrecord_activity);
mContext = this;
myActivity = this;
Main.setTitle(myActivity, mContext);
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(
mContext));
TextView txtTitle = (TextView) findViewById(R.id.txt_Title1);
txtTitle.setText("Transcription");
TextView txtOption = (TextView) findViewById(R.id.txtOptionName);
TextView txtPatientName = (TextView) findViewById(R.id.txtPatientName);
setProperty();
txtOption.setText("" + mDescription);
txtPatientName.setText("" + mPatient_name);
}
private void onRecord(boolean start) throws SettingNotFoundException {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void startRecording() throws SettingNotFoundException {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
mRecorder.setOutputFile(sFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
mRecorder.start();
} catch (IOException e) {
recodeFlag=true;
Log.e("1", "prepare() failed");
e.printStackTrace();
}
}
private void stopRecording() throws SettingNotFoundException {
mRecorder.stop();
mRecorder.release();
Log.d("1", "mRecorder released");
mRecorder = null;
System.gc();
}
public void setRecordPath() throws IOException {
sFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
sFileName += Global.creteFolderPath("IMSEMR" + mTask_flag + "_"
+ mPn_id + FILE_EXTENTION_AMR);
sFileNameMSD = "IMSEMR" + mTask_flag + "_" + mPn_id
+ FILE_EXTENTION_MSD;
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
Global.alertbox("", "SD Card is not mounted.", mContext);
throw new IOException("SD Card is not mounted. It is " + state
+ ".");
}
File directory = new File(sFileName).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
directory.mkdirs();
throw new IOException("Path to file could not be created.");
}
}
public void setRecodingLayout() {
btnStop.setEnabled(false);
try {
btnStart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
boolean mStartRecording = true;
try {
btnStop.setEnabled(true);
btnStart.setEnabled(false);
onRecord(mStartRecording);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
}
});
btnStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
boolean mStartRecording = false;
try {
onRecord(mStartRecording);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
} catch (SQLiteException e) {
// TODO: handle exception
} catch (Exception e) {
// TODO: handle exception
}
}
});
} catch (NullPointerException e) {
}
}
public void setProperty() {
Bundle bundel = getIntent().getExtras();
mReuqestcode = (Integer) bundel.get("REQUEST_CODE");
// property for Record
if (mReuqestcode == 1) {
mPn_id = (String) bundel.get("PN_ID");
mDescription = (String) bundel.get("DESCRIPTION");
mTask_flag = (String) bundel.get("TASK_FLAG");
mPatient_id = (String) bundel.get("PATIENT_ID");
mPatient_name = (String) bundel.get("PATIENT_NAME");
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
btnStop.setEnabled(false);
setRecodingLayout();
try {
setRecordPath();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
You need to create a new instance of media recorder each time you want to record (making sure to release the previous one).
Update --
Sorry, you only need to create a new instance if you call release on the object. Otherwise you can just call reset,
read this: http://developer.android.com/reference/android/media/MediaRecorder.html
Example:
Lets say you have an activity with 2 buttons, a record button and a play button
public class MyRecordDemo extends Activity {
private static final String PATH_NAME = "/sdcard/myfile.3gp"; //probably shouldn't hardcode this
private Button mRecordButton, mStopButton;
private MediaRecorder mRecorder = null;
private boolean mIsRecording = false;
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.recorder_demo);
mRecordButton = (Button) findViewById(R.id.record_btn);
mStopButton = (Button) findViewById(R.id.stop_btn);
mStopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mIsRecording && mRecorder != null) {
mRecorder.stop();
mRecorder.release();
}
}
});
mRecordButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mIsRecording && mRecorder != null) {
mRecorder.stop();
mRecorder.release();
}
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(PATH_NAME);
mRecorder.prepare();
mRecorder.start()
}
});
}
}