我正在制作一个适用于Android的应用程序,并且已经制作了播放和暂停按钮。我的播放按钮和暂停按钮已经设置好了,所以当我单击播放或暂停按钮时,它会先运行,但是在按钮2中却不起作用,代码如下。
MainActivity.java
package com.example.cdp.keroncong;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.media.MediaPlayer;
import android.view.View;
public class MainActivity extends Activity {
MediaPlayer mplayer;
public void playAudio(View view) {
mplayer.start();
}
public void pauseAudio(View view) {
mplayer.stop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mplayer = MediaPlayer.create(this, R.raw.Button1);
//mplayer = MediaPlayer.create(this, R.raw.Button1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
感谢之前
答案 0 :(得分:0)
这是我的activity_main.xml
**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"
android:id="@+id/relativeLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/orange"
android:layout_below="@+id/textView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="215dp"
android:onClick="playAudio" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/playButton"
android:layout_below="@+id/playButton"
android:onClick="pauseAudio"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="@+id/banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/orange"
android:layout_below="@+id/orange"
android:onClick="playAudio"
android:text="Play"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/orange"
android:onClick="pauseAudio"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>
答案 1 :(得分:0)
尝试此代码,它将为您提供帮助
public class MainActivity extends AppCompatActivity {
MediaPlayer Song;
int pause;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
public void play(View view){
if(Song == null){
Song = MediaPlayer.create(this , R.raw.music);
Song.start();
Toast.makeText(MainActivity.this, "Song Play", Toast.LENGTH_SHORT).show(); }
else if(!Song.isPlaying()){
Song.seekTo(pause);
Song.start();
Toast.makeText(MainActivity.this, "Song Play", Toast.LENGTH_SHORT).show(); }
}
public void pause(View view){
if(Song!= null){
Song.pause();
pause = Song.getCurrentPosition();
Toast.makeText(MainActivity.this, "Song Pause", Toast.LENGTH_SHORT).show();
}
}
public void stop(View view){
Song.stop();
Song = null;
Toast.makeText(MainActivity.this, "Song Stop", Toast.LENGTH_SHORT).show();
}
}
xml.file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Play"
android:layout_gravity="center_horizontal"
android:background="@drawable/play"
android:onClick="play" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pause"
android:layout_gravity="center_horizontal"
android:background="@drawable/pause"
android:onClick="pause" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:layout_gravity="center_horizontal"
android:background="@drawable/stop"
android:onClick="stop" />
让我知道它是否对您有帮助
答案 2 :(得分:0)
尝试一下,将其放置在mplayer = MediaPlayer.create(this, R.raw.Button1);
// Play song
Button playButton = (Button) findViewById(R.id.playButton);
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mPlayer.start();
}
});
// Pause song
Button pauseButton = (Button) findViewById(R.id.pauseButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mPlayer.pause();
}
});
我将您的XML修改为如下所示,尽管直到看到全部内容我才能完全纠正它
<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/orange"
android:layout_below="@+id/orange"
android:text="Play"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/orange"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>