我是一個非常初學者,並試圖使我的第一個應用程序與按鈕,按下時會播放聲音...我認爲一個音板。MediaPlayer給出錯誤(262,0)
E/MediaPlayer的:應具有副標題控制器已經設置
E/MediaPlayer的:錯誤(262,0)
當我點擊按鈕第二時間:
E/MediaPlayer:錯誤(-38,0)
Thes e爲我這個簡單的代碼獲得錯誤:
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秒長,我認爲這不是問題...... 我不明白我在做什麼錯 - 一個在線教程告訴我所有這些,有一個類似的代碼... 請幫助我,正如我所說,我是一名初學者開發人員,所以我需要幫助... 先謝謝您!
UPDATE:
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歌曲播放與否按鈕點擊?? –
不,它不玩... – antiriad7
看我已更新回答 –