Android App按住按钮时播放声音

时间:2011-06-27 14:10:31

标签: android android-emulator

新的android开发者在这里充满希望。我一直试图实现一个应用程序,按住“确定”按钮后,它将循环播放默认铃声。

到目前为止我有什么

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

似乎行不通。通过不工作,我的意思是它强制一个“未处理的异常错误”,在执行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;
    }

编译没有任何错误,但当我尝试在Android模拟器上运行应用程序时崩溃,它不会打印任何上述捕获的字符串。

对于我做错了什么的任何想法和想法都将非常感激。

编辑:可能是因为鸸is没有铃声吗?

1 个答案:

答案 0 :(得分:0)

忘了实例化mp ....哈哈哈哈