簡體   English   中英

Android MediaPlayer無法正確播放音頻

[英]Android MediaPlayer not playing audio properly

我的應用程序正在播放Raw文件夾中的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)

請參閱我在以下問題中的回答: Media Player問題

通常,您可能需要檢查每首歌曲是否在單獨播放。 如果是這樣,您應該停止它。 問題可能出在您檢查其他任何媒體播放器是否正在播放,並暫停所有 媒體播放器而與停止哪個媒體播放器無關的方式上。

也就是說,您應該檢查一個,如果正在播放則停止它, 一個接一個地檢查,就像我鏈接的那篇文章一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM