tags:

views:

75

answers:

2

Hi there,

i think a simple question but i did not find anything about doing it right.

I want to start a mediaplayer from my app and send that player a file to play(stream).

Would be nice to automatically choose the player associated with the mime type of the file i process to the player.

The only way to start an app is this one. But i wonder if there is a android native way.

Runtime r = Runtime.getRuntime();

    try {
        if(child != null) {
            child.destroy();
            child = null;
        }
        child = r.exec("player");
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
    }

thanks

+3  A: 

Please do not use the code you have listed above on Android.

You will need to create an ACTION_VIEW Intent describing the path to the file, along with its MIME type. Then, call startActivity() on that Intent. With luck, there will be an application on the device capable of playing that file.

CommonsWare
thx CommonsWare, i will try to figure out how to use an intent. Is it possible to start a stream too? I mean, is it possible to pass a stream that way? Is this the only way to start an application? Is there no direct way like in my example?
Andy
hey, its me again. Based on your information i found this example with google: http://markmail.org/message/pfwpyaf7gkaqzdph#query:android%20intent%20ACTION_VIEW%20example+page:1+mid:pfwpyaf7gkaqzdph+state:results So your answer was very helpful to me. thx :)
Andy
I have an other question. I'm trying to start a special video player by using an intent. I set the mime type and the videourl like this: intent.setDataAndType(Uri.parse(Videourl), "*/*"); Now Android asks me what program to choose to play this stream. But it don't asks me for the exact app it asks for the mime type like "video". How can i a) change the mimetyps in android to start the right app when choosing "video" or b) how can i start start an app directly without using the mime type. thx P.S.: I'm using android emulator with android 2.1
Andy
"change the mimetyps in android to start the right app when choosing "video"" -- you can't change Android. You can only change your app. If you don't like the intent filter on your app, change it. "how can i start start an app directly without using the mime type" -- generally, you don't, unless it is an activity in your own app. Then, you can use the Intent constructor that takes a class as a parameter.
CommonsWare
"you can't change Android. You can only change your app. If you don't like the intent filter on your app, change it." -- I had be more clear on my question, i think. I mean, whats the android setting (in the gui not in the code) to change the default application that is chosen when i call the mime type e.g. "video/*". My problem is, i have installed a video player that can handle the format i want to play but it always gets started with android album. "generally, you don't" -- ok, that sucks.. :/
Andy
"whats the android setting (in the gui not in the code) to change the default application that is chosen when i call the mime type" -- there is no strict notion of "default application" in most cases. If there is more than one candidate, and if the launching application uses Intent.createChooser(), the user will get a list, and sometimes that list includes a checkbox to set the default. I don't see a spot in the Settings app to change that more directly. So, if you are trying to launch a player, wrap your Intent with a call to Intent.createChooser() before calling startActivity() on it.
CommonsWare
ok, thx again, i tried it but i still get only the chance to choose the type (like video), there is no chance to choose what app to use. I will wait now until i get my real android device. maybe its a emulator problem. Hopefully the nexus one is available in germany soon. ;)
Andy
A: 

Launching other applications in Android is a bit weird, at least in my opinion. You generally do that by creating an Intent object and passing it to Context.startActivity(). Depending on what you know about the other app etc. you can specify a class that is to be launched or let Android determine what to run for you by providing some other, let's say "less concrete" information.

You probably want to read the Developer guide on Intents and Intent filters and also the documentation of the class Intent , especially the explanation of explicit and implicit Intents.

Robert Petermeier
thx to you too. Pretty much the same like CommonsWare said.
Andy