gpt4 book ai didi

android - Media.setLooping无法正常使用[Android]

转载 作者:行者123 更新时间:2023-12-02 23:48:07 25 4
gpt4 key购买 nike

大家好,我需要我的android项目一些帮助:)我想按一个按钮播放声音时,但我不希望这种声音结束,我希望它重复播放。这是我的代码:

package projectill.mrme4ka.org.projectill;

import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity implements
View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void onClick(View v){};

public void buttonBtn (View v){

MediaPlayer mediaPlayer = MediaPlayer.create(this, R.drawable.wavsp);
mediaPlayer.start();
mediaPlayer.setLooping(true);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

}

它正在工作,但不再播放。它不会循环。请帮助我:) +您能帮我在声音上添加OFF按钮吗,否则我自己做,但是我不知道为什么循环不起作用

最佳答案

我不能肯定地说,但是您可能在准备MediaPlayer之前先调用start()setLooping()。 MediaPlayer对象是状态机,必须小心处理(请参阅State Diagram here)。

尝试以下方法:

mediaPlayer.setDataSource(this, R.drawable.wavsp);
mediaPlayer.setLooping(true);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});

关于android - Media.setLooping无法正常使用[Android],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28644040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com