媒体播放器中播放的多个文件同时 [英] Media player playing multiple files at the same time

查看:165
本文介绍了媒体播放器中播放的多个文件同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪里,一旦用户点击一个声音从我的ListView玩的话,而播放声音时,他们点击另一个声音问题,2听起来,他们在同一时间点击播放。

我想有一个目前正在播放的声音,结束,然后开始新的声音,他们最近一次点击。

如果有人可以帮助我,那将是多AP preciated!

code:

 的ListView BoardList =(ListView控件)findViewById(R.id.BoardList);    字符串列表[] = {     音频1,音频2,音频3,Audio4,Audio5
         Audio6,Audio7,Audio8,音频9
           Audio10,Audio11,Audio12};    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
            R.layout.listcustomize,R.id.textItem,列表);    BoardList.setAdapter(适配器);    BoardList.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){
            MediaPlayer的MPLAYER = NULL;
            如果(位置== 0){                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio1);
                mPlayer.start();
            }            如果(位置== 1){
                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio2);
                mPlayer.start();            }
            如果(位置== 2){
                MPLAYER = MediaPlayer.create(HodgeMain.this,R.raw.Audio3);
                mPlayer.start();            }
            如果(位置== 3){
                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio4);
                mPlayer.start();
            }
            如果(位置== 4){
                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio5);
                mPlayer.start();
            }
            如果(位置== 5){
                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio6);
                mPlayer.start();
            }
            如果(位置== 6){
                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio7);
                mPlayer.start();
            }            如果(位置== 7){
                MPLAYER =的MediaPlayer
                        .create(HodgeMain.this,R.raw.Audio8);
                mPlayer.start();
            }
            如果(位置== 8){
                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio9);
                mPlayer.start();
            }
            如果(位置== 9){
                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio10);
                mPlayer.start();
            }
            如果(位置== 10){
                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio11);
                mPlayer.start();
            }
            如果(位置== 11){                MPLAYER = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio12);
                mPlayer.start();            }


解决方案

 媒体播放器播放多个文件在​​同一时间

尝试

声明 MediaPlayer的Mplayer的; 所有常用

如同

  BoardList.setAdapter(适配器);
MediaPlayer的MPLAYER;

然后用 mPlayer.release();

 如果(位置== 0){    如果(MPLAYER!= NULL)
    {
        mPlayer.release();
        MPLAYER = NULL;
    }    MPLAYER = MediaPlayer.create(HodgeMain.this,
            R.raw.Audio1);
    mPlayer.start();}




如果(位置== N)
{
        如果(MPLAYER!= NULL)
        {
            mPlayer.release();
            MPLAYER = NULL;
        }
        MPLAYER = MediaPlayer.create(HodgeMain.this,
                R.raw.AudioN);
        mPlayer.start();
}

关于发布()

释放资源 MediaPlayer对象相关。

它被认为是很好的做法,调用这个方法,当你使用的MediaPlayer 完成。

在特定的,每当一个应用程序的活动暂停(它的onPause()方法被调用),或停止(它的onStop()方法被调用),这方法应该是调用释放MediaPlayer对象,除非应用程序有特殊需要保持对象周围。

PS。我曾尝试与发布()这是工作的罚款!

示例:这是为我工作

  {尝试如果(位置== 1){
    如果(MPLAYER!= NULL){
        mPlayer.release();
        MPLAYER = NULL;
    }
    MPLAYER = MediaPlayer.create(MainActivity.this,R.raw.all);
    mPlayer.start();
}
如果(位置== 2){
    如果(MPLAYER!= NULL){
        mPlayer.release();
        MPLAYER = NULL;
    }
    MPLAYER = MediaPlayer.create(MainActivity.this,R.raw.all2);
    mPlayer.start();
}
}赶上(例外五){
    // TODO:处理异常
    e.printStackTrace();
}

I am having a problem where once the user clicks on a sound to play from my ListView, then while that sound is playing they click on another sound, the 2 sounds they clicked play at the same time.

I would like to have the sound that was currently playing, finish, then start the new sound that they most recently clicked.

If someone could help me, that would be much appreciated!

CODE:

ListView BoardList = (ListView) findViewById(R.id.BoardList);

    String List[] = {

     "Audio1", "Audio2", "Audio3", "Audio4", "Audio5"
         , "Audio6", "Audio7", "Audio8", "Audio9"
           , "Audio10", "Audio11", "Audio12" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.listcustomize, R.id.textItem, List);

    BoardList.setAdapter(adapter);

    BoardList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            MediaPlayer mPlayer = null;
            if (position == 0) {



                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio1);
                mPlayer.start();
            }

            if (position == 1) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio2);
                mPlayer.start();

            }
            if (position == 2) {


                mPlayer = MediaPlayer.create(HodgeMain.this, R.raw.Audio3);
                mPlayer.start();

            }
            if (position == 3) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio4);
                mPlayer.start();
            }
            if (position == 4) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio5);
                mPlayer.start();
            }
            if (position == 5) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio6);
                mPlayer.start();
            }
            if (position == 6) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio7);
                mPlayer.start();
            }

            if (position == 7) {


                mPlayer = MediaPlayer
                        .create(HodgeMain.this, R.raw.Audio8);
                mPlayer.start();
            }
            if (position == 8) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio9);
                mPlayer.start();
            }
            if (position == 9) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio10);
                mPlayer.start();
            }
            if (position == 10) {


                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio11);
                mPlayer.start();
            }
            if (position == 11) {

                mPlayer = MediaPlayer.create(HodgeMain.this,
                        R.raw.Audio12);
                mPlayer.start();

            }

解决方案

Media player playing multiple files at the same time

Try

Declare MediaPlayer mPlayer; common for all

Like

BoardList.setAdapter(adapter);
MediaPlayer mPlayer;

then use mPlayer.release();

if (position == 0) {

    if(mPlayer!=null)
    {
        mPlayer.release();
        mPlayer=null;
    }

    mPlayer = MediaPlayer.create(HodgeMain.this,
            R.raw.Audio1);
    mPlayer.start();

}
.
.
.
.
if(position==N)
{
        if(mPlayer!=null)
        {
            mPlayer.release();
            mPlayer=null;
        }
        mPlayer = MediaPlayer.create(HodgeMain.this,
                R.raw.AudioN);
        mPlayer.start();
}

About release():

Releases resources associated with this MediaPlayer object.

It is considered good practice to call this method when you're done using the MediaPlayer.

In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around.

PS. i have tried with release() which is working fine !

Example: which is working for me

try {

if (position == 1) {
    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
    mPlayer = MediaPlayer.create(MainActivity.this, R.raw.all);
    mPlayer.start();
}
if (position == 2) {
    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
    mPlayer = MediaPlayer.create(MainActivity.this, R.raw.all2);
    mPlayer.start();
}
}

catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}

这篇关于媒体播放器中播放的多个文件同时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆