Can't play audio


Last Updated:

  1. kevindroidX

    kevindroidX New Member This Topic's Starter

    Joined:
    Aug 7, 2010
    Messages:
    2
    Likes Received:
    0
    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!
     

    Advertisement
  2. JiMMaR

    JiMMaR Well-Known Member

    Joined:
    Jun 12, 2010
    Messages:
    363
    Likes Received:
    57
    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 New Member This Topic's Starter

    Joined:
    Aug 7, 2010
    Messages:
    2
    Likes Received:
    0
    that worked! thanks :)
     

Share This Page

Loading...