[转]android 播放铃声

本文转自:http://blog.csdn.net/hu48934/article/details/6920773

 

package com.hu;

import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class ShengYinActivity extends Activity {

MyThread m;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.button1);
m = new MyThread(this);
m.start();
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(m.isAlive()){
m.StopAlarmRing();
}
}
});
}


class MyThread extends Thread{
MediaPlayer mMediaPlayer;
Context mContext;



public MyThread(Context mContext) {
mMediaPlayer = new MediaPlayer();
this.mContext = mContext;
}




@Override
public void run() {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
try {

mMediaPlayer.setDataSource(mContext, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.start();
}


private void StopAlarmRing() {
mMediaPlayer.stop();
}

}


}

 

posted on 2012-01-19 13:45  freeliver54  阅读(2543)  评论(0编辑  收藏  举报

导航