Android Studio媒体播放器播放和暂停

时间:2016-02-09 12:15:21

标签: android android-layout android-fragments android-intent android-mediaplayer

早安/女士,所以我能够在onCreate播放音乐。但我的问题是我需要在单击按钮时停止音乐。问题是当我在案例

中键入mpBM.stop()
r.id.BUTTONNAME:
mpBm.stop();
 break;

mpBM.stop();有错误,它正好在mpBM上我认为该命令无法调用:

 MediaPlayer mpBM = MediaPlayer.create(R.raw.ceusong); 

我在on create中声明。当我尝试将其放在onCreate之外并将其放在LinearLayout ll;顶部onCreate之后?它在声明中有错误:

MediaPlayer mpBM = MediaPlayer.create(R.raw.ceusong);

有谁能告诉我解决方案?

顺便说一下,当我按下音乐任何按钮时,我真正想要的是什么。

package org.intercode.lifeatceu;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


public class browse extends AppCompatActivity implements View.OnClickListener {
TextView presidentmessage, foreword, missionvision, qualitypolicyandobjectives, expectedgraduateattributes, briefhistory,
        fus, degreeprogramofferings, tvAAEP, tvPFOT,tvSGAOFOFA,tvSSAF,tvUSRP,tvCAH,tvEARSA,tvUOAEO,tvCEUMANILA,tvCEUMAKATI
        ,tvCEUMALOLOS,tvAPPEN,tvSCAO,tvPUBLICATIONS,tvCEAAR,tvPOFMOSCDO,tvGR;
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_browse);

    presidentmessage = (TextView) findViewById(R.id.presidentmessage);
    foreword = (TextView) findViewById(R.id.foreword);
    missionvision = (TextView) findViewById(R.id.missionvision);
    qualitypolicyandobjectives = (TextView) findViewById(R.id.qualitypolicyandobjectives);
    expectedgraduateattributes = (TextView) findViewById(R.id.expectedgraduateattributes);
    briefhistory = (TextView) findViewById(R.id.briefhistory);
    fus = (TextView) findViewById(R.id.fus);
    degreeprogramofferings = (TextView) findViewById(R.id.degreeprogramofferings);
    tvAAEP = (TextView) findViewById(R.id.tvAAEP);
    tvPFOT = (TextView) findViewById(R.id.tvPFOT);
    tvSGAOFOFA = (TextView) findViewById(R.id.tvSGAOFOFA);
    tvSSAF = (TextView) findViewById(R.id.tvSSAF);
    tvUSRP = (TextView) findViewById(R.id.tvUSRP);
    tvCAH = (TextView) findViewById(R.id.tvCAH);
    tvEARSA = (TextView) findViewById(R.id.tvEARSA);
    tvUOAEO = (TextView) findViewById(R.id.tvUOAEO);
    tvCEUMANILA = (TextView) findViewById(R.id.tvCEUMANILA);
    tvCEUMAKATI = (TextView) findViewById(R.id.tvCEUMAKATI);
    tvCEUMALOLOS = (TextView) findViewById(R.id.tvCEUMALOLOS);
    tvAPPEN = (TextView) findViewById(R.id.tvAPPEN);
    tvSCAO = (TextView) findViewById(R.id.tvSCAO);
    tvPUBLICATIONS = (TextView) findViewById(R.id.tvPUBLICATIONS);
    tvCEAAR = (TextView) findViewById(R.id.tvCEAAR);
    tvPOFMOSCDO = (TextView) findViewById(R.id.tvPOFMOSCDO);
    tvGR = (TextView) findViewById(R.id.tvGR);

    presidentmessage.setOnClickListener(this);
    foreword.setOnClickListener(this);
    missionvision.setOnClickListener(this);
    qualitypolicyandobjectives.setOnClickListener(this);
    expectedgraduateattributes.setOnClickListener(this);
    briefhistory.setOnClickListener(this);
    fus.setOnClickListener(this);
    degreeprogramofferings.setOnClickListener(this);
    tvAAEP.setOnClickListener(this);
    tvPFOT.setOnClickListener(this);
    tvSGAOFOFA.setOnClickListener(this);
    tvSSAF.setOnClickListener(this);
    tvUSRP.setOnClickListener(this);
    tvCAH.setOnClickListener(this);
    tvEARSA.setOnClickListener(this);
    tvUOAEO.setOnClickListener(this);
    tvCEUMANILA.setOnClickListener(this);
    tvCEUMAKATI.setOnClickListener(this);
    tvCEUMALOLOS.setOnClickListener(this);
    tvAPPEN.setOnClickListener(this);
    tvSCAO.setOnClickListener(this);
    tvPUBLICATIONS.setOnClickListener(this);
    tvCEAAR.setOnClickListener(this);
    tvPOFMOSCDO.setOnClickListener(this);
    tvGR.setOnClickListener(this);
    final MediaPlayer mpBM = MediaPlayer.create(this,R.raw.ceusong);
    mpBM.start();

}
@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.presidentmessage:
            startActivity(new Intent(this, pm.class));
            break;
        case R.id.foreword:
            startActivity(new Intent(this, F.class));
            break;
        case R.id.missionvision:
            startActivity(new Intent(this, cvmgacv.class));
            break;
        case R.id.qualitypolicyandobjectives:
            startActivity(new Intent(this, cqpao.class));
            break;
        case R.id.expectedgraduateattributes:
            startActivity(new Intent(this, ceega.class));
            break;
        case R.id.briefhistory:
            startActivity(new Intent(this, ceuabh.class));
            break;
        case R.id.fus:
            startActivity(new Intent(this, fus.class));
            break;
        case R.id.degreeprogramofferings:
            startActivity(new Intent(this, fus.class));
            break;
        case R.id.tvAAEP:
            startActivity(new Intent(this, aaep.class));
            break;
        case R.id.tvPFOT:
            startActivity(new Intent(this, PFOT.class));
            break;
        case R.id.tvSGAOFOFA:
            startActivity(new Intent(this, SGAOFOFA.class));
            break;
        case R.id.tvSSAF:
            startActivity(new Intent(this, SSAF.class));
            break;
        case R.id.tvUSRP:
            startActivity(new Intent(this, USRP.class));
            break;
        case R.id.tvCAH:
            startActivity(new Intent(this, CAH.class));
            break;
        case R.id.tvEARSA:
            startActivity(new Intent(this, EARSA.class));
            break;
        case R.id.tvUOAEO:
            startActivity(new Intent(this,UOAEO.class));
            break;
        case R.id.tvCEUMANILA:
            startActivity(new Intent(this,CEUMANILA.class));
            break;
        case R.id.tvCEUMAKATI:
            startActivity(new Intent(this,CEUMAKATI.class));
            break;
        case R.id.tvCEUMALOLOS:
            startActivity(new Intent(this,CEUMALOLOS.class));
            break;
        case R.id.tvAPPEN:
            startActivity(new Intent(this,Appendices.class));
            break;
        case R.id.tvSCAO:
            startActivity(new Intent(this, Appendices.class));
            break;
        case R.id.tvPUBLICATIONS:
            startActivity(new Intent(this, PUBLICATION.class));
            break;
        case R.id.tvCEAAR:
            startActivity(new Intent(this, CEAAR.class));
            break;
        case R.id.tvPOFMOSCDO:
            startActivity(new Intent(this, POFMOSCDO.class));
            break;
        case R.id.tvGR:
            startActivity(new Intent(this, GR.class));
            break;
    }
}
}

1 个答案:

答案 0 :(得分:0)

将mpBM的声明移动为活动类的成员,而不是onCreate()中的局部变量,以便在其他方法中可见。