2014-02-08 93 views
1

我知道這個問題的千種不同版本已被詢問,並且我已經瀏覽過它們並嘗試了這些建議,但都沒有奏效。我試圖在單擊按鈕時播放聲音,同時在屏幕上顯示新文本。當我按下按鈕時,我得到文本切換,但聲音從不播放。這是我的代碼:使用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 
+0

捕獲中是否發生錯誤?發佈您嘗試播放聲音時收到的printStackTrace。 – Travis

+0

我在帖子末尾添加了我所看到的 – googlygoogly2

+0

這是因爲創建失敗。它從來沒有達到預備狀態。試玩時你仍處於初始狀態。我問你第一次嘗試打印堆棧是什麼。 – Travis

回答

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(); 
      } 
     } 
    }); 


} 
+0

我試過這個,但現在我得到了我添加到我的原始帖子結尾的錯誤。 – googlygoogly2

+0

你在使用模擬器嗎? –

+0

我當時開始使用我的手機(Galaxy S4) – googlygoogly2

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。在上面的例子中,原始文件夾中的聲音文件被命名爲blop。

+0

我實際上有這樣的事情起初,因爲我把MP3文件放在一個原始文件夾中,但它仍然無法正常工作。有了這個應用程序將無法打開... – googlygoogly2

相關問題