MediaPlayer doesn't play in background when started by an intent - android

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.

Related

Need to stop an app that was launched via Context.startActivity();

My app launches an app to display images selected by the user, but it needs to be able to terminate the displayer app on certain conditions. Here's what I have so far:
Uri attachmentUri = Uri.fromFile(new File("/sdcard/" + filename));
attachmentViewIntent = new Intent(Intent.ACTION_VIEW);
attachmentViewIntent.setDataAndType(attachmentUri, contentType);
attachmentViewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
try {
mContext.startActivity(attachmentViewIntent);
} catch (Exception e) {
}
The images get displayed just fine, but I don't see how to kill the display app. I tried
mContext.stopService(attachmentViewIntent);
but that had no effect (I'm not sure I want to stop the service anyway, just the displayer). Any ideas?
Once started the new activity, it will be up to the user to stop it (or less) and you have no control on how and when it will happen.
Calling Context.stopService(Intent) is used to stop a working Service, hence you have no effect because you started an Activity instead.
EDIT:
If you are not familiar with the concept of task in Android you should absolutely take a look at the docs:
Navigation between apps
Tasks and back stack

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.

How to close Android player

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);

Android - Opening Default Music Library Properly

In my Android application I have a button which should open the Music Library of device.
I'm using the following code now
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);
I understand that there can be devices without default music library.
So, I want the code snippet which will open music library if it is possible and show some message(or something else), otherwise.
Surround it in a try/catch block. If an ActivityNotFoundException is thrown, then there is no component on the device capable of receiving that intent:
try {
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);
catch (ActivityNotFoundException anfe) {
// Handle no component found here (e.g. show a toast or dialog)
}

Can you start the default music player in the background?

I have 2 areas in my app that launch the default media player on the device and play an audio file (a podcast). One gets it from the file system after a download, the other streams it from the file on the site.
This is working fine right now, problem is if I hit back or home to try and get back to my app after it launches the media player, the player closes and the playback stops.
Is there a way to launch it in the background or as a service so that when I go back to the app, the music keeps playing in the player. I want it to function as if you started a song from the music player. So the user can use the device as normal and the playback will continue.
Here's my code for launching the music player for streaming.
private void playPodcast( Podcast podcast ) {
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setDataAndType( Uri.parse( podcast.getContentLink() ), "audio/*" );
startActivity( intent );
}
Thanks in advance.
EDIT! After receiving the advice below, I tried launching it as a Service. I've tried this in 2 ways.
Both ways have this as the manifest entry:
<service android:name=".DefaultMusicPlayerLaunchService">
</service>
The first attempt was to make a Service class which was merely a skeleton and launching it with a similar method as above.
DefaultMusicPlayerLaunchService V1:
public class DefaultMusicPlayerLaunchService extends Service {
#Override
public IBinder onBind( Intent intent ) {
// TODO Auto-generated method stub
return null;
}
public int onStartCommand( Intent intent, int flags, int startId ) {
return START_STICKY;
}
}
playPodcast Method:
private void playPodcast( Podcast podcast ) {
Intent intent = new Intent( getApplicationContext(),
DefaultMusicPlayerLaunchService.class );
intent.setDataAndType( Uri.parse( podcast.getContentLink() ), "audio/*" );
startService( intent );
}
My thought was that this way, it would launch the music player similarly to the way it was in the Activity I had before, but launch it without leaving the screen it was on. I didn't see anything happen in the LogCat. I feel like this should work but I'm missing something.
The other method I attempted involved serializing the podcast object as an extra and putting it on the Intent I made for the DefaultMusicPlayerLaunchService. Then, in the onStartCommand method, I grabbed the object, created a new Intent and then called startActivity similarly to how the original playPodcast method at the top does. And as expected, it does just what the top one does... launches the player but as soon as I hit back or home, it closes.
I apologize if I'm missing something obvious. I'm new to Android and everything I've worked with up to now have used Activity. This is my first attempt at starting a Service.
Yes you can start it as a service. You need to create a class that extends android.app.Service. And call startService(intent) from an Activity passing it an intent that will start the service. Override the onStartCommand method and have it return START_STICKY which will have the service run until its explicitly stopped. Either by you in code or by the Android OS to conserve resources. You can also override onBind to bind the service to an Activity which will give the service the same importance as a foreground Activity which makes it less likely that Android will kill the service to save resources.

Categories

Resources