如何点击按钮播放声音

时间:2011-06-18 07:00:17

标签: android

我试图在点击按钮时在模拟器上播放声音文件,但我收到消息“应用程序播放音频已意外停止”

我的代码是:

package com.java4u.android;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public  class p1 extends Activity {


// creating instance of media player
MediaPlayer mp=MediaPlayer.create(this,R.raw.meow);
@Override
protected void onCreate(Bundle savedInstanceState) 
{    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button btnSound=(Button)this.findViewById(R.id.playSound);

    btnSound.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        startActivity(new Intent("com.java4u.android.p1")); 
            mp.start();
        }
    }); 

        }

}

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.java4u.android"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".p1" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

main.xml中

<?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 android:text="play audio" 
    android:id="@+id/playSound" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
    </Button>
</LinearLayout>

4 个答案:

答案 0 :(得分:6)

试试这个:

Button btn1=(Button)findViewById(R.id.xBtn1);

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.raw.fruit_dance);  
        mp1.start();
    }
}

答案 1 :(得分:2)

MediaPlayer实例的创建移至onCreate方法。 这将使您的应用程序运行:

// creating instance of media player
MediaPlayer mp; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mp = MediaPlayer.create(this, R.raw.ljudman__grenade);

此外,如果您尝试在按钮单击时启动活动的新实例,我相信这是正确的方法:

startActivity(new Intent(p1.this, p1.class));

答案 2 :(得分:2)

mp.start()应该在开始新活动之前。

      btnSound.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            mp.start();
            startActivity(new Intent(currentActivityName.this, newActivityName.class)); 

            }
        }); 

答案 3 :(得分:0)

<?php 
      require('../admin/html2pdf/html2pdf.class.php');
        ob_start();
?>

 <page> 
    <page_header> 
<br>
<table>
    <tr class="headings"  style="background-color: #FFFFFF;">
             <th class="column-title">Home</th>
             <th class="column-title"><h3>web site</h3></div></th>
             <th class="column-title">Home</th>
    </tr>                              
</table>
</page_header> 
<page_footer> 
<div>
<TABLE BORDER = "1" id="table2">
    <TR style="background-color: #f2f2f2;">
        <TH COLSPAN=3 style="width: %;text-align: center;">Footer</TH>
        <TH COLSPAN=3 style="width: %;text-align: center">Copyright </TH>
        <TH COLSPAN=3 style="width: %;text-align: center">Copyright </TH>
    </TR>
    <TR>
        <TD >test</TD> <TD>test</TD><TD>test</TD> <TD>test</TD> <TD>test</TD> <TD>test</TD><TD>test</TD> <TD>test</TD> <TD>test</TD>
    </TR>

</TABLE>
</div>
</page_footer> 
</page> 

**Content :** 
<div style="" id="table1" class="content">  
<table>

    <tr class="headings"  style="background-color: #f2f2f2;">
                                    <th class="column-title" style="width: 0px">firstname</th>
                                    <th class="column-title" style="width: 80px">lastname</th>
                                    <th class="column-title" style="width: 80px">mail</th>
                                    <th class="column-title" style="width: 80px">phone</th>
                                    <th class="column-title" style="width: 80px">login</th>
                                    <th class="column-title" style="width: 80px">password</th>
                                    <th class="column-title" style="width: 80px">birthday</th>
   </tr>

                                    <tbody>


                                    <tr class="even pointer">
                                        <td class=" "><p style="">firstname1</p></td>
                                        <td class=" "><p style="">lastname1</p></td>
                                        <td class=" "><p style="">mail1</p></td>
                                        <td class=" "><p style="">phone1</p></td>
                                        <td class=" "><p style="">login1</p></td>
                                        <td class=" "><p style="">password1</p></td>
                                        <td class=" "><p style="">birthday1</p></td>
                                      </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname2</p></td>
                                        <td class=" "><p style="">lastname2</p></td>
                                        <td class=" "><p style="">mail2</p></td>
                                        <td class=" "><p style="">phone2</p></td>
                                        <td class=" "><p style="">login2</p></td>
                                        <td class=" "><p style="">password2</p></td>
                                        <td class=" "><p style="">birthday2</p></td>
                                      </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname3</p></td>
                                        <td class=" "><p style="">lastname3</p></td>
                                        <td class=" "><p style="">mail3</p></td>
                                        <td class=" "><p style="">phone3</p></td>
                                        <td class=" "><p style="">login3</p></td>
                                        <td class=" "><p style="">password3</p></td>
                                        <td class=" "><p style="">birthday3</p></td>
                                      </tr>
                             <tr class="even pointer">
                                        <td class=" "><p style="">firstname4</p></td>
                                        <td class=" "><p style="">lastname4</p></td>
                                        <td class=" "><p style="">mail4</p></td>
                                        <td class=" "><p style="">phone4</p></td>
                                        <td class=" "><p style="">login4</p></td>
                                        <td class=" "><p style="">password4</p></td>
                                        <td class=" "><p style="">birthday4</p></td>
                                      </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname5</p></td>
                                        <td class=" "><p style="">lastname5</p></td>
                                        <td class=" "><p style="">mail5</p></td>
                                        <td class=" "><p style="">phone5</p></td>
                                        <td class=" "><p style="">login5</p></td>
                                        <td class=" "><p style="">password5</p></td>
                                        <td class=" "><p style="">birthday5</p></td>
                                      </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname6</p></td>
                                        <td class=" "><p style="">lastname6</p></td>
                                        <td class=" "><p style="">mail6</p></td>
                                        <td class=" "><p style="">phone6</p></td>
                                        <td class=" "><p style="">login6</p></td>
                                        <td class=" "><p style="">password6</p></td>
                                        <td class=" "><p style="">birthday6</p></td>
                                      </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname7</p></td>
                                        <td class=" "><p style="">lastname7</p></td>
                                        <td class=" "><p style="">mail7</p></td>
                                        <td class=" "><p style="">phone7</p></td>
                                        <td class=" "><p style="">password7</p></td>
                                        <td class=" "><p style="">password7</p></td>
                                        <td class=" "><p style="">birthday7</p></td>
                                      </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname8</p></td>
                                        <td class=" "><p style="">lastname8</p></td>
                                        <td class=" "><p style="">mail8</p></td>
                                        <td class=" "><p style="">phone8</p></td>
                                        <td class=" "><p style="">login8</p></td>
                                        <td class=" "><p style="">password8</p></td>
                                        <td class=" "><p style="">birthday8</p></td>
                                      </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname9</p></td>
                                        <td class=" "><p style="">lastname9</p></td>
                                        <td class=" "><p style="">mail9</p></td>
                                        <td class=" "><p style="">phone9</p></td>
                                        <td class=" "><p style="">login9</p></td>
                                        <td class=" "><p style="">password9</p></td>
                                        <td class=" "><p style="">birthday9</p></td>
                              </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname10</p></td>
                                        <td class=" "><p style="">lastname10</p></td>
                                        <td class=" "><p style="">mail10</p></td>
                                        <td class=" "><p style="">phone10</p></td>
                                        <td class=" "><p style="">login10</p></td>
                                        <td class=" "><p style="">password10</p></td>
                                        <td class=" "><p style="">birthday10</p></td>
                              </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname11</p></td>
                                        <td class=" "><p style="">lastname11</p></td>
                                        <td class=" "><p style="">mail11</p></td>
                                        <td class=" "><p style="">phone11</p></td>
                                        <td class=" "><p style="">login11</p></td>
                                        <td class=" "><p style="">password11</p></td>
                                        <td class=" "><p style="">birthday11</p></td>
                              </tr>
                              <tr class="even pointer">
                                        <td class=" "><p style="">firstname12</p></td>
                                        <td class=" "><p style="">lastname12</p></td>
                                        <td class=" "><p style="">mail12</p></td>
                                        <td class=" "><p style="">phone12</p></td>
                                        <td class=" "><p style="">login12</p></td>
                                        <td class=" "><p style="">password12</p></td>
                                        <td class=" "><p style="">birthday12</p></td>
                              </tr>

                             <tr class="even pointer">
                                        <td class=" "><p style="">firstname13</p></td>
                                        <td class=" "><p style="">lastname13</p></td>
                                        <td class=" "><p style="">mail13</p></td>
                                        <td class=" "><p style="">phone13</p></td>
                                        <td class=" "><p style="">login3</p></td>
                                        <td class=" "><p style="">password13</p></td>
                                        <td class=" "><p style="">birthday13</p></td>
                              </tr>








                                </tbody>
</table>
</div>

<?php
    $content = ob_get_clean(); // récupère tout le contenu depuis ob_start()
    // ou $content = ob_get_clean() ça fonctionne également;
    $pdf = new HTML2PDF('P', 'A4', 'fr');
    $pdf->pdf->SetDisplayMode('fullpage');
    $pdf->writeHTML($content);
     ob_clean();
    $pdf->Output('test.pdf');
?>