2011-06-27 234 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