开发者

Sounds stopping too soon in MediaPlayer :: Android

开发者 https://www.devze.com 2023-02-08 08:11 出处:网络
Ok well I have been fighting with this for a while now and have soundboard apps that I havn\'t ran into this problem in.I am developing a widget that allows for a sound to be played when a button is p

Ok well I have been fighting with this for a while now and have soundboard apps that I havn't ran into this problem in. I am developing a widget that allows for a sound to be played when a button is pressed on the widget.

I am assuming I am just not using the setOnPrepar开发者_如何学PythonedListener properly but basically what is happening is that some of my sounds play correctly and more likely than not the sounds will cut off the last part noticeable by word or sound effect missing. Below is my code please let me know if you have any idea what's wrong with the code or post a working revision.

Thanx in advance.

    public int onStartCommand(Intent intent, int flags, int startId) {

            /** Get our place holder for quotes & sounds. */
            int s = UpdateService.getQuoteNumber();

            /** Check if a sound is playing if so... stop and reset it. */
            if (mp != null && mp.isPlaying()){mp.stop();mp.reset();}

            /** Create a new mediaplayer and set looping. */
            mp = MediaPlayer.create(this, SoundsClicked.randomSound[s]);
            mp.setLooping(false);

            /** Try to prepare the sound/mediaplayer, if not report error & type. */
            try {mp.prepare();} 
                catch (IllegalStateException e) {e.printStackTrace();Log.e(Integer.toString(s), "IllegalStateException");} 
                catch (IOException e) {e.printStackTrace();Log.e(Integer.toString(s), "IOException");}              

            /** Check if the sound is prepared, if so play it. */
            mp.setOnPreparedListener(new OnPreparedListener() {
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                    while (mp.isPlaying()) {};
                }
            });
            return flags;}

This is called from a service (obviously from the onStartCommand) but figured I would throw that out there for those not familiar with some of the methods.


If you're using Android 2.2, there are apparently some problems with the MediaPlayer cutting off the last X% of at least some audio files. I'm running in to that problem with the app I'm currently working on. I haven't found if there is a solution or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号