无法将 int from button if / else case 传递给媒体播放器

Can't pass an int from button if / else case to media player

你应该如何让媒体播放器知道它应该播放什么?

wholeTextPlayer = MediaPlayer.create(Lesson1Reading.this, engAu);

如果我在顶部声明文件就可以正常工作:

MediaPlayer wholeTextPlayer;
private int engAu = R.raw.l1r_en_l10;
Button btn_default_acc_whole;

在按钮点击 if / else 语句中无论我尝试使用以下组合放置它都不起作用:

MediaPlayer wholeTextPlayer;
private int engAu;
Button btn_default_acc_whole;

点击按钮:

final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
    btn_default_acc_whole.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            if (wholeTextPlayer.isPlaying()) {
                wholeTextPlayer.pause();

            } else {
                wholeTextPlayer.start();
                startPlayProgressUpdater();
            }
            setEngAu(R.raw.l1r_en_l10); //this line doesn't want to fit anywhere
        }
    });

setter:

public void setEngAu(int engAu) {
        this.engAu = engAu;
    }

当然它们是分开放在activity里的,我只是从里面复制粘贴了相关的位

谢谢大家。

完整代码如下:

'public class Lesson1Grammar extends Activity {

private SeekBar seekBar;
MediaPlayer wholeTextPlayer;
private int engAu;
private final Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lesson1grammar);
    WholeDefAccPlayer();

    final RelativeLayout playerScreen = (RelativeLayout)findViewById(R.id.playerScreen);

    final ImageButton btn_player_screen = (ImageButton) findViewById(R.id.btn_player_screen);
    btn_player_screen.setOnClickListener(new View.OnClickListener() {

        //this hides/unhides the part of the layout in which the player is
        @Override
        public void onClick(View arg0) {
            if (playerScreen.isShown()) {
                playerScreen.setVisibility(View.GONE);
            } else {
                playerScreen.setVisibility(View.VISIBLE);
            }
        }
    });


    final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
    btn_default_acc_whole.setOnClickListener(new View.OnClickListener() {



        public void onClick(View arg0) {
            if (wholeTextPlayer.isPlaying()) {
                wholeTextPlayer.pause();
            } else {
                setEngAu(R.raw.default_acc_audio);
                wholeTextPlayer.start();
                startPlayProgressUpdater();
            }
        }
    });
}

public void setEngAu(int engAu) {
    this.engAu = engAu;
}

private void WholeDefAccPlayer() {

    wholeTextPlayer = MediaPlayer.create(Lesson1Grammar.this, engAu);
    ((TextView) findViewById(R.id.getTitleOfAccent)).setText(R.string.btn_lesson1reading);

    seekBar = (SeekBar) findViewById(R.id.seekBar);
    seekBar.setMax(wholeTextPlayer.getDuration());
    seekBar.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            seekChange(v);
            return false;
        }
    });
}

public void startPlayProgressUpdater() {
    seekBar.setProgress(wholeTextPlayer.getCurrentPosition());

    if (wholeTextPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();
            }
        };
        handler.postDelayed(notification, 1000);
    }
    else wholeTextPlayer.pause();
}

// This is event handler thumb moving event
private void seekChange(View v){
    if(wholeTextPlayer.isPlaying()){
        SeekBar sb = (SeekBar)v;
        wholeTextPlayer.seekTo(sb.getProgress());
    }
}

} '

如果您的计划是使方法 setEngAu() 成为您在评论中提到的控制器。那么你需要像这样使用那个方法

public void setEngAu(int enAu)
{
this.EngAu = enAu;
wholeTextPlayer.setDataSource(engAu);
wholeTextPlayer.prepare();
}

您需要从媒体播放器实现 onPrepared 监听器 我不知道你的 类 但我假设你在哪里说了类似的话;

wholeTextPlayer = new MediaPlayer();
wholeTextPlayer.setOnPreparedListener(this);

准备就绪后启动播放器

 public void onPrepared(MediaPlayer player)
    {
       player.start();
    }

请记住,如果您不想让播放器再保留该资源,您将需要调用 wholeTextPlayer.release()(将其视为内存问题 - 如果您查看文档,建议这样做)

编辑: 我稍微调整了你的代码,请看看并告诉我。

private SeekBar seekBar;
MediaPlayer wholeTextPlayer;
private int engAu;
 final Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lesson1grammar);
        WholeDefAccPlayer();

        final RelativeLayout playerScreen = (RelativeLayout) findViewById(R.id.playerScreen);

        final ImageButton btn_player_screen = (ImageButton) findViewById(R.id.btn_player_screen);
        btn_player_screen.setOnClickListener(new View.OnClickListener()
        {
            //this hides/unhides the part of the layout in which the player is
            @Override
            public void onClick(View arg0)
            {
                if(playerScreen.isShown())
                {
                    playerScreen.setVisibility(View.GONE);
                }
                else
                {
                    playerScreen.setVisibility(View.VISIBLE);
                }
            }
        });


        final Button btn_default_acc_whole = (Button) findViewById(R.id.btn_default_acc_whole);
        btn_default_acc_whole.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View arg0)
            {//problem here is
                if(playerState == PlayerState_Playing)
                {
                    wholeTextPlayer.pause();
                    setPlayerState(PlayerState_Paused);
                }
                else if(playerState == PlayerState_Paused)
                {
                    wholeTextPlayer.start();
                    setPlayerState(PlayerState_Playing);
                }
                else
                {
                    setEngAu(R.raw.default_acc_audio);
                    wholeTextPlayer.start();
                    startPlayProgressUpdater();
                    setPlayerState(PlayerState_Playing);
                }
            }
        });
    }

    public void setEngAu(int engAu)
    {
        this.engAu = engAu;
        if(wholeTextPlayer !=null)
        {//just in case you call this method and player was not initialized yet
            wholeTextPlayer.release();
        }

        setPlayerState(PlayerState_Preparing);
        wholeTextPlayer = MediaPlayer.create(this, engAu);
    }

    private void WholeDefAccPlayer()
    {
        //this line probably will fail because engAu is not really initialized yet, unless you have default value for it
        wholeTextPlayer = MediaPlayer.create(this, engAu);
        ((TextView) findViewById(R.id.getTitleOfAccent)).setText(R.string.btn_lesson1reading);

        seekBar = (SeekBar) findViewById(R.id.seekBar);

        //you can not call getDuration unless the player is prepared, so this might crash you
//        seekBar.setMax(wholeTextPlayer.getDuration());

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
        {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b)
            {
                if(playerState != PlayerState_Preparing)
                {//if player state is preparing it means we can not seek yet, player.start() must be called first
                    wholeTextPlayer.seekTo(i);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar)
            {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar)
            {

            }
        });
    }

    public void startPlayProgressUpdater()
    {
        if(seekBar.getMax() != wholeTextPlayer.getDuration())
        {//in case you change track later, this will check if the seek bar max value with track duration.
            // if they are not the same, then it will adjust it for you
            seekBar.setMax(wholeTextPlayer.getDuration());
        }

        seekBar.setProgress(wholeTextPlayer.getCurrentPosition());

        if(wholeTextPlayer.isPlaying())
        {
            Runnable notification = new Runnable()
            {
                public void run()
                {
                    startPlayProgressUpdater();
                }
            };
            handler.postDelayed(notification, 1000);
        }
        else wholeTextPlayer.pause();
    }

    // This is event handler thumb moving event
    private void seekChange(View v)
    {
        if(wholeTextPlayer.isPlaying())
        {
            SeekBar sb = (SeekBar) v;
            wholeTextPlayer.seekTo(sb.getProgress());
        }
    }

    private final int PlayerState_Preparing = 0;
    private final int PlayerState_Playing = 1;
    private final int PlayerState_Paused = 2;

    private int playerState;
    private void setPlayerState(int state)
    {//this is used to track the player state, because wholeTextPlayer.isPlaying() can return false in many conditions ( too general )
        //you can check in the media player documentation to know more details about this
        playerState = state;
    }