使用Android点击按钮播放声音

时间:2014-02-08 20:26:27

标签: java android button android-mediaplayer

我知道这个问题的一千个不同版本已经被问过了,我已经浏览过它们并尝试了这些建议,但没有一个有效。我正在尝试在单击按钮时播放声音,同时还在屏幕上显示新文本。当我按下按钮时,我得到要切换的文本,但声音永远不会播放。这是我的代码:

public class MainActivity extends Activity {

Button button;
TextView text;
final MediaPlayer mp = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);
    text = (TextView) findViewById(R.id.answer);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                AssetFileDescriptor afd;
                afd = getAssets().openFd("myaudio.mp3");
                mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                mp.prepare();
                mp.start();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            toggleString(v);
        }
    });
}

public void toggleString(View v) {
    if(text.getText().toString().equals(getString(R.string.nothing)))
        text.setText(R.string.answer);
    else
        text.setText(R.string.nothing);
}

这是我的活动xml文件:

<TextView
    android:id="@+id/answer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/nothing"
    android:textSize="40sp"
    android:layout_marginTop="40dp"
    />

<Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="130dp"
    android:onClick="onClick"
    android:text="@string/question" 
    />

如果有人可以提供帮助,那就太好了。提前谢谢。

编辑:这是我在尝试第二个建议后在控制台中看到的(这是打印堆栈吗?对不起,我不知道那是什么)

   [2014-02-08 15:17:14 - ddmlib] An established connection was aborted by the software in your host machine
java.io.IOException: An established connection was aborted by the software in your host machine
    at sun.nio.ch.SocketDispatcher.write0(Native Method)
    at sun.nio.ch.SocketDispatcher.write(Unknown Source)
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
    at sun.nio.ch.IOUtil.write(Unknown Source)
    at sun.nio.ch.SocketChannelImpl.write(Unknown Source)
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
    at com.android.ddmlib.Client.sendAndConsume(Client.java:642)
    at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348)
    at com.android.ddmlib.Client.requestAllocationStatus(Client.java:488)
    at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:835)
    at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:803)
    at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:763)
    at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:652)
    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:44)
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:580)

[2014-02-08 15:19:44 - ddms] Can't bind to local 8600 for debugger
[2014-02-08 15:19:44 - ddms] Can't bind to local 8602 for debugger
[2014-02-08 15:19:44 - ddms] Can't bind to local 8603 for debugger
[2014-02-08 15:19:44 - ddms] Can't bind to local 8604 for debugger
[2014-02-08 15:19:44 - ddms] Can't bind to local 8605 for debugger
[2014-02-08 15:19:44 - ddms] Can't bind to local 8606 for debugger
[2014-02-08 15:19:44 - ddms] Can't bind to local 8607 for debugger
[2014-02-08 15:19:44 - ddms] Can't bind to local 8608 for debugger

2 个答案:

答案 0 :(得分:3)

您必须设置数据源并准备一次媒体播放器。

试试这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);
    text = (TextView) findViewById(R.id.answer);

    AssetFileDescriptor afd;
    try {
        afd = getAssets().openFd("myaudio.mp3");
        mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
        mp.prepare();
    } catch (IOException e1) {
        e1.printStackTrace();
    }


     button.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
        try {     
            mp.start();

        } catch (IllegalStateException e) {
            e.printStackTrace();
            }
        }
    });


}

答案 1 :(得分:1)

#import android.media.MediaPlayer;

public class MainActivity extends Activity {

MediaPlayer mySound;
@override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
mySound = MediaPlayer.create(MainActivity.this, R.raw.blop);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        try {
            mySound.start();
        } catch (NullPointerException e) {
            mySound = MediaPlayer.create(MainActivity.this, R.raw.blop);
            /* Optional */ // mySound.start();
        }
        toggleString(v);
    }
});
}

尝试将声音文件保存在原始文件夹中,并在活动中调用MediaPlayer.create,而不是资源。在上面的示例中,raw文件夹中的声音文件名为blop。