Android Live Video Stream Not Playing - java

I am trying to stream a live feed in rtsp as such:
String uri = "rtsp://54.213.167.253:5544/63a1203d-4e12-438e-86ec-f447fa90cfd7";
Uri uri_add = Uri.withAppendedPath(MediaStore.Video.Media.INTERNAL_CONTENT_URI, "1");
videoView.setVideoURI(uri_add.parse(uri));
mediaController = new MediaController(_context);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
This works on an HTC, Sony, and LG device that I have tested with, however does not work on the Galaxy S6 or any Samsung device. I have researched the encoding compatibilities and h.264 is what my stream is encoded, which should work on all the devices I have. I am running Android v. 5.0.2 and 5.1.1 on these devices and there is no correlation between software to the issue. That is to say, the GalaxyS6 running 5.0.2 is not playing video while a HTC running 5.0.2 is playing video. I am completely lost as to what could be the cause of the "Can't Play Video" message that I get.
I have read all the articles and posts people have about streaming live video and attempted to implement them in my code, however I run in to the same issue each time. I am pretty sure there is nothing wrong with the code, else it would not work at all on any device. Anyone have any ideas what could be causing this and why?

This problem seems to be common on a few Samsung devices. Did you check what Logcat shows?
I had the same problem with a Galaxy Tab 4, I ended up using Vitamio's library for video streaming. It's not been supported for some time now, but pretty easy to use and for basic customization

Use this class. This is running on Samsung devices also.
private ProgressDialog progressDialog;
VideoView videoView;
private myAsync sync;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
String videourl = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
videoView = (VideoView) findViewById(R.id.video_view);
progressDialog = ProgressDialog.show(CustomizeProgressDialogActivity.this, "",
"Buffering video...", true);
progressDialog.setCancelable(false);
// progressDialog.dismiss();
MediaController mediaController = new MediaController(CustomizeProgressDialogActivity.this);
mediaController.setAnchorView(videoView);
Uri video = Uri.parse(videourl);// insert video url
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.requestFocus();
sync = new myAsync();
sync.execute();
// PlayVideo();
}
private class myAsync extends AsyncTask<Void, Integer, Void> {
int duration = 0;
int current = 0;
#Override
protected Void doInBackground(Void... params) {
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
videoView.start();
duration = videoView.getDuration();
}
});
do {
current = videoView.getCurrentPosition();
System.out.println("duration - " + duration + " current- "
+ current);
if (sync.isCancelled())
break;
}
while (current != duration || current == 0);
return null;
}
}

Related

Downloaded Videos Not Playable on Android Version <= 6.1

I have a video downloader android application It's allow people to download videos from twitter and Something has changed in 2 months Downloaded videos are not playable on Android version <= 6.0 Error is : "Can't Play This Video" Some of these videos playable but most of it is not. same format mp4.
I didn't make any changes in my code. I tried download files manually from browser and still error is occurs.
// Progress Dialog
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
// File url to download
private static String file_url = "https://video.twimg.com/ext_tw_video/1122253815884001280/pu/vid/1280x720/xTTWb4wnRMvFzpXk.mp4";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new DownloadFileFromURL().execute(file_url);
}
/**
* Showing Dialog
* */
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Bar Dialog
* */
#Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
#Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100%
// progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
OutputStream output = new FileOutputStream(Environment
.getExternalStorageDirectory().toString()
+ "/2011.mp4");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
* **/
#Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
}
}
I want to make these videos playable as others. Some of the mp4 videos plable and most of its is not. I don't know the reason is codec or not but I want to make these playable too.
This videos is example of situation.
https://video.twimg.com/ext_tw_video/1122253815884001280/pu/vid/1280x720/xTTWb4wnRMvFzpXk.mp4
Your example video uses H.264 profile of High #Level 3. Not supported in Android version <= 6.
H.264 is the "image" format of the video (where audio is MP3/AAC).
Lowest-to-high Profie order is: Baseline --> Main --> High.
See docs: https://developer.android.com/guide/topics/media/media-formats#video-codecs
MediaInfo analysis:
Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High#L3.1
Normally you fix by offering alternate encodings of the video file from your site. Since you're not in charge of Twitter server, you'll have to check if Twitter itself is keeping any Low/Standard-Def versions of uploaded videos, for older devices that can't handle High-Def. If found, then just offer users a multiple choice of "quality" links.
Alternatively try to see if FFmpeg can play the format. Try VLC Player app (is FFmpeg powered) on a problematic device. If it plays okay, then try importing Android-FFmpeg into your app code, where you use it to decode/play the downloaded videos in your app.

Is there any option to add user-agent in streaming media Exo Player?

I'm facing a problem with exomedia player, I want to add support for some types of streaming url s but exomedia won't accept them,I have tried to much time to add the option " User AGENT" but can't turn it on, I have added my code and if there is someone who can help to fix this ?
I have tried to add User -Agent ,but in this code The player runs smooth but not some type of urls
public class ActivityStreaming extends Activity implements OnPreparedListener {
private long exitTime = 0;
private VideoView videoView;
String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_streaming);
url = getIntent().getStringExtra("url");
videoView = (VideoView) findViewById(R.id.video_view);
videoView.setOnPreparedListener(this);
Uri videoUri = Uri.parse(url);
videoView.setVideoURI(videoUri);
}
#Override
public void onPrepared() {
//Starts the video playback as soon as it is ready
videoView.start();
}
public void onBackPressed() {
super.onBackPressed();
}
public void closeStreaming() {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(this, getString(R.string.press_again_to_close_streaming), Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
}
}
}

Android 6.0 - Set video speed with PlaybackParams

I have problems about how to implement PlaybackParams to set video speed:
public PlaybackParams getPlaybackParams ()
Added in API level 23
Gets the playback rate using PlaybackParams.
PlaybackParams setSpeed (float speed) //Sets the speed factor.
Returns:
the playback rate being used.
Throws IllegalStateException:
if the internal sync engine or the audio track has not been initialized.
This is my code:
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.setPlaybackParams(new PlaybackParams().setSpeed(1.f));
if (mPlaybackState == PlaybackState.PLAYING) { mVideoView.start();}
}
});
You are getting an IllegalStateException while calling the 'setPlayParams' method, because you're not doing PlaybackParams params = mp.getPlaybackParams(), set the speed and then pass it to mp.setPlaybackParams()! Set the speed DIRECTLY while calling the mp.getPlayParams()!
MediaPlayer mp = ...;
float speed = 0.55f;
mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed));
After many try i find a solution.
Example how use VideoView
final VideoView mVideoView = findViewById(R.id.videoView);
mVideoView.setVideoPath(Environment.getExternalStorageDirectory() + "/bluetooth/test.webm"); //Path of your file video
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(0.55f));
mVideoView.start();
}
});
MediaController media = new MediaController(this); //this is for play and restart play manually
media.setAnchorView(mVideoView);
mVideoView.setMediaController(media);
//mVideoView.start();
You have not started the Media Playe..
Try This Code:
#Override
public void onPrepared(MediaPlayer mp)
{
mp.start()
mp.setPlaybackParams(new PlaybackParams().setSpeed(1.f));
}

Google Play services activity recognition disconnects without any error

I am writing a background service (started by an activity) that records Screen on/off events and user's activity.
For activity, I am using google api client. The app works correctly on Moto G phones i.e. records both activity and screen but activity recognition stops on HTC one phone.
I have done few updates to code but still there is an issue that activity recognition stops after few minutes. As suggested by another member, I also exported both the android-support-v4.jar and android-support-v7-appcompat.jar files but still the issue is there.
The phone's location is on and it is not on power saving mode. Also, I updated my SDK as well as google play services on phone to the latest one, but still my api client disconnects after few minutes. Below are the code files that I used.
Please help me to correct this. I am using eclipse.
MyActiviy:
public class MyActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private PendingIntent pIntent;
GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(ActivityRecognition.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
mGoogleApiClient.connect();
IntentFilter filter = new IntentFilter();
filter.addAction("ACTIVITY_RECOGNITION");//For filtering
}
#Override
public void onConnected(Bundle arg0) {
Intent intent = new Intent(this, ActivityRecognitionService.class);
pIntent = PendingIntent.getService(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 0, pIntent);//0
}
//#Override
public void onConnectionSuspended(int arg0) {
// TODO Auto-generated method stub
mGoogleApiClient.connect(); //I found this recently, but still app doesn't works
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
}
}
ActivityRecognitionService
public class ActivityRecognitionService extends IntentService {
private String TAG = "appLogs...";
private long fName;
public ActivityRecognitionService() {
super("My Activity Recognition Service");
}
#Override
protected void onHandleIntent(Intent intent) {
if(ActivityRecognitionResult.hasResult(intent)){
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
Log.i(TAG, getType(result.getMostProbableActivity().getType()) + "t" + result.getMostProbableActivity().getConfidence());
}
}
private String getType(int type){
if(type == DetectedActivity.UNKNOWN)
return "Unknown";
else if(type == DetectedActivity.IN_VEHICLE)
return "In Vehicle";
else if(type == DetectedActivity.ON_BICYCLE)
return "On Bicycle";
else if(type == DetectedActivity.ON_FOOT)
return "On Foot";
else if(type == DetectedActivity.STILL)
return "Still";
else if(type == DetectedActivity.TILTING)
return "Tilting";
else if(type == DetectedActivity.RUNNING)
return "Running";
else if(type == DetectedActivity.WALKING)
return "Walking";
else
return "";
}
As I have just answered here, it seems that there is no way around. Activity reporting will stop after phone is "still" for some time.
If you want to record even if the phone is "still", I see two ways:
1) rely entirely on the ActivityRecognition API and record "still" until a SIGNIFICANT_MOTION will be detected by Google Services and ActivityRecognition start send you new updates;
2) to write your own simple StillActivityRecognitionService, which starts when there is no updates from "official" API. This Service should listen to the accelerometer sensors, interpret sensor events (deviation from the mean, peak values etc.) and send it's decision "still"/"not still".

when video finished go back to activity

I have some videos using videoView. everything is running well, but when the video is finished nothing happens, the phone stay on dark screen waiting to pres play again or the blink back on the phone.
I want that when videos is finished, go back to the activity before. this is my code.
public class chapterone extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.videoplayer);
VideoView videoView =
(VideoView)this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
videoView.setVideoURI(Uri.parse(
"http://video.com/episode/" +
"cotton1.mp4"));
/* videoView.setVideoPath(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES) +
"/movie.mp4");
*/
videoView.requestFocus();
videoView.start();
}
}
the URL is only an example.
videoView.setOnCompletionListener(completionListener);
OnCompletionListener completionListener=new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
chapterone.this.finish();
}
};
i hope can help i found this, but i am not sure if is correct uset it
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mp){
// invoke your activity here
Intent i = new Intent(chapterone.this, videoserie.class);
startActivity(i);
}
});

Categories