1. Are you ready for the Galaxy S20? Here is everything we know so far!

Can't play audio

Discussion in 'Android Development' started by kevindroidX, Aug 7, 2010.

  1. kevindroidX

    kevindroidX Lurker
    Thread Starter

    Hi,

    My layout shows up correctly, which I borrowed from the Android Developer's site, but the audio won't play. Perhaps I'm using the wrong context input to MediaCenter.create( ... ) ?

    Here's my SpellThis class and main.xml is below

    Code (Text):
    1.  
    2. public class SpellThis extends Activity implements View.OnClickListener {
    3.     public void onCreate(Bundle savedInstanceState) {
    4.         Button submitButton, replayButton;
    5.         super.onCreate(savedInstanceState);
    6.         setContentView(R.layout.main);
    7.        
    8.         submitButton = (Button)findViewById(R.id.submitButton);
    9.         replayButton = (Button)findViewById(R.id.replayButton);
    10.         MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.mitochondria); // raw/mitochondria.mp3 is a working mp3
    11.         mp.start();
    12.    }
    13. }
    main.xml

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent">
    6.     <TextView
    7.         android:id="@+id/label"
    8.         android:layout_width="fill_parent"
    9.         android:layout_height="wrap_content"
    10.         android:text="Type here:"/>
    11.     <EditText
    12.         android:id="@+id/entry"
    13.         android:layout_width="fill_parent"
    14.         android:layout_height="wrap_content"
    15.         android:background="@android:drawable/editbox_background"
    16.         android:layout_below="@id/label"/>
    17.     <Button
    18.         android:id="@+id/submitButton"
    19.         android:layout_width="wrap_content"
    20.         android:layout_height="wrap_content"
    21.         android:layout_below="@id/entry"
    22.         android:layout_alignParentRight="true"
    23.         android:layout_marginLeft="10dip"
    24.         android:text="Submit" />
    25.     <Button
    26.         android:id="@+id/replayButton"
    27.         android:layout_width="wrap_content"
    28.         android:layout_height="wrap_content"
    29.         android:layout_toLeftOf="@id/submitButton"
    30.         android:layout_alignTop="@id/submitButton"
    31.         android:text="Replay sound" />
    32. </RelativeLayout>
    33.  
    Thanks!
     


  2. JiMMaR

    JiMMaR Android Enthusiast

    I use "this" instead of "getBaseContext"

    so it'll become
    Code (Text):
    1.  
    2. MediaPlayer mp = MediaPlayer.create(this, R.raw.mitochondria);
    3.  
    try it and tell me
     
    kevindroidX likes this.
  3. kevindroidX

    kevindroidX Lurker
    Thread Starter

    that worked! thanks :)
     
Loading...

Share This Page

Loading...