我知道這個問題的千種不同版本已被詢問,並且我已經瀏覽過它們並嘗試了這些建議,但都沒有奏效。我試圖在單擊按鈕時播放聲音,同時在屏幕上顯示新文本。當我按下按鈕時,我得到文本切換,但聲音從不播放。這是我的代碼:使用Android按鈕單擊播放聲音
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
捕獲中是否發生錯誤?發佈您嘗試播放聲音時收到的printStackTrace。 – Travis
我在帖子末尾添加了我所看到的 – googlygoogly2
這是因爲創建失敗。它從來沒有達到預備狀態。試玩時你仍處於初始狀態。我問你第一次嘗試打印堆棧是什麼。 – Travis