creating a service for playing audio in the background - java

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 :).

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.

How to record video in background when having video calling in application

I am developing an application that will be use to record video in background once the video calling starts.I googled about it but didn't get any solution. Many articles say that it is not possible.
Please let me know the way to implement the functionality
What you can do is that you can simply record the screen of the phone. There are several libraries available for implementing that. You can trigger or alert the library to start recording the screen once the video call starts.

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!!

Download multiple files in background in Android

Basically I'm trying to make a little app for watching offline content. So there's a moment where the user selects to download the contents (and the app should download about 300 small files and images).
I'd like to show the user how does the process go if he enters the proper activity. Showing a list of all the files, telling what has been already downloaded, in progress or waiting for download.
My problem is that I really don't know what approach to take for achieve this. Since the download should last until finished I imagine the solution is an Service, but whats best? an IntentService, a Bound Service or an Standard Service calling a startService() for each download? And how can I keep my objects updated for displaying them later? should I use a database or objects in memory?
Thanks
I would suggest using AsyncTask class, it allows you to easily move time consuming code(like downloading files) to a different thread. This will keep your app responsive, while giving you the ability to update your UI in the process.
It's hard to be more specific without having more details about how exactly you want your app to behave. Are the downloads only going to happen when the app is running or in the background as well?
You could use Asynctask or implement a ExecutorService with custom policies and send to it the download threads.
You need to keep a reference to the AsyncTask or a Future object respectively inside of a collection if you want to give the oportunity to the user to stop downloads.
Of course, you need to call startService each time you want to download a new file.
Service onCreate only is called if service is not running and onStartCommand run each time you call startService. In onStartCommand you run a new thread for download a new file.
You can bind service with an activity and each time that your downloadsActivity is created you show the state of downloads implementing a custom Adapter. Service only finishes when you call activity.stopService or service.stopSelf

Control Android Media Player

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.

Categories

Resources