我使用一个布局和两个按钮构建一个简单的应用程序,这是我的代码..
package com.example.tessound;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener
{
MediaPlayer player;
Button play,mute;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button)findViewById(R.id.button1);
play.setOnClickListener(this);
mute = (Button)findViewById(R.id.button2);
mute.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View view)
{
if(view.getId()==R.id.button1)
{
playSound(1);
}
else if(view.getId()==R.id.button2)
{
playSound(2);
}
}
public void playSound(int arg)
{
try
{
if(player != null)
{
if (player.isPlaying())
{
player.stop();
player.release();
}
}
}
catch(Exception e)
{
}
if (arg == 1)
{
player = MediaPlayer.create(this, R.raw.atur);
}
else if (arg == 2)
{
player = MediaPlayer.create(this, R.raw.back);
}
if(player != null)
{
player.setLooping(false);
player.start();
}
}
}
我的原始文件夹包含: atur.mp3,back.mp3,belajar.mp3,level.mp3,skip.mp3和start.mp3 ...... 当我点击按钮静音时,会播放back.mp3,但是当我点击按钮播放时,原始文件夹中的所有mp3都会被播放..有任何评论吗?
答案 0 :(得分:0)
我复制了你的代码并进行了测试。在我的系统中它工作正常。我刚刚重命名歌曲和按钮ID,因为那些原始歌曲我无法得到。请看这段代码,代码是一样的,因为我刚刚复制了这段代码:
package com.mukherjee.satyaki.test;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity1 extends Activity implements OnClickListener
{
MediaPlayer player;
Button play,mute;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (Button)findViewById(R.id.songid1);
play.setOnClickListener(this);
mute = (Button)findViewById(R.id.songid2);
mute.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View view)
{
if(view.getId()==R.id.songid1)
{
playSound(1);
}
else if(view.getId()==R.id.songid2)
{
playSound(2);
}
}
public void playSound(int arg)
{
try
{
if(player != null)
{
if (player.isPlaying())
{
player.stop();
player.release();
}
}
}
catch(Exception e)
{
}
if (arg == 1)
{
player = MediaPlayer.create(this, R.raw.ram1);
}
else if (arg == 2)
{
player = MediaPlayer.create(this, R.raw.ram3);
}
if(player != null)
{
player.setLooping(false);
player.start();
}
}
}
您的代码很好,根据我之前的假设重新启动模拟器并擦除用户数据然后进行测试。我认为问题将通过您的代码解决。
感谢。
答案 1 :(得分:0)
我记得有类似的问题,我的代码最终看起来像这样:
if (!getmMediaPlayer().isPlaying()) {
try {
AssetFileDescriptor fd = getResources().openRawResourceFd(
R.raw.snooze);
getmMediaPlayer().setDataSource(fd.getFileDescriptor(),
fd.getStartOffset(), fd.getLength());
getmMediaPlayer().setAudioStreamType(
AudioManager.STREAM_ALARM);
getmMediaPlayer().setLooping(true);
getmMediaPlayer().prepare();
getmMediaPlayer().start();
} catch (IllegalStateException e) {
Log.e(TAG, e.toString());
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
具体来说,我认为这是重要的setDataSource。