MediaPlayer音乐Android Studio

时间:2018-04-05 04:02:41

标签: java android android-mediaplayer

只是想知道是否有人可以帮助我或指出我正确的方向。我正在学习如何制作一个简单的音乐播放器,用户点击列表视图项目并播放mp3。顶部有一个播放按钮,下一个和上一个歌曲按钮(尚未添加搜索功能)。然而,当我选择新歌时它会不断崩溃。这是我的活动代码:

public class AudioActivity extends AppCompatActivity {


ListView listView;
ArrayList<String> musicList;
ArrayAdapter adapter;
MediaPlayer player;
ImageButton prev;
ImageButton play;
ImageButton next;

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


    prev = (ImageButton) findViewById(R.id.previous);
    play = (ImageButton) findViewById(R.id.play);
    next = (ImageButton) findViewById(R.id.next);


    listView = findViewById(R.id.musiclist);
    musicList = new ArrayList<>();
    musicList.add("song");
    musicList.add("song1");
    musicList.add("song2");
    musicList.add("song3");
    musicList.add("song4");

    adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, musicList);
    listView.setAdapter(adapter);
    player = new MediaPlayer();
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        switch (position){
            case 0:

                String url = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3";
                try {
                    player.stop();
                    //player.release();
                    player.setDataSource(url);
                    } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    player.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

            case 1:

                url = "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-8.mp3";
                try {
                    player.stop();
                    //player.release();
                    player.setDataSource(url);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    player.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

                default:
                break;
        }
        player.start();
        }
    });


}

public void onPlayBtnClicked(){
    if(!player.isPlaying())
    {
        player.start();

    }else
    {
        player.pause();
    }

}

}

04/05 00:08:09: Launching app
$ adb shell am start -n "com.example.brads.group08/com.example.brads.group08.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.example.brads.group08.test | com.example.brads.group08
Waiting for application to come online: com.example.brads.group08.test | com.example.brads.group08
Waiting for application to come online: com.example.brads.group08.test | com.example.brads.group08
Connecting to com.example.brads.group08
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ActivityThread: Application com.example.brads.group08 is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/zygote: Debugger is active
Connected to the target VM, address: 'localhost:8600', transport: 'socket'
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/chatty: uid=10091(com.example.brads.group08) identical 4 lines
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1448)
I/InstantRun: starting instant run server: is main process
D/OpenGLRenderer: HWUI GL Pipeline

                  [ 04-05 04:08:16.176 10444:10464 D/         ]
                  HostConnection::get() New Host Connection established 0xa5a26b40, tid 10464
I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
D/EGL_emulation: eglCreateContext: 0xa5a70f60: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xa5a70f60: ver 2 0 (tinfo 0xa83b2680)
D/EGL_emulation: eglMakeCurrent: 0xa5a70f60: ver 2 0 (tinfo 0xa83b2680)
I/zygote: Do partial code cache collection, code=21KB, data=30KB
I/zygote: After code cache collection, code=21KB, data=30KB
I/zygote: Increasing code cache capacity to 128KB
I/zygote: Do partial code cache collection, code=31KB, data=56KB
I/zygote: After code cache collection, code=31KB, data=56KB
I/zygote: Increasing code cache capacity to 256KB
I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I/zygote: Do full code cache collection, code=116KB, data=72KB
I/zygote: After code cache collection, code=90KB, data=45KB
D/EGL_emulation: eglMakeCurrent: 0xa5a70f60: ver 2 0 (tinfo 0xa83b2680)
D/EGL_emulation: eglMakeCurrent: 0xa5a70f60: ver 2 0 (tinfo 0xa83b2680)
D/EGL_emulation: eglMakeCurrent: 0xa5a70f60: ver 2 0 (tinfo 0xa83b2680)
W/MediaPlayer: Use of stream types is deprecated for operations other than volume control
W/MediaPlayer: See the documentation of setAudioStreamType() for what to use instead with android.media.AudioAttributes to qualify your playback use case
D/EGL_emulation: eglMakeCurrent: 0xa5a70f60: ver 2 0 (tinfo 0xa83b2680)
D/EGL_emulation: eglMakeCurrent: 0xa5a70f60: ver 2 0 (tinfo 0xa83b2680)
D/EGL_emulation: eglMakeCurrent: 0xa5a70f60: ver 2 0 (tinfo 0xa83b2680)
E/MediaPlayerNative: stop called in state 1, mPlayer(0x0)
E/MediaPlayerNative: error (-38, 0)
V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@89d8a5e): Cookies: null
V/MediaHTTPService: makeHTTPConnection: CookieManager created: java.net.CookieManager@a4007a4
V/MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@89d8a5e): cookieHandler: java.net.CookieManager@a4007a4 Cookies: null
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/Choreographer: Skipped 163 frames!  The application may be doing too much work on its main thread.
E/MediaPlayer: Error (-38,0)
I/zygote: Do partial code cache collection, code=123KB, data=81KB
I/zygote: After code cache collection, code=123KB, data=81KB
I/zygote: Increasing code cache capacity to 512KB
V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@7615caf): Cookies: null
E/MediaPlayerNative: attachNewPlayer called in state 64
E/InputEventReceiver: Exception dispatching input event.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.lang.IllegalStateException
                        at android.media.MediaPlayer.nativeSetDataSource(Native Method)
                        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1172)
                        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1160)
                        at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1127)
                        at com.example.brads.group08.AudioActivity$1.onItemClick(AudioActivity.java:65)
                        at android.widget.AdapterView.performItemClick(AdapterView.java:318)
                        at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
                        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3127)
                        at android.widget.AbsListView.onTouchUp(AbsListView.java:4054)
                        at android.widget.AbsListView.onTouchEvent(AbsListView.java:3813)
                        at android.view.View.dispatchTouchEvent(View.java:11776)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2962)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2643)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:448)
                        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1829)
                        at android.app.Activity.dispatchTouchEvent(Activity.java:3307)
                        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:410)
                        at android.view.View.dispatchPointerEvent(View.java:12015)
                        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4795)
                        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4609)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
                        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4293)
                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
                        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4350)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
                        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
                        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
                        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6661)
                        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6635)
                        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6596)
                        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6764)
                        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
                        at android.os.MessageQueue.nativePollOnce(Native Method)
                        at android.os.MessageQueue.next(MessageQueue.java:325)
                        at android.os.Looper.loop(Looper.java:142)
                        at android.app.ActivityThread.main(ActivityThread.java:6494)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                    	at com.android
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.brads.group08, PID: 10444
                  java.lang.IllegalStateException
                      at android.media.MediaPlayer.nativeSetDataSource(Native Method)
                      at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1172)
                      at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1160)
                      at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1127)
                      at com.example.brads.group08.AudioActivity$1.onItemClick(AudioActivity.java:65)
                      at android.widget.AdapterView.performItemClick(AdapterView.java:318)
                      at android.widget.AbsListView.performItemClick(AbsListView.java:1158)
                      at android.widget.AbsListView$PerformClick.run(AbsListView.java:3127)
                      at android.widget.AbsListView.onTouchUp(AbsListView.java:4054)
                      at android.widget.AbsListView.onTouchEvent(AbsListView.java:3813)
                      at android.view.View.dispatchTouchEvent(View.java:11776)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2962)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2643)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                      at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
                      at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
                      at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:448)
                      at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1829)
                      at android.app.Activity.dispatchTouchEvent(Activity.java:3307)
                      at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:68)
                      at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:410)
                      at android.view.View.dispatchPointerEvent(View.java:12015)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4795)
                      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4609)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
                      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4293)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
                      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4350)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
                      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
                      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
                      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
                      at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6661)
                      at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6635)
                      at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6596)
                      at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6764)
                      at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
                      at android.os.MessageQueue.nativePollOnce(Native Method)
                      at android.os.MessageQueue.next(MessageQueue.java:325)
                      at android.os.Looper.loop(Looper.java:142)
                      at android.app.ActivityThread.main(ActivityThread.java:6494)
                      at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

0 个答案:

没有答案