java - 我怎样才能在android中播放声音
问题描述
我写了 simplw android 程序来播放声音,但它不工作。不知道为什么。也许这与我在 mymp3 文件(在原始目录下)附近有问号有关。我尝试了几个没有任何 suseess 的选项。
下面附上我的代码,在此先感谢。xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:background="#CFEC89"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="200dp"
android:layout_height="80dp"
android:id="@+id/sound"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_gravity="center"
android:text="Sound"
android:textSize="30sp"
android:background="#EAE3E3"
/>
</LinearLayout>
主要活动java
package com.example.playsound;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
// define my variables
Button sound;
final MediaPlayer mp = MediaPlayer.create(this,R.raw.cat);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v)
{
mp.start();
}
}
和我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playsound">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PlaySound">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
解决方案
首先,删除你的TextView
它可能覆盖你的Button
. 这就是为什么您可能无法单击它的原因。
将其粘贴到您的课程中:
package com.example.playsound;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.cat);
final Button sound = (Button) this.findViewById(R.id.sound);
sound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
}
}
对于未来,您需要在OnCreate
上面而不是上面定义变量。
推荐阅读
- php - GitLab CI shell 运行程序不继承系统环境变量
- node.js - objection.js modifyEager .as('count') 似乎对 .count() 没有任何作用
- django - 使用 mod_wsgi 在 apache 上部署 django 应用程序时出现“没有名为“编码”的模块”错误
- reporting-services - SSRS 报告列问题
- c# - XmlSerializer 在反序列化时抛出 StackOverflowException,但前提是它是从 ASMX Web 服务调用的
- python - 具有自定义匹配功能的 Python 序列匹配器
- apollo - Apollo CodeGen 无法生成 TS 类型
- javascript - 如何将函数转换为等待 REST 请求
- javascript - 无法从 jQuery 对象中轻松访问和重新渲染元素
- java - 我可以在控制器级别添加请求参数并将其转发到另一个网站吗?