Control Android Media Player - java

I'm looking for a way to control the default Android media player from my own service. I'm NOT interested in playing media from my service or activity; I want to control the existing application, which consists of an activity (MediaPlaybackActivity.java) and more importantly a service (MediaPlaybackService.java) located in packages/apps/Music/src/com/android/music/. Ideally, I would like a solution that is independent of the version of Android.
I have figured out how to do pause/play/stop/next/previous operations using Intents. I can intercept track change events using a broadcast receiver. I can also get a list of playlists and the contents of each playlist. What I would like to be able to do is instruct the MediaPlaybackService to play a specific file/song. Again, I don't want to play this song in my application; I want the Android default media player to play it.
I have tried two approaches so far:
I imported the IMediaPlaybackService.aidl file from packages/apps/Music/src/com/android/music into my own application, and used this to bind to the MediaPlaybackService. On Froyo, this works great; I can pass a path to the openFile method, and the service will play the file. However, on Gingerbread, I get an error: Permission Denial: Accessing service ComponentInfo{com.google.android.music/com.android.music.MediaPlaybackService} from
pid=17721, uid=10058 requires null. Finding a workaround to this error would be a good temporary solution, but it's not future-proof. This service may well change again in future versions of Android.
Starting the media player via an ACTION_VIEW Intent. This also works; however, as expected, the media player UI is brought to the front, which is not ideal.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(someUri, "audio/mp3");
startActivity(intent);
Is there any other way to accomplish this? Is there an Intent I missed that would instruct the media player to play a specific song? Or alternatively, is there away to start an activity in the background, or to start an activity and immediately switch back to the previous one?

Sorry, there is no supported way to do this. All of the stuff you are describing is using private implementation details, and as you have seen is not robust. If you want to play music, you should do it in your own app.

I'm looking for a way to control the default Android media player from my own service.
There is no "default Android media player". The application you are referring to may exist on some devices and will not exist on others. AFAICT, few devices will have what you think is the "default Android media player" going forward -- those not already running proprietary media players from device manufacturers may have the new proprietary Google Music app.

Related

How to make a phone call without switching activities to a default calling app in android?

I'm trying to make a phone call from my app in a way it does not need to switch activities, But every guide I find has the following code snippet,
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:XXXXXXXXX"));
startActivity(callIntent);
which switches the activity I start the call from to another activity (in this case to an activity in a different app). Is there a way to stop this from happening? I managed to do it with a third party library called "sinch" but I'm wondering if there is a native way to do it or maybe a better library?
Ps- the app I'm building it for myself, basically, I'm building a voice assistant that can make calls via voice commands, hence I can't let it switch activities. I have no intention of publishing it on the app store and I have no difficulty giving dangerous permissions :) My plan is to run it on a separate piece of hardware in the future.
This link can help you, but as Xavier Rubio Jansana wrote previously, Google hardly accepts applications that do not use intents to make phone calls :
https://google-developer-training.github.io/android-developer-phone-sms-course/Lesson%201/1_c_phone_calls.html
Google wants any programmer to use an intent to make the user view the default phone application handle the phone call.
If your app does not need to be available on Google Play Store then it would be ok for you to make the phone call directly.
To elaborate on what I was talking about earlier, it is talked about in this stack overflow question (perhaps upvote their answers if they are helpful). How to make a phone call using intent in Android?.
From memory, ACTION_DIAL will bring up the dialler and pre-populate the number.. it requires no special permission because it requires the user to actually place the call.
On the other hand, ACTION_CALL will actually initiate the call and requires special permissions.
If returning focus (bringing your app back to the foreground) is a concern, you could try using a scheduled intent to (re) launch your activity (maybe using alarm manager or similar) some time after invoking the dialler. You can retain state with a little care around launch modes in the intent and/or a special "do nothing" activity published in your app manifest which does nothing but re-activate your app.

Android Studio - Route all audio from phone (calls, music player, etc.) through App

I've made an app in Android Studio which is essentially a parametric EQ with some additional features.
The app takes signal from the microphone source (whether the built-in mic or external mic). The signal is then passed through biquad filters and out to the speakers/earphones. I used LibPb to handle the signal processing.
Now, I want all the audio that would be coming out the speakers to be routed through the app.
In other words, any music playing or calls, I want the audio to also be passed through the filters in my app.
I have tried to find articles/resources on this topic but haven't found much.
I'm guessing that it can be done via Audio Focus or Media Router or something along those lines, but I cant figure it out!!
Firstly, is this possible?
Secondly, any code or hints as to how to go about it?
Any help is much appreciated!!

Route phone audio call to a custom sound service in android?

I have implemented my custom audio interface which is derived from AudioHardwareBase, and I have initialized it in AudioFlinger constructor class. This class will stream the audio through network.
This work fine when I play sound from a file, but not when make phone call.
The problem is due probably because the audio of phone call come from hardware unlike audio file (which come from software).
I'm willing to modify basic android library/driver in order to get this work.
Any idea how can I overcome this issue?
Thanks

creating a service for playing audio in the background

I have searched, but can not find the answer I'm looking for.
I want to create a service and be able to call a function to play an audio file.
I have 5 different intents that will be using the service to play 5 different audio files (one each) and I have a stop button in each one. Whichever stop button is pressed I want it to stop all audio that has been called from the service.
Does any one have an example code which I can use? (Something close to what I'm looking for.)
I'm building a media player to understand how Android works, and I have a service that queues audio files to play them. You can get the full source code from github.
The service is in /src/com/augusto/mymediaplayer/services
I'm not using Intens, but binding the service to the Activities that use it, but changing it to receive intents shouldn't be that hard. Just in case, this service runs ok on Android 1.6+.
I think that to change it to receive intents, you'll need to change onStart() and do a switch on the intent.
I know that this doesn't answer your question 100% but it's a place to start :).

How to play media with my own media player as default

I am an Android developer. I've developed a media player that can play both local and streaming videos. But there is a problem. When I click a local media in the gallery how can I configure it to be played with my player? At least, there should be a list of media players including mine one and I should be able to choose it from this list. After succeeding this, I want to open rtsp protocoled files with my player as default. How can I do this? Waiting for your help.
What you are looking for is called an Intent Filter. Specifically, you want to set up an intent filter on the type of media you are playing, a data type intent filter. There is more information on this in the Android SDK.

Categories

Resources