2011-06-27 193 views
0

新的android dev有希望。我一直在试图实现一个应用程序,按住“确定”按钮,它会播放默认的铃声循环。Android App按住按钮播放声音

我有什么到目前为止

package com.tick.helloAndroid; 

import net.technologichron.android.control.NumberPicker; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Bundle; 
//import android.widget.TextView; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.Button; 
import android.widget.Toast; 


public class main extends Activity implements OnTouchListener { 
    private int REQUEST_ENABLE_BT = 1; 
    private MediaPlayer mp; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Set up the window layout 
     setContentView(R.layout.main); 

     final Button zero = (Button) this.findViewById(R.id.ok); 
     zero.setOnTouchListener(this); 
     Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); 
     mp.setDataSource(this, alert); 

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

From doing some searching on SO, this seems to work for some people,然而,行

mp.setDataSource(this, alert); 

似乎不起作用。如果不工作,我的意思是,它迫使一个“未处理的异常错误”,当我尝试在Android模拟器运行应用程序,它在做一个try-catch语句,

try { 
     mp.setDataSource(this, alert); 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     Toast.makeText(this, "args", Toast.LENGTH_LONG).show(); 
     finish(); 
     return; 
    } catch (SecurityException e) { 
     // TODO Auto-generated catch block 
     Toast.makeText(this, "sec", Toast.LENGTH_LONG).show(); 
     finish(); 
     return; 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     Toast.makeText(this, "illega", Toast.LENGTH_LONG).show(); 
     finish(); 
     return; 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     Toast.makeText(this, "io", Toast.LENGTH_LONG).show(); 
     finish(); 
     return; 
    } 

没有任何错误,但崩溃编译,它不打印任何上述渔获物的字符串。

任何关于我做错了什么的想法和想法将不胜感激。

编辑:可能是因为em没有铃声这个事实吗?

+0

什么是例外?请记住:try/catch是运行时错误。 – ethrbunny

+0

未处理的异常错误,当您为大量可能的异常执行一系列catch语句时,该应用程序会编译好的错误。但是运行时,它只会崩溃 –

回答

0

忘了实例化mp .... haha​​hahha