0

我用 seekbar 创建了音频活动,它被播放了,但是当我使用 seekbar 时,我遇到了问题,因为我使用了(Thread.sleep),所以每 1 秒的声音就会减少。

我在互联网上检查了另一个解决方案,我检查的所有代码都使用相同的概念。

这是我的代码

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;
        }            

    }

}

}

4

1 回答 1

0

尝试在mp = null;此处添加

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    mp.release();
    mp = null;
}
于 2013-10-17T18:22:13.570 回答