[VIDEO-Tut] - Playing Media(mp3) on the emulator
What you learn: You will learn how playback media(mp3) on the emulator, how to add audio-files as raw-resources and where to set "-useaudio".
Difficulty: 1 of 5

What it will look like:
The Full Source:
Do not forget: /res/raw/everlast.mp3 (or a similar name; How to --> see the screencast!)
/res/layout/main.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button id="@+id/cmd_play"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Play the music !!!"
- />
- </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
/src/your_package_structure/MusicPlayer.java
Using java Syntax Highlighting
- package org.anddev.android.musicplayer;
- import android.app.Activity;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MusicPlayer extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.main);
- // Find the Button from the xml-file.
- Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
- cmd_play.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- MediaPlayer mp = MediaPlayer.create(MusicPlayer.this,
- R.raw.everlast);
- mp.prepare();
- mp.start();
- // i.e. react on the end of the music-file:
- mp.setOnCompletionListener(new OnCompletionListener(){
- // @Override
- public void onCompletion(MediaPlayer arg0) {
- // File has ended !!! <img src="http://www.anddev.org/images/smilies/wink.png" alt=";)" title="Wink" />
- }
- });
- }
- });
- }
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Regards,
plusminus







>2s lag - seems like I'll have to investigate that....



