简体   繁体   English

Android MediaPlayer无法正确播放音频

[英]Android MediaPlayer not playing audio properly

My app is playing 5 different songs from raw folder, I listed the songs using list-View and array adapter. 我的应用程序正在播放Raw文件夹中的5首不同的歌曲,我使用列表视图和数组适配器列出了这些歌曲。 the problem with my code is it works fine when I play any song then I play another song anywhere from list but when I want to play third song it won't play but the first two songs I played before is can be played and paused. 我的代码的问题是,当我播放任何歌曲然后在列表中的任意位置播放另一首歌曲时,它都可以正常工作,但是当我想播放第三首歌曲时,它不会播放,但是我之前播放的前两首歌曲可以播放和暂停。 I tried this problems in many different way but all in vain. 我以许多不同的方式尝试了这个问题,但徒劳无功。 below is the code i included. 下面是我包含的代码。

package khan.panizai.adele21;


import java.util.List;


import android.app.ListActivity;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class MusicActivity extends ListActivity {

    MediaPlayer s1,s2,s3,s4,s5;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.music_layout);

        Log.d("Saoud123", "music activity is called");

        s1 = MediaPlayer.create(MusicActivity.this, R.raw.s1_rolling_in_the_deep);

        s2 = MediaPlayer.create(MusicActivity.this, R.raw.s2_turning_tabels);

        s3 = MediaPlayer.create(MusicActivity.this, R.raw.s3_dont_you_remember);

        s4 = MediaPlayer.create(MusicActivity.this, R.raw.s4_set_fire_to_the_rain);

        s5 = MediaPlayer.create(MusicActivity.this, R.raw.s5_someone_like_you);




        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 

                 getResources().getStringArray(R.array.songs_list)));


    }


    @Override

    protected void onListItemClick(ListView l, View v, int position, long id) {


        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);



        if(getListAdapter().getItem(position).equals("Rolling in the deep")){


            //stop other songs and play this
            if(s2.isPlaying()||s3.isPlaying()||s4.isPlaying()||s5.isPlaying()){
                s2.pause();s3.pause();s4.pause();s5.pause();

                s2.seekTo(0);s3.seekTo(0);s4.seekTo(0);s5.seekTo(0);
                }

                s1.start();

                s1.setLooping(true);    
        }


        else if(getListAdapter().getItem(position).equals("Turning tables")){

            //stop other songs and play this

            if(s1.isPlaying()||s3.isPlaying()||s4.isPlaying()||s5.isPlaying()){

                s1.pause();s3.pause();s4.pause();s5.pause();

                s1.seekTo(0);s3.seekTo(0);s4.seekTo(0);s5.seekTo(0);

                }

                s2.start();


                s2.setLooping(true);    
        }

else if(getListAdapter().getItem(position).equals("Dont you remember")){

            //stop other songs and play this

            if(s2.isPlaying()||s1.isPlaying()||s4.isPlaying()||s5.isPlaying()){

                s2.pause();s1.pause();s4.pause();s5.pause();

                s2.seekTo(0);s1.seekTo(0);s4.seekTo(0);s5.seekTo(0);

                }

                s3.start();


                s3.setLooping(true);    
        }

else if(getListAdapter().getItem(position).equals("Set fire to the rain")){

            //stop other songs and play this

            if(s2.isPlaying()||s3.isPlaying()||s1.isPlaying()||s5.isPlaying()){

                s2.pause();s3.pause();s1.pause();s5.pause();

                s2.seekTo(0);s3.seekTo(0);s1.seekTo(0);s5.seekTo(0);

                }

                s4.start();


                s4.setLooping(true);    
        }

        else if(getListAdapter().getItem(position).equals("Someone like you")){

            //stop other songs and play this

            if(s2.isPlaying()||s3.isPlaying()||s4.isPlaying()||s1.isPlaying()){

                s2.pause();s3.pause();s4.pause();s1.pause();

                s2.seekTo(0);s3.seekTo(0);s4.seekTo(0);s1.seekTo(0);

                }
                s5.start();



                s5.setLooping(true);    
        }


    }

    @Override

    public void onBackPressed() {
        if(s1.isPlaying()||s2.isPlaying()||s3.isPlaying()||s4.isPlaying()

                ||s5.isPlaying())

            s1.stop();s2.stop();s3.stop();s4.stop();s5.stop();

        super.onBackPressed();
    }

    @Override

    protected void onDestroy() {

        if(s1.isPlaying()||s2.isPlaying()||s3.isPlaying()||s4.isPlaying()
                ||s5.isPlaying())

            s1.release();s2.release();s3.release();s4.release();s5.release();

        super.onDestroy();
    }


}

here is the logcat error msgs 这是logcat错误消息

01-31 19:15:19.485: D/AbsListView(9369): setCacheColorHint current in default theme.

01-31 19:15:19.485: D/Saoud123(9369): music activity is called

01-31 19:15:19.487: D/MediaPlayer(9369): mPlayerID = 128

01-31 19:15:19.509: D/MediaPlayer(9369): mPlayerID = 129

01-31 19:15:19.554: D/MediaPlayer(9369): mPlayerID = 130

01-31 19:15:19.590: D/MediaPlayer(9369): mPlayerID = 131

01-31 19:15:19.626: D/MediaPlayer(9369): mPlayerID = 132

01-31 19:15:20.336: I/MediaPlayer(9369): Duration update (duration=229392)

01-31 19:15:20.506: I/MediaPlayer(9369): Duration update (duration=250200)

01-31 19:15:20.556: I/MediaPlayer(9369): Duration update (duration=243288)

01-31 19:15:20.656: I/MediaPlayer(9369): Duration update (duration=241776)

01-31 19:15:20.663: I/MediaPlayer(9369): Duration update (duration=287136)

01-31 19:15:51.687: E/MediaPlayer(9369): pause called in state 8

01-31 19:15:51.688: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:51.688: E/MediaPlayer(9369): pause called in state 8

01-31 19:15:51.688: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:51.692: E/MediaPlayer(9369): pause called in state 8

01-31 19:15:51.692: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:51.692: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state: 

mPlayer=0x21a800, mCurrentState=0

01-31 19:15:51.692: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:51.692: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state: 
mPlayer=0x2a4a10, mCurrentState=0

01-31 19:15:51.692: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:51.693: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state: mPlayer=0x295ea0, mCurrentState=0

01-31 19:15:51.693: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:51.708: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:51.709: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:59.592: E/MediaPlayer(9369): pause called in state 0

01-31 19:15:59.592: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:59.592: E/MediaPlayer(9369): pause called in state 0

01-31 19:15:59.592: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:59.593: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state: 

mPlayer=0x2a4a10, mCurrentState=0
01-31 19:15:59.594: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:59.594: E/MediaPlayer(9369): Attempt to perform seekTo in wrong state: 
mPlayer=0x295ea0, mCurrentState=0

01-31 19:15:59.594: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:59.594: E/MediaPlayer(9369): start called in state 0

01-31 19:15:59.594: E/MediaPlayer(9369): error (-38, 0)

01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)

01-31 19:15:59.598: E/MediaPlayer(9369): Error (-38,0)

Refer to my answer in this question : Problems with Media Player 请参阅我在以下问题中的回答: Media Player问题

Mostly you might need to check if each song is playing individually . 通常,您可能需要检查每首歌曲是否在单独播放。 and if it is, you should stop it. 如果是这样,您应该停止它。 Problem might be in the way you have checked if any of the other media players are playing, and pausing all of them irrespective of which media player is stopped. 问题可能出在您检查其他任何媒体播放器是否正在播放,并暂停所有 媒体播放器而与停止哪个媒体播放器无关的方式上。

That is, you should check for one, and stop it if it is playing, one after the other like in that post I have linked. 也就是说,您应该检查一个,如果正在播放则停止它, 一个接一个地检查,就像我链接的那篇文章一样。

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

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