2011-04-21 92 views
0

我是Android开发人员的新手,并且遇到了让我的代码实现并实际播放声音文件的问题。我怀疑这可能是由于我的代码中存在问题,但是我已经查看了很多,并且我不知道是什么导致了这个问题。 谢谢,安妮使用Android应用程序开发播放声音文件的问题

package com.mediaplayer; 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.Button; 
import android.R.raw; 
public class PlayPlayer extends Activity implements OnTouchListener { 

    private MediaPlayer mp; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final Button button1 = (Button) this.findViewById(android.R.id.button1); 
     button1.setOnTouchListener(this); 
      mp = MediaPlayer.create(this, R.raw.music); 
      mp.start(); 

    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 

     switch (event.getAction()) { 

     case MotionEvent.ACTION_DOWN: 
      mp.setLooping(true); 
      mp.start(); 

     case MotionEvent.ACTION_UP: 
      mp.pause(); 
     } 

     return true; 
    } 

} 

回答

0

虽然我从来没有在Android中使用的音频,并假设资源是正确的(R.raw.music),您必须在交换机的一个问题(没有休息),它应该看起来像这个:

switch (event.getAction()) { 

    case MotionEvent.ACTION_DOWN: 
     mp.setLooping(true); 
     mp.start(); 
     break; 

    case MotionEvent.ACTION_UP: 
     mp.pause(); 
     break; 
    } 
+0

感谢您的帮助,但我试过了,它仍然不播放声音文件。我想知道是否可能是由于我使用触摸屏功能而不是点击功能,尽管我的触摸屏模拟器软件包安装在我的AVD上。 – Anne 2011-04-21 00:47:22

+0

你可以使用调试,看是否调用onTouch方法 – 2011-04-27 13:53:40

相关问题