File file = new File(mFileName);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
In my application, I am using this code and playing an audio file but when it's finished playing the player doesn't close. Is there a way to close the player after the whole audio is played ?
For example https://gist.github.com/850599.
I tried your code on my android phone, after the music was played the default player closed and return to the last activity. So Maybe it depends on the configuration of default player.
I think it's a better way to use MediaPlayer instand of default player which is more certain.
By the way, You can try kill the default player process. Get the time of the music, and start a thread to kill the process after the play time.
Runtime.getRuntime().exec("kill" +pid);
Related
In my app would like user to click on a button on actionbar. This action to invoke default mediaplayer, so that user can select any music as they like. Need ability to return to the app while the music is played in the background.
Any help is appreciated.
Following code works as intended...
Intent intent = new Intent();
intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
Intent.CATEGORY_APP_MUSIC);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Min SDK 15
startActivity(intent);
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.
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.
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.
In my app I download an mp3 podcast and then play it with MediaPlayer using an intent:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("file://" + filepath);
intent.setDataAndType(data, "audio/*");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
This works, but unfortunately the player pauses as soon as I leave it, ie. when it goes to the background. When opened manually the player happily plays in the background.
What do I need to make it play in background when starting it with an intent?
Just to clarify, I don't want to start the player in the background, I just wonder why it stops when it looses focus.
What do I need to make it play in background when starting it with an intent?
You don't do it that way. You use the MediaPlayer class and operate it from within a service.