我正在为我的Android开发类工作的一个项目是构建一个棒球应用程序。我想将播放器的walking歌曲添加到单选按钮,将统计信息添加到另一个按钮。我已经完成了总体布局,但是我在播放mp3时遇到了问题。我昨天晚上和教练一起花了大约2个小时和时间,并且无法找到问题。我知道有一个比我聪明的人的世界,所以你能提供的任何帮助都会很精彩。
Carpenter.class
package net.androidbootcamp.cardinals;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
public class Carpenter extends Activity {
RadioButton btmarp;
int playing;
MediaPlayer mpmarp;
Button.OnClickListener bmarp = (new Button.OnClickListener);
protected void onCreate(Bundle savedInstanceState) {
//TODO
super.onCreate(savedInstanceState);
setContentView(R.layout.carpenter);
btmarp.setOnClickListener(bmarp);
btmarp = (RadioButton) findViewById(R.id.btnMarp);
mpmarp = new MediaPlayer();
mpmarp = MediaPlayer.create(this, R.raw.marp);
playing = 0;
Button btnmarp = (Button)findViewById(R.id.btnCarpenter);
btnmarp.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.baseball-reference.com/players/c/carpema01.shtml"));
startActivity(intent);
}
});
@Override
public void onClick(View v) {
//Todo
switch (playing) {
case 0:
mpmarp.start();
playing = 1;
btmarp.setText("Long Hot Summer Day - Turnpike Troubadours");
break;
case 1:
mpmarp.pause();
playing = 0;
btmarp.setText("Long Hot Summer Day - Turnpike Troubadours");
break;
}
};
}
}
Carpenter.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@mipmap/cardsbackground2">
<ImageView
android:contentDescription="@string/marp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@mipmap/marp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="90dp"
android:layout_marginStart="90dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnText"
android:id="@+id/btnCarpenter"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnMarp"
android:id="@+id/radCarpenter"
android:layout_marginTop="53dp"
android:layout_below="@+id/btnCarpenter"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
答案 0 :(得分:1)
这就是我为声音所做的。希望它有所帮助...
public class MainActivity extends Activity {
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton button1 = (ImageButton) findViewById(R.id.button1);
ImageButton button2 = (ImageButton) findViewById(R.id.button2);
ImageButton button3 = (ImageButton) findViewById(R.id.button3);
ImageButton button4 = (ImageButton) findViewById(R.id.button4);
ImageButton button5 = (ImageButton) findViewById(R.id.button5);
ImageButton button6 = (ImageButton) findViewById(R.id.button6);
final ImageButton stopButton = (ImageButton) findViewById(R.id.stopButton);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player = MediaPlayer.create(MainActivity.this, R.raw.gijane1);
player.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player = MediaPlayer.create(MainActivity.this, R.raw.rounds);
player.start();
}
});