MediaPlayer OnClicListener在开始播放新播放器时停止

时间:2012-08-22 16:31:34

标签: java android media-player

我正在做一个音板应用程序,这是我必须修复的最后一件事,谢谢你的帮助! 我希望声音停止然后我点击一个新声音。

package com.passaros;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.google.ads.AdRequest;    
import com.google.ads.AdView;
import com.murdersquirrel.newboard.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;    
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

public class newBoard extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    Toast.makeText(this, "Long touch to save as Ringtones.", Toast.LENGTH_LONG).show();

    AdView layout = (AdView)this.findViewById(R.id.adView);


    AdRequest adRequest = new AdRequest();
    adRequest.setTesting(true);

    layout.loadAd(adRequest);


    final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
    final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02);
//more and more....


    Button s01 = (Button) findViewById(R.id.button01); 
    s01.setText(this.getString(R.string.quote01));
    s01.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound01.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sound01.start();                
            }

    });
    registerForContextMenu(s01);

再次感谢!

忽略这一点:我只是在打字,因为网站认为我没有用足够的词来帮助代码......

2 个答案:

答案 0 :(得分:0)

试试这个......

public void onClick(View v) {
            try {
                sound01.prepare();
                sound01.start();    // Place your start() in try block.

            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            }

    });

答案 1 :(得分:0)

尝试做一个检查声音是否正在播放的简单方法,如果是真的话,停止声音并允许播放新声音怎么样?

此外,您只能使用一个媒体播放器,因为您只需切换正在播放的声音即可。