打开/关闭声音

时间:2016-09-23 12:39:26

标签: android android-mediaplayer

我想点击按钮打开和关闭声音。

 public void onClick(View v) {
     buttonSound.setBackgroundResource(R.drawable.soundan);
     CommonMethod.soundplayer.stop();

     //buttonSound.setBackgroundResource(R.drawable.soundaus);
     //CommonMethod.soundplayer.setVolume(0,0);
 }

这是CommonMethod

public class CommonMethod {

    public static MediaPlayer soundplayer;
    public static void Soundplayer(Context ctx,int raw_id)
    {
        soundplayer = MediaPlayer.create(ctx,raw_id);
        soundplayer.setLooping(true);
        soundplayer.setVolume(100, 100);
    }
}

通过点击停止声音,但它不会再次打开

我也得到了这个代码,但同样的问题

public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                if (counter % 2 == 0) {
                    buttonSound.setBackgroundResource(R.drawable.soundan);
                    CommonMethod.soundplayer.setVolume(100,100);

                }
                else
                {
                    buttonSound.setBackgroundResource(R.drawable.soundaus);
                    CommonMethod.soundplayer.setVolume(0,0);

                }

            }

2 个答案:

答案 0 :(得分:0)

请参阅documentation

这篇文章

有一些有用的选票如:

  

MediaPlayer对象必须先进入Prepared状态   可以开始播放。

  

一旦处于Stopped状态,直到prepare()才能开始播放   或者调用prepareAsync()来将MediaPlayer对象设置为   再次准备状态。

答案 1 :(得分:0)

你可以pause而不是stooping如果你停止,那么你再次prepaid然后它会play而不是pause并且可以玩if(mPlayer.isPlaying()){ mPlayer.pause(); } else { mPlayer.start(); } “再次来自同一个位置。

  $('td, a', '#MenuItem').each(function () {
    var cell = $(this);
    cell.width(cell.width());
});
$(document).ready(function () {
    $('#MenuItem tbody').sortable({
        axis: 'y',
        update: function (event, ui) {
            var data = $(this).sortable('serialize');

            // POST to server using $.post or $.ajax
            $.ajax({
                data: data,
                type: 'POST',
                url: 'MoveFunction'
            });
        }
    });
});

[HttpPost]

    public ActionResult MoveFunction(Product vm)
    {




        return View(vm);
    }



  public class Product
{
    //ID's
    [Key]
    public int ProductID { get; set; }

    //Members


 <fieldset class="fieldset">
        <legend class="legend">Products</legend>

        <table id = "MenuItem"  class="promo full-width alternate-rows" style="text-align: center;">  <!-- Cedric Kehi DEMO CHANGE -->



            <tr>
                <th>Product Code
                </th>
                <th>Product Template
                </th>
                @*<th>
                    @Html.DisplayNameFor(model => model.IndexList[0].Priority)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.IndexList[0].WindowProduct)
                </th>*@
                <th>Description <!-- JACK EDIT -->
                </th>
                <th>Actions</th>
            </tr>
            <tbody>
            @foreach (var item in Model.IndexList)
            {


                <tr>
                    <td class="center-text">
                        @Html.DisplayFor(modelItem => item.ProductCode)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ProductTemplate.Description)
                    </td>
                    @*<td class="center-text">
                        @Html.DisplayFor(modelItem => item.Priority)
                    </td>
                    <td class="center-text">
                        @Html.Raw(item.WindowProduct ? "Yes" : "No")
                    </td>*@
                    <td>
                        @Html.DisplayFor(modelItem => item.Description)
                    </td>


  <td class="center-text nowrap">
                        @Html.ActionLink(" ", "Edit", new { id = item.ProductID }, new { title = "Edit", @class = "anchor-icon-no-text edit" })
                        @Html.ActionLink(" ", "Details", new { id = item.ProductID }, new { title = "Details", @class = "anchor-icon-no-text details" })
                        @Html.ActionLink(" ", "Delete", new { id = item.ProductID }, new { title = "Delete", @class = "anchor-icon-no-text delete" })
                    </td>
                </tr>

            }

                </tbody>

        </table>

了解下图。

enter image description here