简体   繁体   English

Android-Mediaplayer的下一个按钮不起作用

[英]Android - Mediaplayer next button doesn't work

I am working on a live streaming radio Android app...i have an array with 4 tracks, when i reach the end of the list suppose that the next button will represent " the last song" but it doesn't and crashes the app. 我正在开发一个实时流式广播Android应用程序...我有一个包含4首曲目的数组,当我到达列表的末尾时,假设下一个按钮将代表“最后一首歌”,但不会,并且使该应用程序崩溃。

Here is the button code: 这是按钮代码:

    counter=0;

    mediaPlayer=new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    btn_next.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View v) {
             if (counter < songurl.length) {
                 counter = counter + 1;
                 textview.setText(songurl[counter]);
                 try {
                     mediaPlayer.reset();
                 } catch (Exception ex) {
                     try {
                         mediaPlayer.setDataSource(songurl[counter]);
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                     try {
                         mediaPlayer.prepare();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                     mediaPlayer.start();
                 }
             } else {

                 Toast.makeText(MainActivity.this, "last song", Toast.LENGTH_SHORT).show();
             }
         }
    });

One more question... in case I want to make it a closed loop when the app reaches the last song and I want to press next to start the list from the beginning how can I do it? 还有一个问题...如果我想在应用播放到最后一首歌曲时将其设为闭环,并且我想按next从头开始播放列表,该怎么办? Thanks. 谢谢。

You can simply add a modulo (%) operator in your code 您可以在代码中简单地添加一个模(%)运算符

counter = counter++ % songurl.length

no need to add an extra if else block 无需添加其他if块

btn_next.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        counter = counter++ % songurl.length
        textview.setText(songurl[counter]);
        try {
            mediaPlayer.reset();
        } catch (Exception ex) {
            try {
                mediaPlayer.setDataSource(songurl[counter]);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.start();
        }

    }
});

One problem you have is that you are incrementing the counter just after you check that it is within the bounds, which means your safety check is useless. 您遇到的一个问题是,在检查计数器是否在边界内之后就增加了计数器,这意味着您的安全检查是无用的。 Move counter = counter + 1; 移动counter = counter + 1; to the end of the if statement. 到if语句的末尾。

As far as making it loop continuously, you can change it to counter = (counter + 1) % songurl.length; 至于使其连续循环,您可以将其更改为counter = (counter + 1) % songurl.length; at the end of the if statement body. 在if语句主体的末尾。

Beyond the scope of the question you asked, there are some problems with your usage of MediaPlayer . 除了您所问问题的范围之外,您对MediaPlayer的使用还存在一些问题。 It doesn't make any sense to have so much key logic in a catch statement. 在catch语句中包含如此多的关键逻辑没有任何意义。 That means you expect the exception to happen. 这意味着您期望异常发生。 You should re-think what you are doing there. 您应该重新考虑您在那里所做的事情。

You see you called mediaPlayer.setDataSource(songurl[counter]) in the catch block. 您会在catch块中看到名为mediaPlayer.setDataSource(songurl [counter])的信息。 what if it will not have exception when you call mediaPlayer.reset() 如果调用mediaPlayer.reset()时没有异常怎么办

try this: 尝试这个:

try{
    mediaPlayer.reset();
    mediaPlayer.setDataSource();
    mediaPlayer.prepareAsync();--or mediaPlayer.prepare();
    (It depends on whether you load the url on the internet)
    mediaPlayer.start(); -- if you call mediaPlayer.prepareAsync() you should call mediaPlayer.start() at the OnPreparedListener;

 mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
}

And if you still wondering, I suggest you to read something about MediaPlay's state. 而且,如果您仍然想知道,我建议您阅读一些有关MediaPlay状态的信息。

PS If mediaPlayer is playing ,and you wanna let it play another one ,call mediaPlayer.stop() first; PS:如果mediaPlayer正在播放,并且您想让它播放另一个,请首先调用mediaPlayer.stop();。

To make the loop you should check in the onclicklistener that the if the counter is equal to the songurl.length which is i think the total number of song in the playlist. 要进行循环,您应该在onclicklistener中检查是否计数器等于songurl.length,即我认为播放列表中歌曲的总数。

int counter=0;

    MediaPlayer mediaPlayer=new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    btn_next.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if (counter < songurl.length) {
                counter = counter + 1;
                textview.setText(songurl[counter]);
                try {
                    mediaPlayer.reset();
                } catch (Exception ex) {
                    try {
                        mediaPlayer.setDataSource(songurl[counter]);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.start();
                }
            } else if(counter == songurl.length) {

                counter = 0;
               // Toast.makeText(MainActivity.this, "last song", Toast.LENGTH_SHORT).show();
                textview.setText(songurl[counter]);
                try {
                    mediaPlayer.reset();
                } catch (Exception ex) {
                    try {
                        mediaPlayer.setDataSource(songurl[counter]);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.start();
                }
            }
        }
    });

in the elseif condition i have set the counter to 0. so that it will get in the loop. 在elseif条件下,我将计数器设置为0。这样它将进入循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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