我创建了带有seekbar的音频 Activity ,但已播放,但是在进行seekbar时我遇到了问题,因为我使用了(Thread.sleep),因此每隔1秒就会减少声音。

我检查了Internet中的另一个解决方案,我检查的所有代码都使用相同的概念。

这是我的代码

public class PlaySounds extends Activity implements Runnable {
SeekBar seeksounds;
MediaPlayer mp;
Handler handler = new Handler();
int id;
public boolean isFileExists(String fileName) {
    try {
        File myFile = new File(fileName);
        FileInputStream fIn = new FileInputStream(myFile);
        BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
        return true;
    } catch (Exception e) {
        return false;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playsounds);
    seeksounds = (SeekBar) findViewById(R.id.seeksounds);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        String path = Environment.getExternalStorageDirectory().toString() + "/dowload/splash.mp3";
        if (isFileExists(path)) {
            mp = new MediaPlayer();
            try {
                mp.setDataSource(path);
                mp.prepare();
                mp.start();
                int maxD = mp.getDuration();
                int currentD = mp.getCurrentPosition();
                seeksounds.setMax(maxD);
                seeksounds.setProgress(currentD);
                new Thread(this).start();
                seeksounds.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar arg0, int progress,
                            boolean arg2) {
                        mp.seekTo(progress);
                        seeksounds.setProgress(progress);

                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                });
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    mp.release();
}

@Override
public void run() {
    int currentPosition= 0;
    int total = mp.getDuration();
    while (mp!=null && currentPosition<total) {
        try {
            Thread.sleep(1000);
            currentPosition= mp.getCurrentPosition();
            seeksounds.setProgress(currentPosition);
        }  catch (Exception e) {
            return;
        }

    }

}

}

最佳答案

尝试在此处添加mp = null;

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    mp.release();
    mp = null;
}

关于java - android中的音频和搜索栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19433384/

10-10 09:31