Home How to use same button after setoncompletitionlistener?
Reply: 0

How to use same button after setoncompletitionlistener?

user957
1#
user957 Published in May 20, 2018, 11:38 am

After I set oncompletetionlistener for MediaPlayer. Using the same button again after the mediaplayer is release, it crashes the app. How do I call a event to play audio after the media player is release?

public class MainActivity extends AppCompatActivity {



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


    Button bt1 = findViewById(R.id.b1);
    final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.button1);
    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mp1.start();
            mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp1.release();
                }
            });
        }
    });

}}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308487 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO