MediaPlayer一次播放所有歌曲

时间:2015-02-21 13:18:18

标签: java android android-mediaplayer

我有一个简单的应用程序,它有一个按钮和一个textview,点击后,它从音频流缓冲,并使用mediaPlayer对象播放它。

我为不同的歌曲创建了一个简单的循环,以便在不同的应用程序执行时播放。 (每次用户打开应用程序并按下播放时,必须播放新歌)

但是媒体播放器播放了一首歌,adn然后缓冲secod和第三首歌,所有歌曲立刻开始播放,造成混乱。

有什么问题?

这是我的代码:

package dreamnyc.lyricazam;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;

public class Search extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        String url = "";
        final TextView e1;
        final Button b;
        b = (Button) findViewById(R.id.button);
        e1 = (TextView) findViewById(R.id.textView);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(numb1=5; numb1<8; numb1++){
                    e1.setText(String.valueOf(numb1));
                    nyc2(numb1);
                    b.setVisibility(View.GONE);
                }
            }
        });
    }
    public int numb1=0;
    public String url = "";
    void nyc2(int a) {
        a = numb1;
        url = "http://sound3.mp3slash.net/indianx/namaste_london/namaste_london0" + numb1 + "(www.songs.pk).mp3";
        nycnyc();
        }
    void nycnyc() {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(url);
            } catch (IOException e) {
            }
        try {
            mediaPlayer.prepare();

            } catch (IOException e) {
            }
            // might take long! (for buffering, etc)
        mediaPlayer.start();
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过随机为numb1分配值来解决此问题。

double max = 10.0; // number of songs in the view.
double min = 1.0;

int numb1;

b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                numb1 = (int)(Math.random() * (max - min) + min);
                e1.setText(String.valueOf(numb1));
                nyc2(numb1);
                b.setVisibility(View.GONE);
            }
        }
    });