在数组列表中播放下一首歌时,Android应用程序强制关闭

时间:2014-05-26 14:50:06

标签: java android media-player

我正在创建一个Android应用程序(供我自己使用),让我可以从GTA V收听我最喜欢的歌曲。 媒体播放器从我的保管箱帐户流式传输mp3曲目,所有这一切都正常。

问题是,在第一首歌结束后,我希望它播放下一首歌,但是当我在第一首歌完成播放应用程序崩溃后运行应用程序而不是播放列表中的下一首歌时。

有人可以帮我解决这个问题吗?

感谢。

代码

public class channelx extends Activity implements MediaPlayer.OnPreparedListener {

    Button play, pause, home, refresh;
    protected Dialog mSplashDialog;

     private int playlistPos = 0;
        private List<Uri> myUris = new ArrayList<Uri>();
        private MediaPlayer sdrPlayer = new MediaPlayer();

    /** Called when the activity is first created. */@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.channelx);

        final ProgressDialog progressDialog = new ProgressDialog(channelx.this);

        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Loading...");
        progressDialog.show();

        final Timer t = new Timer();
        t.schedule(new TimerTask() {
            public void run() {
                progressDialog.dismiss(); 
                t.cancel(); 
            }
        }, 5000); 

        play = (Button) findViewById(R.id.play);
        pause = (Button) findViewById(R.id.pause);
        home = (Button) findViewById(R.id.home);
        refresh = (Button) findViewById(R.id.refresh);

         myUris.add(Uri.parse("https://db.tt/4ANlGRms"));
         myUris.add(Uri.parse("https://db.tt/TpmxsqGJ"));

         myUris.add(Uri.parse("https://db.tt/rbkYtuNI"));
         myUris.add(Uri.parse("https://db.tt/JYVHp9nm"));
         myUris.add(Uri.parse("https://db.tt/xWLATUQp"));
         myUris.add(Uri.parse("https://db.tt/WzV2TzFu"));
            // Add the others as well...

            initSong(myUris.get(playlistPos));

            sdrPlayer.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    sdrPlayer.reset();

                    playlistPos++;
                    initSong(myUris.get(playlistPos));


                    sdrPlayer.start(); // Start it as well if you wish
                }
            });



        play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sdrPlayer.start();
            }
        }
        );


        pause.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sdrPlayer.pause();
            }
        });

        home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder1 = new AlertDialog.Builder(channelx.this);
                builder1.setMessage("Are you sure you want to leave this station? Playback will be stopped.");
                builder1.setCancelable(true);
                builder1.setPositiveButton("Continue",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        sdrPlayer.stop();
                        Intent intent = new Intent(channelx.this, MainActivity.class);
                        startActivity(intent);
                    }
                });
                builder1.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

                AlertDialog alert11 = builder1.create();
                alert11.show();

            }
        });

        refresh.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sdrPlayer.stop();
                Intent intent = new Intent(channelx.this, channelx.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onPrepared(MediaPlayer mp) {

    }

    public void initSong(Uri myUri) {
        try {
            sdrPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            sdrPlayer.setDataSource(this, myUri);           
            sdrPlayer.prepareAsync(); // don't use prepareAsync for mp3 playback
        }
        catch (IOException e) {

            e.printStackTrace();
            Toast.makeText(channelx.this,
                       "Please turn on WiFi and try again",
                       Toast.LENGTH_LONG).show();
        }
    }




}

logcat的

05-26 15:37:14.574: D/MediaPlayer(16744): mIsAboveSense55:-1
05-26 15:37:14.574: D/MediaPlayer(16744): sense_version:5.5
05-26 15:37:14.574: D/MediaPlayer(16744): ver s1:   55000
05-26 15:37:14.574: D/MediaPlayer(16744): ver s2:   55000
05-26 15:37:14.664: V/MediaPlayer(16744): network type=wifi
05-26 15:37:14.664: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:37:14.694: D/MediaPlayer(16744): Couldn't open file on client side, trying server side
05-26 15:37:14.694: I/MediaPlayer(16744): setLPAflag() in
05-26 15:37:14.704: D/MediaPlayer(16744): SystemProperties.get htc.audio.mirrorlink.status: 
05-26 15:37:14.704: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false +
05-26 15:37:14.704: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false -
05-26 15:37:14.704: I/MediaPlayer(16744): setLPAflag() out
05-26 15:37:14.754: D/MediaPlayer(16744): Mediaplayer receives message, message type: 8
05-26 15:37:22.833: W/MediaPlayer(16744): info/warning (1, 902)
05-26 15:37:22.833: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:22.833: D/MediaPlayer(16744): Mediaplayer receives message, message type: 5
05-26 15:37:22.833: D/MediaPlayer(16744): Mediaplayer receives message, message type: 1
05-26 15:37:22.853: E/MediaPlayer(16744): Should have subtitle controller already set
05-26 15:37:24.044: D/[MediaPluginDLNA](16744): [getIsRegionSupported] sRegionCode: 6
05-26 15:37:24.044: D/[MediaPluginDLNA](16744): not in Mirror mode
05-26 15:37:24.044: D/MediaPlayer(16744): doStart() in
05-26 15:37:24.044: D/MediaPlayer(16744): Htc_getIntParameter = 902
05-26 15:37:24.184: D/MediaPlayer(16744): Mediaplayer receives message, message type: 6
05-26 15:37:25.115: D/MediaPlayer(16744): Mediaplayer receives message, message type: 7
05-26 15:37:26.176: W/MediaPlayer(16744): info/warning (703, 0)
05-26 15:37:26.176: W/MediaPlayer(16744): info/warning (701, 0)
05-26 15:37:26.176: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:26.186: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:27.177: W/MediaPlayer(16744): info/warning (702, 0)
05-26 15:37:27.207: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:28.238: D/MediaPlayer(16744): Mediaplayer receives message, message type: 7
05-26 15:37:29.370: W/MediaPlayer(16744): info/warning (703, 0)
05-26 15:37:29.370: W/MediaPlayer(16744): info/warning (701, 0)
05-26 15:37:29.370: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:29.370: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:30.391: W/MediaPlayer(16744): info/warning (702, 0)
05-26 15:37:30.401: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:31.412: D/MediaPlayer(16744): Mediaplayer receives message, message type: 7
05-26 15:37:31.612: W/MediaPlayer(16744): info/warning (703, 0)
05-26 15:37:31.612: W/MediaPlayer(16744): info/warning (701, 0)
05-26 15:37:31.612: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:31.612: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:37:32.433: W/MediaPlayer(16744): info/warning (702, 0)
05-26 15:37:32.433: D/MediaPlayer(16744): Mediaplayer receives message, message type: 200
05-26 15:39:16.584: D/MediaPlayer(16744): Mediaplayer receives message, message type: 7
05-26 15:39:16.764: E/MediaPlayer(16744): internal/external state mismatch corrected
05-26 15:39:16.774: D/MediaPlayer(16744): Mediaplayer receives message, message type: 2
05-26 15:39:16.774: D/MediaPlayer(16744): reset() in
05-26 15:39:16.814: D/MediaPlayer(16744): reset() out
05-26 15:39:16.814: D/MediaPlayer(16744): reset() out
05-26 15:39:16.824: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:16.864: V/MediaPlayer(16744): network type=wifi
05-26 15:39:16.864: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:16.894: D/MediaPlayer(16744): Couldn't open file on client side, trying server side
05-26 15:39:16.894: I/MediaPlayer(16744): setLPAflag() in
05-26 15:39:16.894: D/MediaPlayer(16744): SystemProperties.get htc.audio.mirrorlink.status: 
05-26 15:39:16.904: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false +
05-26 15:39:16.904: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false -
05-26 15:39:16.904: I/MediaPlayer(16744): setLPAflag() out
05-26 15:39:16.904: D/[MediaPluginDLNA](16744): [getIsRegionSupported] sRegionCode: 6
05-26 15:39:16.904: D/MediaPlayer(16744): doStart() in
05-26 15:39:16.904: D/MediaPlayer(16744): Htc_getIntParameter = 0
05-26 15:39:16.904: E/MediaPlayer(16744): start called in state 4
05-26 15:39:16.904: E/MediaPlayer(16744): error (-38, 0)
05-26 15:39:16.904: D/MediaPlayer(16744): start() out
05-26 15:39:16.904: D/MediaPlayer(16744): U58 Send PlaybackCompleteEvent
05-26 15:39:16.924: D/MediaPlayer(16744): Mediaplayer receives message, message type: 100
05-26 15:39:16.924: E/MediaPlayer(16744): Error (-38,0)
05-26 15:39:16.924: D/MediaPlayer(16744): reset() in
05-26 15:39:16.924: D/MediaPlayer(16744): reset() out
05-26 15:39:16.924: D/MediaPlayer(16744): reset() out
05-26 15:39:16.934: E/MediaPlayer(16744): error (1, -107)
05-26 15:39:16.934: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:16.934: V/MediaPlayer(16744): network type=wifi
05-26 15:39:16.934: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:16.934: D/MediaPlayer(16744): Couldn't open file on client side, trying server side
05-26 15:39:16.934: I/MediaPlayer(16744): setLPAflag() in
05-26 15:39:16.934: D/MediaPlayer(16744): SystemProperties.get htc.audio.mirrorlink.status: 
05-26 15:39:16.934: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false +
05-26 15:39:16.934: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false -
05-26 15:39:16.934: I/MediaPlayer(16744): setLPAflag() out
05-26 15:39:16.934: D/[MediaPluginDLNA](16744): [getIsRegionSupported] sRegionCode: 6
05-26 15:39:16.934: D/MediaPlayer(16744): doStart() in
05-26 15:39:16.944: D/MediaPlayer(16744): Htc_getIntParameter = 0
05-26 15:39:16.944: E/MediaPlayer(16744): start called in state 4
05-26 15:39:16.944: E/MediaPlayer(16744): error (-38, 0)
05-26 15:39:16.944: D/MediaPlayer(16744): start() out
05-26 15:39:16.944: D/MediaPlayer(16744): Mediaplayer receives message, message type: 100
05-26 15:39:16.944: E/MediaPlayer(16744): Error (1,-107)
05-26 15:39:16.944: D/MediaPlayer(16744): reset() in
05-26 15:39:16.944: D/MediaPlayer(16744): reset() out
05-26 15:39:16.944: D/MediaPlayer(16744): reset() out
05-26 15:39:16.944: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:16.944: E/MediaPlayer(16744): error (1, -107)
05-26 15:39:16.955: V/MediaPlayer(16744): network type=wifi
05-26 15:39:16.955: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:16.965: D/MediaPlayer(16744): Couldn't open file on client side, trying server side
05-26 15:39:16.965: I/MediaPlayer(16744): setLPAflag() in
05-26 15:39:16.965: D/MediaPlayer(16744): SystemProperties.get htc.audio.mirrorlink.status: 
05-26 15:39:16.975: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false +
05-26 15:39:16.975: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false -
05-26 15:39:16.975: I/MediaPlayer(16744): setLPAflag() out
05-26 15:39:16.975: D/[MediaPluginDLNA](16744): [getIsRegionSupported] sRegionCode: 6
05-26 15:39:16.975: D/MediaPlayer(16744): doStart() in
05-26 15:39:16.975: D/MediaPlayer(16744): Htc_getIntParameter = 0
05-26 15:39:16.975: E/MediaPlayer(16744): start called in state 4
05-26 15:39:16.975: E/MediaPlayer(16744): error (-38, 0)
05-26 15:39:16.975: D/MediaPlayer(16744): start() out
05-26 15:39:16.975: D/MediaPlayer(16744): Mediaplayer receives message, message type: 100
05-26 15:39:16.975: E/MediaPlayer(16744): Error (1,-107)
05-26 15:39:16.975: D/MediaPlayer(16744): reset() in
05-26 15:39:16.975: D/MediaPlayer(16744): reset() out
05-26 15:39:16.975: D/MediaPlayer(16744): reset() out
05-26 15:39:16.975: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:16.985: E/MediaPlayer(16744): error (1, -107)
05-26 15:39:16.985: V/MediaPlayer(16744): network type=wifi
05-26 15:39:16.985: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:16.995: D/MediaPlayer(16744): Couldn't open file on client side, trying server side
05-26 15:39:16.995: I/MediaPlayer(16744): setLPAflag() in
05-26 15:39:16.995: D/MediaPlayer(16744): SystemProperties.get htc.audio.mirrorlink.status: 
05-26 15:39:16.995: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false +
05-26 15:39:16.995: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false -
05-26 15:39:16.995: I/MediaPlayer(16744): setLPAflag() out
05-26 15:39:17.005: D/[MediaPluginDLNA](16744): [getIsRegionSupported] sRegionCode: 6
05-26 15:39:17.005: D/MediaPlayer(16744): doStart() in
05-26 15:39:17.005: D/MediaPlayer(16744): Htc_getIntParameter = 0
05-26 15:39:17.005: E/MediaPlayer(16744): start called in state 4
05-26 15:39:17.005: E/MediaPlayer(16744): error (-38, 0)
05-26 15:39:17.005: D/MediaPlayer(16744): start() out
05-26 15:39:17.005: D/MediaPlayer(16744): Mediaplayer receives message, message type: 100
05-26 15:39:17.005: E/MediaPlayer(16744): Error (1,-107)
05-26 15:39:17.005: D/MediaPlayer(16744): reset() in
05-26 15:39:17.005: D/MediaPlayer(16744): reset() out
05-26 15:39:17.005: D/MediaPlayer(16744): reset() out
05-26 15:39:17.005: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:17.015: E/MediaPlayer(16744): error (1, -107)
05-26 15:39:17.015: V/MediaPlayer(16744): network type=wifi
05-26 15:39:17.015: D/MediaPlayer(16744): mIsAboveSense55:1
05-26 15:39:17.015: D/MediaPlayer(16744): Couldn't open file on client side, trying server side
05-26 15:39:17.025: I/MediaPlayer(16744): setLPAflag() in
05-26 15:39:17.025: D/MediaPlayer(16744): SystemProperties.get htc.audio.mirrorlink.status: 
05-26 15:39:17.025: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false +
05-26 15:39:17.025: D/MediaPlayer(16744): set AwesomePlayer LPA flag to false -
05-26 15:39:17.025: I/MediaPlayer(16744): setLPAflag() out
05-26 15:39:17.025: D/[MediaPluginDLNA](16744): [getIsRegionSupported] sRegionCode: 6
05-26 15:39:17.025: D/MediaPlayer(16744): doStart() in
05-26 15:39:17.025: D/MediaPlayer(16744): Htc_getIntParameter = 0
05-26 15:39:17.025: E/MediaPlayer(16744): start called in state 4
05-26 15:39:17.025: E/MediaPlayer(16744): error (-38, 0)
05-26 15:39:17.025: D/MediaPlayer(16744): start() out
05-26 15:39:17.025: D/MediaPlayer(16744): Mediaplayer receives message, message type: 100
05-26 15:39:17.025: E/MediaPlayer(16744): Error (1,-107)
05-26 15:39:17.025: D/MediaPlayer(16744): reset() in
05-26 15:39:17.035: D/MediaPlayer(16744): reset() out
05-26 15:39:17.035: D/MediaPlayer(16744): reset() out
05-26 15:39:17.035: W/dalvikvm(16744): threadid=1: thread exiting with uncaught exception (group=0x4167de18)
05-26 15:39:17.035: E/MediaPlayer(16744): error (1, -107)
05-26 15:39:17.045: E/AndroidRuntime(16744): FATAL EXCEPTION: main
05-26 15:39:17.045: E/AndroidRuntime(16744): Process: com.example.gtavradio, PID: 16744
05-26 15:39:17.045: E/AndroidRuntime(16744): java.lang.IndexOutOfBoundsException: Invalid index 6, size is 6
05-26 15:39:17.045: E/AndroidRuntime(16744):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at java.util.ArrayList.get(ArrayList.java:308)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at com.example.gtavradio.channelx$2.onCompletion(channelx.java:75)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:3162)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at android.os.Looper.loop(Looper.java:157)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at android.app.ActivityThread.main(ActivityThread.java:5872)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at java.lang.reflect.Method.invokeNative(Native Method)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at java.lang.reflect.Method.invoke(Method.java:515)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
05-26 15:39:17.045: E/AndroidRuntime(16744):    at dalvik.system.NativeStart.main(Native Method)
05-26 15:39:19.918: D/Process(16744): killProcess, pid=16744
05-26 15:39:19.918: D/Process(16744): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

编辑:忽略我的代码中的一些愚蠢评论

新日志

05-26 16:20:11.884: E/MediaPlayer(1530): attachNewPlayer called in state 128
05-26 16:20:11.884: W/dalvikvm(1530): threadid=1: thread exiting with uncaught exception (group=0x4167de18)
05-26 16:20:11.904: E/AndroidRuntime(1530): FATAL EXCEPTION: main
05-26 16:20:11.904: E/AndroidRuntime(1530): Process: com.example.gtavradio, PID: 1530
05-26 16:20:11.904: E/AndroidRuntime(1530): java.lang.IllegalStateException
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.media.MediaPlayer._setDataSource(Native Method)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1236)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1203)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1137)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at com.example.gtavradio.channelx.initSong(channelx.java:155)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at com.example.gtavradio.channelx$2.onCompletion(channelx.java:78)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:3089)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.os.Looper.loop(Looper.java:157)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at android.app.ActivityThread.main(ActivityThread.java:5872)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at java.lang.reflect.Method.invoke(Method.java:515)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
05-26 16:20:11.904: E/AndroidRuntime(1530):     at dalvik.system.NativeStart.main(Native Method)

编辑此代码现在不会使应用程序崩溃,但它仍然无法自动播放下一首歌曲,我必须重新播放才能播放下一首歌曲 < / p>

sdrPlayer.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    sdrPlayer.stop();
                            sdrPlayer.reset();


                    playlistPos++;
                    if (playlistPos == myUris.size()) {
                        playlistPos = 0;
                    }
                    initSong(myUris.get(playlistPos));

                    //sdrPlayer.start(); // Start it as well if you wish
                }
            });

3 个答案:

答案 0 :(得分:1)

你有一个数组索引超出范围的例外 - 我认为这是由

引起的
playlistPos++;
initSong(myUris.get(playlistPos));

只需检查playlistPos是否不大于播放列表大小:

playlistPos++;

if (playlistPos == myUris.size()) {
    playlistPos = 0;
}

initSong(myUris.get(playlistPos));

将为您循环播放列表。

现在,为了防止您遇到其他错误,请尝试从sdrPlayer.start()删除onCompletionListener,并在播放器准备好时仅调用start。

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

编辑:以下是一些正常工作的代码:

public class MainActivity extends Activity {

    private MediaPlayer mp;
    private int playlistPos = 0;
    private List<Uri> playlist;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        playlist = createPlaylist();
        mp = new MediaPlayer();
        initMediaPlayer();
        initSong();
    }


    private void initMediaPlayer() {
        mp.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                playlistPos++;

                if (playlistPos == playlist.size()) {
                    playlistPos = 0;
                }

                initSong();
            }
        });

        mp.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
    }


    private void initSong() {
        mp.reset();
        try {
            mp.setDataSource(this, playlist.get(playlistPos));
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        mp.prepareAsync();
    }


    private List<Uri> createPlaylist() {
        List<Uri> myUris = new ArrayList<Uri>();
        myUris.add(Uri.parse("https://db.tt/4ANlGRms"));
        myUris.add(Uri.parse("https://db.tt/TpmxsqGJ"));
        myUris.add(Uri.parse("https://db.tt/rbkYtuNI"));
        myUris.add(Uri.parse("https://db.tt/JYVHp9nm"));
        myUris.add(Uri.parse("https://db.tt/xWLATUQp"));
        myUris.add(Uri.parse("https://db.tt/WzV2TzFu"));
        return myUris;
    }

}

答案 1 :(得分:0)

错误代码与数组大小有关。

java.lang.IndexOutOfBoundsException: Invalid index 6, size is 6

在代码中的某个地方,您的代码最终会播放下一首索引号为6的歌曲。我不确定这是来自Dropbox API还是您正在制作它。但错误是这样的

int[6] songIds = [10, 14, 12, 54, 24, 54];
/* then somewhere while playing 
 * you reference the array as */
songIds[6];

这清楚地表明您正在尝试播放的歌曲不是阵列的一部分。您需要播放第5首歌曲,或者您需要返回第一首歌曲。您应该记住,Array索引从0开始,然后上升到arraysize - 1

在播放列表的初始化中,可能playlistPos被设置为6。你应该确保这一点。

答案 2 :(得分:-1)

我在android和java中非常漂亮:(但我想解决这个问题:

public class QueuePlay extends Thread {

    private Vector<SoundPair> mVoiceQueue = new Vector<SoundPair>();
    private PlaySound playSound;
    Object lock = new Object();

    public QueuePlay() {
    };

    // *********************************************************
    public synchronized QueuePlay setPlaySound(PlaySound playSound) {

        this.playSound = playSound;

        playSound.setEventListenerPlay(new OnEventListenerPlay() {

            @Override
            public void onPlaySound(SoundPair soundPair) {
                Log.i("MSH", "onPlaySound->" + soundPair.toString());
            }

            @Override
            public void onFinishSound(SoundPair soundPair) {
                Log.e("MSH", "onFinishSound->" + soundPair.toString());
                synchronized (lock) {
                    lock.notify();
                }
            }

            @Override
            public void onErrorOccurred(String errorMsg) {
                // TODO Auto-generated method stub
                Log.i("MSH", "onErrorOccurred->" + errorMsg);
            }
        });
        return this;
    }

    // -------------------------------------------
    public synchronized void addToVoiceQueue(SoundPair soundPair) {
        mVoiceQueue.add(soundPair);
        Log.i("MSH", "addToVoiceQueue->" + soundPair.toString());
        notify();

    }

    // -----------------------------------------
    private synchronized SoundPair getNextVoiceFromQueue()
            throws InterruptedException {

        while (mVoiceQueue.size() == 0)
            wait();

        SoundPair soundPair = (SoundPair) mVoiceQueue.get(0);
        Log.i("MSH", "getNextVoiceFromQueue->" + soundPair.toString());
        mVoiceQueue.removeElementAt(0);
        return soundPair;
    }

    private synchronized void playSound_(SoundPair aMessage) throws Exception {
        if (playSound != null) {
            playSound._play_numbers(aMessage);
        }
    }

    @Override
    public void run() {
        try {
            while (!isInterrupted()) {
                SoundPair message = getNextVoiceFromQueue();
                playSound_(message);

                synchronized (lock) {
                    lock.wait();
                }
            }
        } catch (Exception e) {
            Log.e("MSH", e.getMessage()
                    + "->RUN->Exception (Queue Play voice) ");

        } finally {

        }
    }
}

等等

public class PlaySound {

private final int MAX_NUMBER_NO = 41;
private final int MAX_QUEUE_NO = 101;

private Context context;
private AppController ac = new AppController();
private MediaPlayer mPlayer;

int number_no[] = new int[MAX_NUMBER_NO];// یک
int queue_no[] = new int[MAX_QUEUE_NO];// شماره 1 به باجه

public interface OnEventListenerPlay {
    public void onPlaySound(SoundPair soundPair);

    public void onFinishSound(SoundPair soundPair);

    public void onErrorOccurred(String errorMsg);
}

private OnEventListenerPlay mOnEventListenerPlay = null;

public void setEventListenerPlay(OnEventListenerPlay mOnEventListener) {
    this.mOnEventListenerPlay = mOnEventListener;
}

// ----------------------------------------------------------------------
public PlaySound(Context context) {
    this.context = AppController.mContext;
    getRawResource();
}

private void getRawResource() {

    for (int i = 1; i < queue_no.length; i++) {
        queue_no[i] = context.getResources().getIdentifier("raw/s" + i,
                "raw", context.getPackageName());
    }
    for (int i = 1; i < number_no.length; i++) {
        number_no[i] = context.getResources().getIdentifier("raw/n" + i,
                "raw", context.getPackageName());
    }

}

public void _play_numbers(final SoundPair soundPair) throws Exception {
    if (soundPair != null) {
        final int baje = soundPair.getBaje();
        final int shoamre = soundPair.getShoamre();
        try {
            if ((shoamre > 0 && shoamre < queue_no.length)) {
                mPlayer = MediaPlayer.create(context, queue_no[shoamre]);
                mPlayer.start();
                if (mOnEventListenerPlay != null)
                    mOnEventListenerPlay.onPlaySound(soundPair);
                mPlayer.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mPlayer.stop();
                        if (baje > 0 && baje < number_no.length) {
                            mPlayer = MediaPlayer.create(context,
                                    number_no[baje]);
                            mPlayer.start();
                            mPlayer.setOnCompletionListener(new OnCompletionListener() {

                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    mPlayer.stop();
                                    if (mOnEventListenerPlay != null)
                                        mOnEventListenerPlay.onFinishSound(soundPair);
                                }
                            });
                        } else
                            ac.logError(getClass(), "number: " + baje
                                    + " Not In Range:"
                                    + (number_no.length - 1));
                    }
                });
            } else {
                ac.logError(getClass(), "queue: " + shoamre
                        + " Not In Range:" + (queue_no.length - 1));

            }
        } catch (Exception ex) {
            ac.logError(getClass(), ex.getMessage());
            if (mOnEventListenerPlay != null)
                mOnEventListenerPlay.onErrorOccurred( ex.getMessage());
        }

    }

}

}