我在菜单上创建了一个播放mp3的项目。但它无法停止。当我点击"停止"我怎么能停止媒体播放器?按钮。我希望点击停止按钮必须停止所有在菜单上播放mp3。 感谢...
公共类DetailActivity扩展了AppCompatActivity {
Toolbar mToolbar;
ImageView mFlower;
TextView mDescription,mBaslik;
Button fab;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
mToolbar = findViewById(R.id.toolbar);
mToolbar.setTitle(getResources().getString(R.string.app_name));
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("MP3 --->");
}
@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) {
int id = item.getItemId();
if (id == R.id.action_settings) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon2);
mediaPlayer.start();
}
if (id == R.id.stop) {
mediaPlayer.stop();
mediaPlayer.release();
// mediaPlayer.onDestroy();
}
if (id == R.id.fav) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon2);
mediaPlayer.start();
}
if (id == R.id.yor) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon3);
mediaPlayer.start();
}
if (id == R.id.asd) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon1);
mediaPlayer.start();
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
您必须在此方法之外声明MediaPlayer对象
MediaPlayer mediaplayer;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.foni) {
//MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon2);
mediaPlayer = MediaPlayer.create(this, R.raw.fon2);
mediaPlayer.start();
}
if (id == R.id.stop) {
//MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.stop();
}
if (id == R.id.fonb) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon1);
mediaPlayer.start();
}
if (id == R.id.fonu) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.fon3);
mediaPlayer.start();
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
MediaPlayer mp;
AssetFileDescriptor descriptor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout./////Your activity layout////);
if(mp != null){
if(mp.isPlaying()){
mp.stop();
}
}
try {
descriptor = getAssets().openFd("////the name of your song.mp3////"); // song should be in assets folder
} catch (IOException e) {
e.printStackTrace();
}
audioPlayer();
}
public void audioPlayer() {
//set up MediaPlayer
mp = new MediaPlayer();
try {
mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
public void OnPlayPressed(View view){
if(mp.isPlaying()){
mp.pause();
}else{
mp.start();
}
}
public void OnStopPressed(View view){
mp.stop();
try {
descriptor = getAssets().openFd("////the name of your song.mp3////"); // song should be in assets folder
} catch (IOException e) {
e.printStackTrace();
}
audioPlayer();
}
创建2个按钮,1个用于开始/暂停,1个用于停止。 “OnPlayPressed”是第一个按钮的监听器,“OnStopPressed”是第二个按钮的监听器。
只要你掌握了它背后的逻辑,就随意乱用这段代码。
编辑:在onStopPressed中我重新初始化描述符,以便您可以再次按“播放”按钮再次播放您的歌曲。但这不是必需的。你可以写“mp.stop();”。