Next Tutorial
Making a Video Player
Prev Page
View All Tutorials
Android Tutorial
Coding a Media Player With Audio
If you are just arriving at this page; and you are not familiar with
creating Android Apps,we suggest you begin with our
tutorials introduction:
About Our Tutorials.
From there follow along with each tutorial lesson.
COPY/PASTE CODE
First,
create a java/xml android template appusing AIDE, (or Android Studio).
Then you can get the code for this app from the
EXAMPLE CODE section on this page.
Replace the code on the pages you need to(with our tutorial example code); in order to create this app.
Sometimes, new pages have to be created.
All the other coded pages in your app
can remain as they are; they don't have to be changed.
This is the App
Media Player Audio
It can play audio files.
There are two buttons; one for play and one for pause.
How To Use Media Player Code Class
Media Player is a code class for adding media to your android apps. You can add audio, video, or streaming content as well. And, many features can be added to a
media player. Some common ones are buttons: Play, Stop, Rewind, Fast-Forward, Pause, Seek, Search.
Additional reading about Media Player code class at android developer website.
Media Player Android and, media formats;
Media Formats Android
For this tutorial app, we will
create a media player to play an audio file.
A media player can be added to any page in your app. You just have to add the code that defines the media player class and then define and add your media file(s)
to your res/raw folder in your app project.
The
raw folder is where all media type files are added for the app project. If one is not already created, then you must create the folder. This is easy to do in the AIDE coding editor.
Typically, the media player code is added to the MainActivity.java file(the java class file) that allows the media player to be created(Create method) and play the media file; in this case an audio file.
And, if your adding buttons as we are for this media player; those are defined in the the layout file (main.xml) of your app. We have two buttons; one for
pause and one for play; so we will code two button elements which will hold each of the buttons code and, whatever text you want to add to the buttons.
Then, in the java class (MainActivity.java)
we will add an
onclicklistener for each button. The onclicklistener is what allows the button to be clicked and the audio to be played.
We also added a
text message(toast message) to each button; so that when it is clicked on a message populates on the app screen indicating that the file is Playing or is Paused.
This code is also added to the java class file for the app.
Creating - Naming Your App Using AIDE
When creating your app,(from left menu) choose:
Create New Project;
Then, choose
New Android App(gradle, android Java/xml)
or
Hello World App Java/xml
if your using the original version of AIDE
Use the name as shown in our example code;
MediaPlayerAudio for the App Name and,
for the package name(next line) use:
com.aac.mediaplayeraudioexample;
After your app is created you can change the app name. To do so, highlight the app name in the App Project file hierachy, and choose
Rename. Then, in your strings.xml at app_name, change the text to the new name.
FYI:
You cannot change the app package name(.apk). It remains the same thru the lifecycle of the app. However, you can change the
'app version code', which is typically changed incrementally with any new updates to your app.
So, example, first app build is usually 1; subsequent build 1.1; and so forth. You can choose whatever 'numbering' you want; it just needs to increment from the previous version. You have to change it in the 'build.gradle' in your app project with each new build.
For app stores, like Google, Amazon;
this is moreso important when you are submitting an app update to a previously submitted app.
In AIDE, for newly created apps, the version code is at 1.
There is also a
version name in the build.gradle for your app project. This name which is at 1.0 for newly created apps, can be changed to numbers or letters up to 50 characters.
Usually the version name is used to characterize an app or not changed from the default number.
Version name is shown in your app profile at most app stores; the version code is not shown. It is used only to specify various app builds.
The Coded Pages
Code for the app - Media Player Audio
For this app you will use these coded pages:
1 layout file, main.xml,(already created, just copy paste this tutorial app code into it and save); located at res/layout/.
1 java page, MainActivity.java,(already created) where we define our layout:
setContentView(R.layout.main); and add the Create method for the Media Player.
Just copy paste the code from this tutorial, into the file MainActivity.java; to replace code that's there and save the file.
1 audio file - sample_audio.wav; You can download it from our Example Code section; then add it to your res/raw folder. You will
have to
create the raw folder. To create the folder; goto
src/main/res folder in the app name; then right side Select dots, then Select; create new folder, type raw, Select; ok. Folder is now created.
Use your tablets File Manager to copy paste the audio file into the AppProjects/MediaPlayerAudio/app/src/main/res/raw/ folder.
1 strings file, strings.xml (already created) - where we define the text we put in the app; and has
app name already added. Just copy paste code from our Example Code section to replace
code that is in the file now and save the page.
1 manifest file, AndroidManifest.xml (already created) - where we define the app's name, launcher icon, and the app's theme. And, where
we define our app's activity page; MainActivity.java; and also any new java pages we create. This must be defined in the Activity element in the manifest file.
This page MainActivity, is created automatically each time you create a new Android Java/xml app. The version code, version name, launcher icon, app theme, app name attributes are all added
for you when you use an Android Template Java/xml app to create your app project.
And, you don't need to change code on this page unless:
your adding additional activity pages
to your app;
need to add Internet permission,
want to change the App's Theme;
or want to add a custom launcher icon
These are some of the most common elements that are coded in the manifest file.
For this app tutorial, MediaPlayerAudio, we don't need to add any additional code to this manifest file; so we leave it as it is.
FYI:
If you want to add your own audio wav or audio mp3 file to this app you can; just add it to the res/raw folder in your app project. If you are using spacing in your
audio file name you must use the underscore symbol; like sample_audio.wav. All letters must be lowercase.