Yogesh
Yogesh

Reputation: 1216

Android play sound - forceclose error

I am using below code to play wav sound on touch of imageview

                MediaPlayer mp = MediaPlayer.create(this, R.raw.ok);
                mp.start();
                while (mp.isPlaying()) { 
                    // donothing 
                 };
                 mp.release();

this gives me Force Close error, am i missing anything? or do i need to provide the permission in manifest file?..Please help.

Thanks

Upvotes: 0

Views: 800

Answers (2)

ilango j
ilango j

Reputation: 6037

after created media player you have to call prepare() before start. Try this code

MediaPlayer mp = MediaPlayer.create(this, R.raw.ok);
                mp.prepare();
                mp.start();

Upvotes: 1

Anju
Anju

Reputation: 9479

Try with this and see.

    public void audioPlayer(String path, String fileName){
        //set up MediaPlayer    
        MediaPlayer mp = new MediaPlayer();

        try {
            mp.setDataSource(path+"/"+fileName);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp.start();

}

Change this method according to your code. Also if there is any error, you should post the error log so that others could identify the exact issue.

Upvotes: 0

Related Questions