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!
     



    1. Download the Forums for Android™ app!


      Download

       
  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...
Similar Threads - Can't play audio
  1. SquiddyKat
    Replies:
    1
    Views:
    149
  2. Savage3105
    Replies:
    7
    Views:
    520
  3. Drchiefd113
    Replies:
    5
    Views:
    428
  4. DanCalling
    Replies:
    11
    Views:
    392
  5. 2heaVens
    Replies:
    0
    Views:
    547
  6. startac4
    Replies:
    5
    Views:
    642
  7. James_Watson
    Replies:
    10
    Views:
    725
  8. Queenie AM
    Replies:
    5
    Views:
    1,376
  9. manny2308
    Replies:
    10
    Views:
    696
  10. OsakaWebbie
    Replies:
    5
    Views:
    772

Share This Page

Loading...