Android MediaPlayer - Audio
There are two distinct frameworks for recording and playing audio.The choice on which to use depends on the application:- MediaPlayer/MediaRecorder—This is the standard method to manipulate audio, but must be file- or stream-based data. Creates its own thread for processing. SoundPool utilizes this framework.
- AudioTrack/AudioRecorder—Provides direct access to raw audio. Useful to manipulate audio in memory, write to the buffer while already playing, or any other usage that does not require a file or stream. It does not create its own thread for processing.
Choosing and Playing Back Audio Files
The MediaRecorder and MediaPlayer classes are used to record and play back either audio or video.This recipe focuses on audio, and the usage is straightforward. For playback,
the steps are
1. Create an instance of the MediaPlayer:
MediaPlayer m_mediaPlayer = new MediaPlayer();
2. Specify the source of media. It can be created from a raw resource:
m_mediaPlayer = MediaPlayer.create(this, R.raw.my_music);
Another option is to set as a file from the filesystem (which then also needs a prepare statement):
m_mediaPlayer.setDataSource(path);
m_mediaPlayer.prepare();
In any case, these statements need to be surrounded by a try-catch block because the specified resource might not exist.
3. Start playback of the audio:
m_mediaPlayer.start();
4. When the playback is done, stop the MediaPlayer and release the instance to free up resources:
m_mediaPlayer.stop();
m_mediaPlayer.release();
MainActivity.java Code
package
android.example.mediaplayer;
import
android.os.Bundle;
import
android.app.Activity;
import
android.content.Intent;
import android.view.Menu;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class
MainActivity extends Activity {
private Button mlocalaudio;
private Button mresourcesaudio;
private Button mstreamaudio;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlocalaudio = (Button)
findViewById(R.id.localaudio);
mlocalaudio.setOnClickListener(mLocalAudioListener);
mresourcesaudio = (Button)
findViewById(R.id.resourcesaudio);
mresourcesaudio.setOnClickListener(mResourcesAudioListener);
}
private
OnClickListener mLocalAudioListener = new
OnClickListener() {
public void
onClick(View v) {
Intent intent =
new
Intent(MainActivity.this.getApplication(),
MediaPlayerDemo_Audio.class);
intent.putExtra(MEDIA, LOCAL_AUDIO);
startActivity(intent);
}
};
private
OnClickListener mResourcesAudioListener = new
OnClickListener() {
public void
onClick(View v) {
Intent intent =
new
Intent(MainActivity.this.getApplication(),
MediaPlayerDemo_Audio.class);
intent.putExtra(MEDIA, RESOURCES_AUDIO);
startActivity(intent);
}
};
}
MediaPlayerDemo_Audio.java Code
package
android.example.mediaplayer;
import
android.app.Activity;
import
android.media.MediaPlayer;
import
android.os.Bundle;
import
android.util.Log;
import
android.widget.TextView;
import
android.widget.Toast;
public class
MediaPlayerDemo_Audio extends Activity {
private static final String TAG = "MediaPlayerDemo";
private MediaPlayer
mMediaPlayer;
private static final String MEDIA = "media";
private static final int LOCAL_AUDIO = 1;
private static final int STREAM_AUDIO = 2;
private static final int RESOURCES_AUDIO = 3;
private String path;
private TextView tx;
@Override
public void
onCreate(Bundle icicle) {
super.onCreate(icicle);
tx = new TextView(this);
setContentView(tx);
Bundle extras =
getIntent().getExtras();
playAudio(extras.getInt(MEDIA));
}
private void
playAudio(Integer media) {
try {
switch (media) {
case LOCAL_AUDIO:
/**
* TODO: Set the
path variable to a local audio file path.
*/
path = "/sdcard/My
Music/test.mp3";
if (path == "") {
// Tell
the user to provide an audio file URL.
Toast
.makeText(
MediaPlayerDemo_Audio.this,
"Please edit MediaPlayer_Audio
Activity, "
+ "and set the path variable to your audio file path."
+ " Your audio file must be stored on sdcard.",
Toast.LENGTH_LONG).show();
}
mMediaPlayer = new
MediaPlayer();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
break;
case RESOURCES_AUDIO:
/**
* TODO: Upload a
audio file to res/raw folder and provide
* its resid in
MediaPlayer.create() method.
*/
mMediaPlayer =
MediaPlayer.create(this, R.raw.music);
mMediaPlayer.start();
}
tx.setText("Playing
audio...");
} catch (Exception
e) {
Log.e(TAG, "error:
" + e.getMessage(), e);
}
}
@Override
protected void onDestroy()
{
super.onDestroy();
// TODO
Auto-generated method stub
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
}
activity_main.xml Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FF99FF"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/localaudio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="40dp"
android:text="Play Audio
from Local File" />
<Button
android:id="@+id/resourcesaudio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="26dp"
android:layout_marginTop="20dp"
android:text="Play Audio
from Resources" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:src="@drawable/android_logo"
/>
</LinearLayout>
Download Android Media Player Example code
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android สอนเขียนโปรแกรม Android
http://androidcontrol.blogspot.com/2012/01/beginning-android-training-android.html
ติดต่อ amphancm@gmail.com
ติดต่อ amphancm@gmail.com
ไม่มีความคิดเห็น:
ไม่อนุญาตให้มีความคิดเห็นใหม่