top of page

Android - Creating Intents: Media

Hello all,

I really enjoyed the Udacity Android Basics course by Google lesson Practice with Intents so I am taking some time to fully explore each of the intents in the Android Common Intents documentation. The one I have most recently looked at is Media (Music or Video).

First I just created a simple button to activate the intent. The app looks like this at first:

Android App screen showing a single PLAY MEDIA button.

And the code in activity_main.xml looks like this:

<Button

android:id="@+id/media"

android:layout_width="@dimen/button_width"

android:layout_height="@dimen/button_height"

android:layout_gravity="center"

android:layout_margin="@dimen/margin"

android:onClick="playMedia"

android:text="Play Media"/>

Then I created an activity in MainActivity.java to call the intent when the button is clicked. To launch a media app it would seem you need slightly different code to that shown in Media (Music or Video).

public void playMedia(View view) {

File musicFile2Play = new File("/storage/external_SD/media/Silverchair/Frogstomp/02 Tomorrow.mp3");

Intent intent = new Intent();

intent.setAction(android.content.Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(musicFile2Play), "audio/mp3");

if (intent.resolveActivity(getPackageManager()) != null) {

startActivity(intent);

}

}

The first line, public void playMedia(View view), links this object to the onClick event ofthe button I created in the activity_main.xml document.

The second line, File musicFile2Play = new File("/storage/external_SD/media/Silverchair/Frogstomp/02 Tomorrow.mp3"); takes the file pathway and name of a media item (in this example a song on my phone) and saves in in a variable “musicFile2Play”. To find out the file pathway and name of a particular song or video on your phone you need to…

  1. Go to your File Manager app (or similar)

Apps screen showing where "File Manager" is often located.

  1. Go to where your file is located (in my case Music)

Contents of the File Managed showing where key files are stored.  In this example the focus is on Music.

  1. Scroll through your folders until you find the file you want…

Example list of music folders.

  1. Press and hold on the icon for that particular file…

List of song files from a particular album.

  1. Until this window pops up, and then select “Details”…

Sube menu that pops up when you press and hold on a media file (here a music file).

  1. What you want is the Location followed by a forward slash “/” and then the name of the file.

Details of a media file including the name, size, location, lenght, artists, album and the date and time it was last modified.

  1. That gets you /storage/external_SD/media/Silverchair/Frogstomp/02 Tomorrow.mp3 which you then insert within parens and quotes after new File.

The third line Intent intent = new Intent(); creates a new intent. Note, you can rename “intent” with a lower-case i to anything you want, using usual naming conventions.

The fourth line intent.setAction(android.content.Intent.ACTION_VIEW); sets the action of the intent to a view.

The fifth line intent.setDataAndType(Uri.fromFile(musicFile2Play), "audio/mp3"); is quite different from the example code on Media (Music or Video)1. Firstly we are setting the Data and Type because to make this intent work you need to send the Uri and also establish the MIMEtype. The Uri is the media file we stored in the variable `musicFile2Play’. The MIMEtype is audio/mp3.

The final two lines:

if (intent.resolveActivity(getPackageManager()) != null) {

startActivity(intent);

starts the intent as long as there is an app on the users phone.

There is a LOT more you can do with this intent including playing a video file, playing a file from the internet or playing a file or group of files according to artist or album etc.

Please feel free to question, correct or further explain anything I have covered here. I am by no means an expert on this topic and would love anything that will help me learn more.

Links:


Featured Posts
Recent Posts
Archive
Search By Tags
Follow Us
  • LinkedIn Social Icon
  • Pinterest Social Icon
  • Facebook Basic Square

© 2023 by Coming Soon

Proudly created with Wix.com

bottom of page