gpt4 book ai didi

android - 在Android中,当我播放音频并进行其他 Activity 时,音频在返回后不会停止

转载 作者:行者123 更新时间:2023-12-03 02:32:58 25 4
gpt4 key购买 nike

我是Android环境的新手,我的问题是播放音频文件并转到其他 Activity 或按“后退”按钮,然后再次打开我播放音频的应用程序,但按“停止”按钮时并没有停止。当我按下主页按钮并尝试停止时它起作用。谁能建议我该怎么做
这是我的 Activity 代码

package com.example.mudassar.myaudioapp;

import android.content.Intent;
import android.os.Environment;
import android.os.NetworkOnMainThreadException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

//MediaPlayer mp;
Button playBtn, pauseBtn, stopBtn;
SeekBar sb;
Handler han = new Handler();
MediaPlayer mp;// = MediaPlayer.create(MainActivity.this, R.raw.firefly);

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

playBtn= (Button)findViewById(R.id.playBtn);
pauseBtn= (Button)findViewById(R.id.pauseBtn);
stopBtn=(Button) findViewById(R.id.stopBtn);




try{
//you can change the path, here path is external directory(e.g. sdcard) /Music/maine.mp3
//mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/raw/.mp3");
mp = MediaPlayer.create(MainActivity.this, R.raw.firefly);
mp.prepare();
}catch(Exception e){e.printStackTrace();}

sb = (SeekBar)findViewById(R.id.seekBar);
sb.setMax(mp.getDuration());

SeekUpdation();

playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeekUpdation();
//if (mp == null)
if(mp.isPlaying() == false)
mp = MediaPlayer.create(MainActivity.this, R.raw.firefly);
mp.start();
sb.setMax(mp.getDuration());
sb.setProgress(mp.getCurrentPosition());
}
});

pauseBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(mp.isPlaying() == false){
mp.start();
sb.setProgress(mp.getCurrentPosition());
Toast.makeText(MainActivity.this, "Playing", Toast.LENGTH_SHORT).show();
}else if(mp.isPlaying() == true){
mp.pause();
sb.setProgress(mp.getCurrentPosition());
Toast.makeText(MainActivity.this, "Paused", Toast.LENGTH_SHORT).show();
}

}
});

stopBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//if (mp != null) {
if(mp.isPlaying() == true){
mp.stop();
sb.setProgress(mp.getDuration());
//mp = null;
}
Toast.makeText(MainActivity.this, "Stoped", Toast.LENGTH_SHORT).show();
}
});

sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onStopTrackingTouch(SeekBar seekBar){
//if(mp!=null){
if(!mp.isPlaying()){
mp.seekTo(seekBar.getProgress());
mp.start();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar){
//if(mp!=null)
if(mp.isPlaying()){
mp.pause();
}else
mp.start();
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){

}
});

Button btn = (Button)findViewById(R.id.secondActivityButton);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, second.class));
}
});
}

public void MyOwnClick(View v){
startActivity(new Intent(MainActivity.this, second.class));
}

public Runnable run = new Runnable() {
@Override
public void run() {
SeekUpdation();
}
};

public void SeekUpdation() {
//if(mp!=null)
if(mp.isPlaying() == true)
{
sb.setProgress(mp.getCurrentPosition());
han.postDelayed(run, 1000);
//Toast.makeText(MainActivity.this, "is Playing", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Seek", Toast.LENGTH_SHORT).show();
}
}
}

最佳答案

为了快速解决方案,您可以创建如下方法:

void resetMediaPlayer() {
if(mediaPlayer!=null) {
try {
mediaPlayer.reset();
}
catch(Exception e) {
e.printStackTrace();
}
}
}

然后在 onBackPressed中覆盖 activity方法并调用 resetMediaPlayer:
@Override
public void onBackPressed(){
super.onBackPressed();
resetMediaPlayer()
}

或在需要停止 media player的任何地方调用它。

但是,这不是停止媒体播放器的好方法。为了更好的实现,您必须像这样更改 resetMediaPlayer方法:
void resetMediaPlayer() {
if(mediaPlayer!=null) {
try {
mediaPlayer.reset();
mediaPlayer.release();
}
catch(Exception e) {
e.printStackTrace();
}
}
}

每当您要启动 Media Player时,都必须创建一个Media Player的新实例,例如:
void startMediaPlayer(int selectedSong) {
try {
mediaPlayer = MediaPlayer.create(activity, selectedSong);
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}

并在 resetMediaPlayeronDestroy方法中调用 activity:
@Override
protected void onDestroy() {
super.onDestroy();
resetPlayView();
}

关于android - 在Android中,当我播放音频并进行其他 Activity 时,音频在返回后不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44416860/

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