【问题标题】:Android MediaPlayer not playing audio properlyAndroid MediaPlayer 无法正确播放音频
【发布时间】:2014-01-27 07:35:39
【问题描述】:

我的应用正在播放原始文件夹中的 5 首不同歌曲,我使用列表视图和数组适配器列出了这些歌曲。我的代码的问题是当我播放任何歌曲时它工作正常然后我从列表中的任何地方播放另一首歌曲但是当我想播放第三首歌曲时它不会播放但我之前播放的前两首歌曲可以播放和暂停。我以许多不同的方式尝试了这个问题,但都是徒劳的。下面是我包含的代码。

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();
    }


}

这里是 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)

【问题讨论】:

  • 你能发布你的 log-cat 错误吗..
  • 请检查我发布的 logcat 错误

标签: java android audio android-listview android-mediaplayer


【解决方案1】:

请参考我对这个问题的回答:Problems with Media Player

大多数情况下,您可能需要检查每首歌曲是否单独播放。如果是,你应该停止它。问题可能在于您检查是否有任何其他媒体播放器正在播放,并暂停所有,而不管哪个媒体播放器已停止。

也就是说,你应该检查一个,如果它正在播放,就停止它,一个接一个,就像我链接的那个帖子一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多