使用 MediaPlayer 多次播放音频

Posted

技术标签:

【中文标题】使用 MediaPlayer 多次播放音频【英文标题】:Audio getting played multiple times using MediaPlayer 【发布时间】:2015-02-13 16:05:28 【问题描述】:

尝试编写一个程序,在该程序中单击 listItem 想要从原始播放特定音频。但每次点击播放都不会停止上一首曲目。

package com.vaishnavismeclass.tiruppavai;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 

    ListView lv;
    Context c = getApplicationContext();
    public static MediaPlayer mp = new MediaPlayer();

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.listView1);
        lv.setAdapter(new VcAdapter(this));
        // 
        // to handle onclicklistener
        //
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 

            @Override
            public void onItemClick(AdapterView<?> parentAdapter, View view, int position,long id) 
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "Position ["+position+"]", Toast.LENGTH_SHORT).show();
                 //Uri pasuram01 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pasuram01);
                 //Uri pasuram02 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pasuram02);
                 //int resID = 0;

                 playMyMusic(position, mp);


                





        );

    

    @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 playMyMusic(int songNumber, MediaPlayer mp) 
        mp.setAudiostreamType(AudioManager.STREAM_MUSIC);

        //
        //
        // assign the music
        //
        //
        Toast.makeText(MainActivity.this, "Position for play is  ["+songNumber+"]", Toast.LENGTH_SHORT).show();
        switch (songNumber) 
        case 0:
            mp = MediaPlayer.create(this, R.raw.taniyan01);
            //mp.start();
            break;
        case 1:
            mp = MediaPlayer.create(this, R.raw.taniyan02);
            //mp.start();
            break;
        case 2:
            mp = MediaPlayer.create(this, R.raw.taniyan03);
            //mp.start();
            break;
        case 3:
            mp = MediaPlayer.create(this, R.raw.pasuram01);
            ;
            break;
        case 4:
            mp = MediaPlayer.create(this, R.raw.pasuram02);
            ;
            break;
        


            //mp.prepare();
            Toast.makeText(MainActivity.this, "Playing  ["+songNumber+"]", Toast.LENGTH_SHORT).show();
            if (mp!=null && mp.isPlaying()) 
                Toast.makeText(MainActivity.this, "Player is playing", Toast.LENGTH_SHORT).show();
                mp.stop();
                mp.reset();
                mp.release();
            
            else
            

                mp.start();
            

            Toast.makeText(MainActivity.this, "Played  ["+songNumber+"]", Toast.LENGTH_SHORT).show();

    




class SingleRow 
    String pasuram;
    int img;

    SingleRow(String pasuram, int img) 
        this.pasuram = pasuram;
        this.img = img;
    


class VcAdapter extends BaseAdapter 
    ArrayList<SingleRow> list;
    Context context;

    VcAdapter(Context c) 
        context = c;
        list = new ArrayList<SingleRow>();
        // get resources using context
        Resources res = c.getResources();
        String[] pasuram_en = res.getStringArray(R.array.pasurams_ta);
        //String[] pasuram_ta = res.getStringArray(R.array.pasurams_ta);
        //String[] pasuram_te = res.getStringArray(R.array.pasurams_te);
        int[] imgs =  R.drawable.p1, R.drawable.p1, R.drawable.p1,
                R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4,
                R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8,
                R.drawable.p9, R.drawable.p10, R.drawable.p11, R.drawable.p12,
                R.drawable.p13, R.drawable.p14, R.drawable.p15, R.drawable.p16,
                R.drawable.p17, R.drawable.p18, R.drawable.p19, R.drawable.p20,
                R.drawable.p21, R.drawable.p22, R.drawable.p23, R.drawable.p24,
                R.drawable.p25, R.drawable.p26, R.drawable.p27, R.drawable.p28,
                R.drawable.p29, R.drawable.p30, R.drawable.p1, R.drawable.p1, R.drawable.ic_launcher ;

        for (int i = 0; i < pasuram_en.length; i++) 
            // list.add(new SingleRow(pasuram_en[i], imgs[i]));
            list.add(new SingleRow(pasuram_en[i], imgs[i]));
        
    

    @Override
    public int getCount() 
        // TODO Auto-generated method stub
        return list.size();
    

    @Override
    public Object getItem(int i) 
        // TODO Auto-generated method stub
        return list.get(i);
    

    @Override
    public long getItemId(int i) 
        // TODO Auto-generated method stub
        return 0;
    

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) 
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.single_row, viewGroup, false);

        TextView pasuram = (TextView) row.findViewById(R.id.textView1);
        ImageView img = (ImageView) row.findViewById(R.id.imageView1);

        SingleRow temp = list.get(i);
        pasuram.setText(temp.pasuram);
        img.setImageResource(temp.img);

        return row;
    


MediaPlayer.IsPlaying() 正在返回 false,即使当前正在播放音频。

提前致谢。

【问题讨论】:

以上代码有效,但正在播放多个音频。当用户单击列表项时,只希望播放一个实例。任何帮助表示赞赏。 【参考方案1】:

将 MediaPlayer 声明为静态。

public static MediaPlayer mp;

【讨论】:

试过了,但同样的问题仍然存在。 让我知道是什么问题? 每次点击列表项都会创建一个媒体播放器实例。 Mediaplayer.isPlaying() 未被识别。 你还在用 as MediaPlayer 的语句吗 mp = new MediayaPlayer();然后从该行中删除“new MediayaPlayer()”部分, 更新了代码,但没有使用新的 mediaPlayer() 进行初始化,代码不起作用。收到错误“在状态 1 中调用停止”。当前状态是多个实例仍在播放。

以上是关于使用 MediaPlayer 多次播放音频的主要内容,如果未能解决你的问题,请参考以下文章

使用MediaPlayer播放音频-----之二

Android多媒体功能开发——使用MediaPlayer类播放音频

在 Android 中使用 MediaPlayer 播放音频文件

android:使用单个 mediaplayer 对象播放音频文件并显示图像

使用MediaPlayer播放音频文件

Android播放音频MediaPlayer的几种方式介绍