【问题标题】:Android Studio Media Player Play and PauseAndroid Studio 媒体播放器播放和暂停
【发布时间】:2016-02-09 12:15:20
【问题描述】:

先生/女士晚安,所以我可以在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;
    }
}
}

【问题讨论】:

  • mpBM 是 onCreate 本地的,因此在 onClick 中不可见。
  • 是的,当我将它移动到活动类的一部分时,活动将不会打开并且错误来自该声明
  • 所以移动声明,但将初始化保留在onCreate 中(您必须删除final)。

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


【解决方案1】:

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

【讨论】:

  • 是的,当我将它移动到活动类的一部分时,活动将不会打开并且错误来自该声明
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多