Android: Launch music player service from application - android

I know that it is possible to launch the music player using an intent using either of the follow:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
or
Intent intent = new Intent(MediaStore.CATEGORY_APP_MUSIC);
startActivity(intent);
However is it possible to start the default music player as a SERVICE rather than an activity? Moreover, is it possible to send actions to that service once it has been created?

However is it possible to start the default music player as a SERVICE rather than an activity?
No, in part because there is no requirement for the user's chosen music player to even have a service. Moreover, the ACTION_VIEW Intent action is only generally supported by activities.
Moreover, is it possible to send actions to that service once it has been created?
Not generally.
There thousands of "music player" apps for Android, any of which can be the "default" for a given user and device. You are welcome to ask each and every one of them if they offer some sort of command-based interaction with some sort of music-playing Service to allow third-party apps to control their player. Some might. Most won't.

Related

Why does an "Intent Selector" exists, what is it used for?

Android's Intent class provides an API called setSelector. I am trying to understand it from the example given in the documentation.
I want to ask that why did Android need to add this API ? What was breaking in Intent before this API ?
My understanding from reading the references is that the problem this API is intending to solve is where you want to send a launcher intent for an app that meets some general restrictions. Say you want to match all apps that open .mp3 files, but you don't want to actually open an mp3 file, you just want to launch an app that supports that. In that case, you could create a generic ACTION_MAIN, CATEGORY_LAUNCHER intent, and set the selector to an intent with an mp3 mime type or data URI.
Before this API there would be no way to do that - if you wanted to target an app that supports opening mp3s, you would have to send an intent for an mp3, which could either cause music to start playing, or cause the music player to throw an error. Also, depending on the music player's launch mode, the launcher intent may return to an existing instance of the music player, while the mp3 intent might create a new one.
According to my understanding, it gives choice to user which intent he wants to select. In that documentation they have given that it gives selection of intents whether user wants to open app's main activity or wants to launch any diff app/activity other than user's app. This is what i understood from that documentation. Check this links for your reference : https://code.google.com/p/android/issues/detail?id=67162 & http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/content/Intent.java#Intent.setSelector%28android.content.Intent%29

Start Google Now on Music Recognition

Is there a way to automatically start Google Now on Music Recognition? I'd need it to create a DashClock extension that start Music Recognizing...
As far as I speak, I can start package com.google.android.googlequicksearchbox successfully but I cannot start voice input... I would not use startActivity for result since I would not need a result, I would really just start Google Now voice recognition feature, is there a way to do it programmatically?
Otherwise, to simply launch Google Now Song Recognition service (without having to tap on "Listen to Music", to be clear), you can create an Intent with the string
"com.google.android.googlequicksearchbox.MUSIC_SEARCH";
that would launch directly the proper interface.
I just found a way to achieve my goal:
Intent intent = new Intent();
intent.setClassName("com.google.android.googlequicksearchbox","com.google.android.googlequicksearchbox.VoiceSearchActivity");
starting this intent will bring up voice search window.

Android - how to open an audio-stream url with an external app?

I want to give the user the choice of opening an audio stream url with an audio player that he already have installed.
The following code works as so far that the user getting a list of installed audio apps and can choose between then.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(stream.getAudio()), "audio/*");
startActivity(intent);
But it will open an intent that lives inside my app only. Therefor the playback will stop if the user closes my app. How can I change the behaviour so that not an new activity is started but the whole app is launched and is detached from my app.
I guess it's because the audio player is opened in the same task stack of your app.
Try Intent.FLAG_ACTIVITY_NEW_TASK to start it as a new task, like:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
See Tasks and Back Stack to learn more.

Controlling android music player by my own application

I want to write some application to control android music player :)
For example when i shake phone -> player should change music next one. Is it possible without rooting my phone?
I think it is possible, here is a snippet for a performing playback through Intent.
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
or
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
Source: Android launching music player using intent
You have to check if there are more Intents you can use by googleing (loading a playlist, etc.).
To control any 3rd party application it first have to be willing to let you to. It is usually done by exposing public API of any sort, so other app would know how to do that. If music player of your choice offers that, then "yes" is the answer. If it does not, then usually it means "no", and rooting is not necessarily a magical solution either. If there's no API or any other communication channel exposed (even non publicly) then achieving your goal would be tricky.

Playing audio files in android

I need to play long audio files using the Android SDK. I am aware of the MediaPlayer framework shipped by Android, but I was wondering wether the built-in "Music Player" application could be invoked, so I don't have to write my own player GUI and code.
Yes, you can.
Set up an intent like this:
act=android.intent.action.VIEW dat=file:///mnt/sdcard/song_name.mp3 typ=audio/mpeg3
Action: VIEW
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri
.parse("file:///mnt/sdcard/song_name.mp3"),
"audio/mpeg3");
startActivity(intent);

Categories

Resources