Video automatically resumed when using Intent Chooser - android

I have an application which opens different videos using Intent chooser. So you can imagine user clicks on the list item which contains many elements with name of the video.
Now, the thing is working fine with this,
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uriPath, "video/mp4");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
but when the user again clicks on the same item, the intent chooser is shown. Selecting any video player in the device resumes the video.
So the question is, Is there any flag or some way in which the video can be started from the beginning ?
This thing not only happens with video but with pdf's. If an pdf was open and scrolled to the last page, then again opening the pdf again from my application opens it with the last page.

hi hardikI had a similar application to be developed where different
videos should be played when select different items from a list view.
I tried to test my application for the behavior that the video resumes
from last played frame if launch again from the app.
For me it always starts from the beginning. I have tested this on 4.2.2
and 4.4 versions only
The code to start the video was something like this:
Intent in = new Intent(Intent.ACTION_VIEW);
in.setDataAndType(uripath, "video/*");
startActivity(in);

This question is answered here already Android play video intent with start position, particularly in this comment from CommonsWare
Even if there were, they would be on a per-app basis.
If you need this level of control over the video playback,
handle the video playback yourself within your own app
(e.g., VideoView), rather than pass control to third party apps
See http://developer.android.com/reference/android/widget/VideoView.html#seekTo(int) and Playing a video in VideoView in Android
When you delegate to another application with an intent, you get what you get. You should control this within your app using the video player, and if you probably need a library for a pdf view widget. Something like http://code.google.com/p/apv/.

As you use an intent to play the video, it is really up to the acting application how to interpret and handle the intent. Unless the acting application (the one handling the intent you're firing) accepts extra parameters to choose between start-from-beginning and resume, there is really not much you can do.
For full control, use your own video player.

Use VideoView to resolve your problem.
private VideoView myVideoView;
VideoView has an inbuilt method start() which will start your video from the beginning like this:
#Override
public void onStart(){
super.onStart();
myVideoView.start();
}
You can also use the myVideoView.start() on onResume method as well.
Hope this would help. :)

I am completely unsure of this. I also agree on the other views that it is the responsibility of the called Application to handle the intent as it wishes.
However, a wild guess would be to change the uri:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
uriPath.buildUpon()
.appendQueryParameter("t", String.valueOf(System.currentTimeMillis()))
.build(),
"video/mp4"
);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Note: I did not have the problem that you are mentioning. The video always restarted.

Related

Android / Xamarin, Open file with intent and close app after back button pressed

I have a problem with a feature of my application.
I need to open external files to display them like .jpeg or .xls for example. For that I use Intents to open them with the default system application and it works fine.
But in a case where we will first open a document, for example a photo. this will display in the default app and then I go back and forth in my app. Everything is OK
But then I'm going to open another standard .xls document, it'll open the document in the appropriate application and going back will show the opened image first and not my application again.
I don't understand why it happens like this.
What I would like is that when we do previous in a file viewer app, the app quits and doesn't stay in the ActivityManager.
Is it possible ?
I have already tried some intent flags but without success.
My code here :
Android.Net.Uri fileUri = FileProvider.GetUriForFile(Android.App.Application.Context, "com.app.fileprovider"
, file);
Intent intent = new Intent(Intent.ActionView);
intent.SetData(fileUri);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.NoHistory);
Android.App.Application.Context.StartActivity(intent);
return true;
Thanks a lot for the help
Max B
SO !
My bad, It was not a real problem.
It was a design error.
Indeed during my call of my function to call the intention I linked my opening event without then unbinding it. So that with each call I recalled X times my file opening function without ever destroying my variable or my variable instance.
The Intent Works Well
Thanks for the comments

How to allow VideoView to continue playing when using Share? Android

I have a video playing full screen in the Android application. I've added a button to allow the user to bring up the "Share" dialog to share to social media, texting, or e-mail. However, when the user selects, for example Facebook, the video will stop playback. Is it possible to force the video to continue playing/rendering in the background with sound ON while the user is performing the share?
The code to Share is below:
public static void share(Activity activity, String subject, String body, String title)
{
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
activity.startActivity(Intent.createChooser(shareIntent, title));
}
VideoView is extremely finicky when it comes to lifecycle, and it stops the video the moment the activity hosting it is paused. Showing an intent chooser dialog effectively pauses your activity.
The preferred method (for both user experience and to handle your issue) would be to use ShareActionProvider to allow the user to select the sharing destination. This is a pop-up list, typically anchored to the action bar, which is a more modern method of showing these options. As an added benefit, the pop-up won't cause your activity to pause, so the video should not stop.
If you must still use the external dialog method, you will have to move away from VideoView and implement the video surface yourself with MediaPlayer. This isn't as scary as it sounds, you can see from the source code that most of VideoView is just wiring it up to MediaPlayer callbacks and attempting to manage state when the surface is created or destroyed.

How can i pass url to play song with samsung s3 inbuilt music player?

Intent intent = new Intent();
ComponentName comp = new ComponentName("com.android.music","com.android.music.MediaPlaybackActivity");
intent.setComponent(comp);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "audio/*");
startActivity(intent);
This is the code that i am using to open samsung player in other phones except galaxy S3. Regarding galaxy S3 this package is replaced with
ComponentName comp2 = new ComponentName("com.sec.android.app.music","com.sec.android.app.music.AudioPreview");
But in this case it is working only as audio preview but on pause it gets finished But i wanted play exactly like the one in the above as a music player . So for that i replaced ComponentName with this to open the musicplayer
ComponentName comp2 = new ComponentName("com.sec.android.app.music",
"com.sec.android.app.music.MusicActionTabActivity");
But in the case the player won't plays the url that i have passed. It just open's the default music player in S3. I need to play file with the android default music player.
Look Android is using AudioPlaybackPreview or someother named activity, to handle the song playback from fileManager or any other place .
Actually you want to use the default Playing activity ,which is used to show all the controls and all .Dear it's not possible to use that Activity , since it had been linked to someother service having data of all the songs and all paths. This playing Activity has control of next atnd previous and all , that is linked with one playback service.
Third thing is that , as far as i know that S3 is almost providing the same layout for playing the song from filemanager or for plaaying from any other activity. But the problem with that is that when u go back from there , it will stop the playing song.
Refer to android music player source code , u will understand why it's not possible to launch that activity.

Pause spotify playback intent

I am looking for a way to pause Spotify playback. I have tried searching, and using the AudioManager, but I believe that only works if it is part of your activity. Does anyone know a way to pause Spotify playback?
How to stop or pause Pandora and Spotify
That issue is similar and shows how to pause and stop most media players.
I found that this works best:
Intent pauseSpotify = new Intent("com.spotify.mobile.android.ui.widget.PLAY");
pauseSpotify.setPackage("com.spotify.music");
sendBroadcast(pauseSpotify);
Essentially, what you are doing is, creating a new intent which action is the built in "PLAY" action from the spotify app. Next you set the package and finally call the intent.
I got the idea from an article and applied it to normal android.
//play
Intent intent = new
Intent("com.spotify.mobile.android.ui.widget.PLAY");
intent.putExtra("paused",true);
intent.setPackage("com.spotify.music");
sendBroadcast(intent);
//pause
Intent intent = new
Intent("com.spotify.mobile.android.ui.widget.PLAY");
intent.putExtra("paused",false);
intent.setPackage("com.spotify.music");
sendBroadcast(intent);
I've analyzed Spotify's apk file and found the command file.
This would work perfectly on version 8.4.77
You can skip song by replacing "PLAY" to "NEXT".
I tried "PREVIOUS" but it didn't work, guessing it as spotify's default design.

Android - Picking a song to play using Intent

I am trying to pick a song using an Activity which will give meta-information about the songs. I want to do this instead of a simple file browser . I have the following code but it unfortunately also plays the song once clicked. I simply want the user to be able to select a song from their MediaStore and act upon it later without playing.
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_PICK);
intent.setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivity(intent);
}
}
As far as I can tell, the 'picker' you're seeing is a 'preview' type of design (looking at it on my phone).
It's a bit like 'picking' a ringtone, for example...
You see a list and can select each one in turn to get a 'preview'. When you decide on the ringtone you want, you click OK and that returns the selection. Clicking Cancel simply leaves things as they were (existing ringtone selection is kept).
I can't see any way of overriding this behaviour of the picker and haven't found any alternative way (Intent parameters, for example) to achieve what you want to do.
In other words, as I understand it, you simply want the user to silently pick a piece of music and it to return to your Activity but the (preview) picker doesn't work that way.
You can find out what the user previewed/selected when they click OK in the picker however, if you use...
startActivityForResult(intent, 1234);
Note, 1234 is just an arbitrary code.
If you check the Intent returned to onActivityResult() it will have the content Uri of the piece of music the user selected before they pressed OK.

Categories

Resources