MediaPlayer无法播放声音

时间:2019-11-13 11:14:51

标签: java android android-studio

我有2个课程 Stopwatch MyAsyncTaskSW

秒表:

private void Something(){ 
    MediaPlayer countDown = null;
    Ringtone done = null;
    countDown = postaviCountDown(countDown);
    done = postaviFinalSound();
    postaviStopAlarm(stopAlarm, done, counter, startBut);
    MyAsyncTaskSW myAsyncTaskSW = new MyAsyncTaskSW(sec, counter, countDown, done, stopAlarm);
    myAsyncTaskSW.execute(counter);
}

private MediaPlayer postaviCountDown(MediaPlayer mp){
    mp = MediaPlayer.create(this, R.raw.sound);
    return mp;
}

private Ringtone postaviFinalSound(){
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    return r;
}

MyAsyncTaskSW:

private Integer sekunde;
    private TextView counter;
    private MediaPlayer CountDown;
    private Ringtone Done;
    private Button StopAlarm;

    public MyAsyncTaskSW(Integer Sekunde, TextView Counter, MediaPlayer coundDown, Ringtone done, Button stopAlarm){
        this.sekunde = Sekunde;
        this.counter = Counter;
        this.Done = done;
        this.CountDown = coundDown;
        this.StopAlarm = stopAlarm;
    }

@Override
    protected void onPostExecute(TextView o) {
        super.onPostExecute(o);
        CountDown.stop();
        Done.play();
        StopAlarm.setVisibility(View.VISIBLE);
        counter.setText("Vrijeme isteklo!");
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        CountDown.start();
        counter.setText(Integer.toString(values[0]));
    }

所以 CountDown.start(); 根本不播放声音,而 Done.play(); 可以正常工作。

我什至试图像这样:

private void Something(){ 
        MediaPlayer countDown = null;
        Ringtone done = null;
        countDown = postaviCountDown(countDown);
        CountDown.start();
        CountDown.start();
        done = postaviFinalSound();
        postaviStopAlarm(stopAlarm, done, counter, startBut);
        MyAsyncTaskSW myAsyncTaskSW = new MyAsyncTaskSW(sec, counter, countDown, done, stopAlarm);
        myAsyncTaskSW.execute(counter);
    }

但仍然没有。 我在 res-> raw 文件夹中添加了声音。

Screenshot

0 个答案:

没有答案