因此在onClick()方法中生成并播放媒体文件。再次单击,随机数生成器生成另一个媒体文件,无论最后一个是否已完成播放。如果快速点击,它们将重叠。但目前我并不关心这一点。如何避免此错误?
mp3.release();
mp3.stop();
mp3 = null;
^^不确定这是否是正确的顺序,但这也给了我另一个错误,我不确定在哪里放置它。
onCompletion(){}
可能会工作。有人有什么建议吗?
package com.example.okunato.chance;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import java.util.Random;
import pl.droidsonroids.gif.GifImageView;
public class MainActivity extends AppCompatActivity {
MediaPlayer introSong;
MediaPlayer introVoice;
MediaPlayer mp3;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
introSong = MediaPlayer.create(this, R.raw.theme); // Backround music
introVoice = MediaPlayer.create(this, R.raw.intro); // the intro that plays upon open
final int mediaLengthSong = introSong.getCurrentPosition();
final int mediaLengthVoice = introVoice.getCurrentPosition();
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
introVoice.seekTo(mediaLengthVoice);
introSong.seekTo(mediaLengthSong);
introSong.pause();
introVoice.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
introVoice.seekTo(mediaLengthVoice);
introSong.seekTo(mediaLengthSong);
introSong.pause();
introVoice.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
final int MAX_VOLUME = 100;
int soundVolume = 65;
final float volume = (float) (1 - (Math.log(MAX_VOLUME - soundVolume) / Math.log(MAX_VOLUME)));
introSong.setVolume(volume, volume);
introSong.seekTo(mediaLengthSong);
introSong.start(); // it doesn't loop
new CountDownTimer(2000, 100) {
public void onFinish() {
// When timer is finished
// Execute your code here
introVoice.start();
}
public void onTick(long millisUntilFinished) {
// millisUntilFinished The amount of time until finished.
}
}.start();
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
// all this is to pause when the user has a phone call or closes the app*/
}
@Override
protected void onStop() {
super.onStop();
introSong.pause();
introVoice.pause();
mp3.pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
introSong.pause();
introVoice.pause();
mp3.pause();
}
@Override
protected void onStart() {
super.onStart();
playMediaPlayer();
}
private void playMediaPlayer() {
introSong.start();
introVoice.start();
}
// Not really sure what I did this for but it seems to help pause and play
int count = 0;
public void onClick(View view) {
if(introVoice.isPlaying() == true){
introVoice.stop();
}
Random randomGenerator = new Random();
int random = randomGenerator.nextInt(10);
final GifImageView home = (GifImageView) findViewById(R.id.gif);
count++;
for (int x = 0; x <= 1; x++){
mp3 = MediaPlayer.create(this, R.raw.highlyfavorable);
final int mediaLengthVoice = mp3.getCurrentPosition();
final GifImageView vuImg = (GifImageView) findViewById(R.id.gif);
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
vuImg.setImageResource(R.drawable.green);
mp3.seekTo(mediaLengthVoice);
mp3.start();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(3500);
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
new CountDownTimer(5000, 100) {
public void onFinish() {
home.setImageResource(R.drawable.home);
}
public void onTick(long millisUntilFinished) {
// millisUntilFinished The amount of time until finished.
}
}.start();
}
if (count % 2 != 0 && count > 1) {
if (random == 1) {
mp3 = MediaPlayer.create(this, R.raw.veryunfavorable);
final int mediaLengthVoice = mp3.getCurrentPosition();
final GifImageView vuImg = (GifImageView) findViewById(R.id.gif);
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
vuImg.setImageResource(R.drawable.red);
mp3.seekTo(mediaLengthVoice);
mp3.start();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
else if (random == 2) {
mp3 = MediaPlayer.create(this, R.raw.unfavorable);
final int mediaLengthVoice = mp3.getCurrentPosition();
final GifImageView vuImg = (GifImageView) findViewById(R.id.gif);
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
vuImg.setImageResource(R.drawable.yellow);
mp3.seekTo(mediaLengthVoice);
mp3.start();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(1000);
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
else if (random == 3) {
mp3 = MediaPlayer.create(this, R.raw.fair);
final int mediaLengthVoice = mp3.getCurrentPosition();
final GifImageView vuImg = (GifImageView) findViewById(R.id.gif);
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
vuImg.setImageResource(R.drawable.yellow);
mp3.seekTo(mediaLengthVoice);
mp3.start();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(1500);
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
else if (random == 4) {
mp3 = MediaPlayer.create(this, R.raw.veryfavorable);
final int mediaLengthVoice = mp3.getCurrentPosition();
final GifImageView vuImg = (GifImageView) findViewById(R.id.gif);
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
vuImg.setImageResource(R.drawable.green);
mp3.seekTo(mediaLengthVoice);
mp3.start();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(2500);
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
else if (random == 5) {
mp3 = MediaPlayer.create(this, R.raw.highlyfavorable);
final int mediaLengthVoice = mp3.getCurrentPosition();
final GifImageView vuImg = (GifImageView) findViewById(R.id.gif);
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
vuImg.setImageResource(R.drawable.green);
mp3.seekTo(mediaLengthVoice);
mp3.start();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(3000);
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
else if (random == 6) {
mp3 = MediaPlayer.create(this, R.raw.highlyunfavorable);
final int mediaLengthVoice = mp3.getCurrentPosition();
final GifImageView vuImg = (GifImageView) findViewById(R.id.gif);
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
mp3.seekTo(mediaLengthVoice);
mp3.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
vuImg.setImageResource(R.drawable.red);
mp3.seekTo(mediaLengthVoice);
mp3.start();
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
else {
int[] quote = new int[]{R.raw.q1, R.raw.q2, R.raw.q3, R.raw.q4, R.raw.q5, R.raw.q6, R.raw.q7, R.raw.q8,
R.raw.q9, R.raw.q10, R.raw.q11, R.raw.q12, R.raw.q13, R.raw.q14, R.raw.q15, R.raw.q16,
R.raw.q17, R.raw.q18, R.raw.q19, R.raw.q20, R.raw.q21, R.raw.q22, R.raw.q23, R.raw.q24, R.raw.q25};
int random2 = randomGenerator.nextInt(25);
// Random number generator for the Quotes above
final MediaPlayer quotes;
quotes = MediaPlayer.create(this, quote[random2]);
final GifImageView quotesImg = (GifImageView) findViewById(R.id.gif);
final int mediaLengthResponse = quotes.getCurrentPosition();
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
String number = incomingNumber;
if (state == TelephonyManager.CALL_STATE_RINGING) {
quotes.seekTo(mediaLengthResponse);
quotes.pause();
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
quotes.seekTo(mediaLengthResponse);
quotes.pause();
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
quotesImg.setImageResource(R.drawable.blue);
quotes.seekTo(mediaLengthResponse);
quotes.start();
}
}
};
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
/*@Override
protected void onCompletion (MediaPlayer mp3) {
super.onCompletion();
mp3.release();
}*/
}
}
任何看到此代码的人都会受到启发。