【发布时间】:2016-10-09 17:50:39
【问题描述】:
我是一个非常初学者,并试图制作我的第一个应用程序,按钮在按下时播放声音......我认为是一个音板。
E/MediaPlayer:应该已经设置了字幕控制器
E/MediaPlayer:错误 (262,0)
当我第二次点击按钮时:
E/MediaPlayer:错误 (-38,0)
这些是我用这个简单的代码得到的错误:
public class MainActivity extends AppCompatActivity {protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer m1 = MediaPlayer.create(getApplicationContext(), R.raw.s1);
Button b1 = (Button) findViewById(R.id.s1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m1.start();
}
});}
“s1.mp3”在原始文件夹中,大约 3-4 秒长,我认为这不是问题...... 我不明白我做错了什么 - 一个在线教程教会了我所有这些,有一个类似的代码...... 请帮助我,正如我所说,我是一名初学者开发人员,所以我需要帮助...... 提前谢谢!
更新:
public class MainActivity extends AppCompatActivity {
MediaPlayer m1;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m1 = MediaPlayer.create(MainActivity.this, R.raw.s1);
Button b1 = (Button) findViewById(R.id.s1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m1.start();
}
});}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.antiriad7.zviadi.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="25"
android:id="@+id/l1">
<Button
android:layout_width="0dp"
android:layout_weight="33"
android:text="S1"
android:id="@+id/s1"
android:layout_height="match_parent" />
<Button
android:layout_width="0dp"
android:layout_weight="34"
android:text="S2"
android:id="@+id/s2"
android:layout_height="match_parent" />
<Button
android:layout_width="0dp"
android:layout_weight="33"
android:text="S3"
android:id="@+id/s3"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="25"
android:layout_below="@+id/l1"
android:id="@+id/l2">
<Button
android:layout_width="0dp"
android:layout_weight="33"
android:text="S4"
android:id="@+id/s4"
android:layout_height="match_parent" />
<Button
android:layout_width="0dp"
android:layout_weight="34"
android:text="S5"
android:id="@+id/s5"
android:layout_height="match_parent" />
<Button
android:layout_width="0dp"
android:layout_weight="33"
android:text="S6"
android:id="@+id/s6"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_below="@+id/l2"
android:layout_weight="25"
android:id="@+id/l3">
<Button
android:layout_width="0dp"
android:layout_weight="33"
android:text="S7"
android:id="@+id/s7"
android:layout_height="match_parent" />
<Button
android:layout_width="0dp"
android:layout_weight="34"
android:text="S8"
android:id="@+id/s8"
android:layout_height="match_parent" />
<Button
android:layout_width="0dp"
android:layout_weight="33"
android:text="S9"
android:id="@+id/s9"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_below="@+id/l3"
android:layout_weight="25"
android:id="@+id/l4">
<Button
android:layout_width="0dp"
android:layout_weight="33"
android:text="S10"
android:id="@+id/s10"
android:layout_height="match_parent" />
<Button
android:layout_width="0dp"
android:layout_weight="34"
android:text="S11"
android:id="@+id/s11"
android:layout_height="match_parent" />
<Button
android:layout_width="0dp"
android:layout_weight="33"
android:text="S12"
android:id="@+id/s12"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
点击按钮时你的mp3歌曲是否正在播放??
-
不,它不播放...
-
查看我的更新答案