简体   繁体   中英

audio and seekbar in android

i created audio activity with seekbar, it was played but i have problem when seekbar in progress there is cutting each 1 second in sound because i used (Thread.sleep).

i check another solution in Internet, all codes i checked were use same concept.

this is my code

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

    }

}

}

Try to add mp = null; here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM