Saturday, December 10, 2011
Play Audio File in Android :
# We have to create an application in which when we click on "play" button , audio file should be play :
package com.myApp.ListeningSong;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class ListeningSongActivity extends Activity
{
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.playSound);
b.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.kunfaya);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
}
return true;
}
});
}
}
// Source Code of main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/playSound"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:background="@drawable/btnscreen"/>
<MediaController
android:id="@raw/kunfaya"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Output :
Note : Uploading and installation of .apk will be depend on audio file size , it may be take too much time so don't irritate, just wait.
# We have to create an application in which when we click on "play" button , audio file should be play :
- Create a new project named as "ListeningSong" and class name will be such as "ListeningSongActivity.java".
- Pick an audio file.
- Create a new folder named as "raw" under res directory.Paste audio file under this raw folder.
- Pick an image icon file named as "btnscreen.png". Paste this file under drawable folders.
package com.myApp.ListeningSong;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class ListeningSongActivity extends Activity
{
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.playSound);
b.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.kunfaya);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
}
return true;
}
});
}
}
// Source Code of main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/playSound"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center"
android:background="@drawable/btnscreen"/>
<MediaController
android:id="@raw/kunfaya"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Output :
After Clicking on Play Button , Song plays |
Note : Uploading and installation of .apk will be depend on audio file size , it may be take too much time so don't irritate, just wait.
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →
Related Posts:
Subscribe to:
Post Comments (Atom)
0 comments: