如何在android中停止后启动MediaPlayer

时间:2016-07-15 08:40:48

标签: android android-mediaplayer

我是Android新手,在我的应用中,我正在使用MediaPlayer

当我第一次点击开始按钮时,歌曲会播放,但是当我点击停止按钮然后再次点击开始按钮时,歌曲不会再次开始播放。这是我到目前为止所做的:

public class PlayngUrlFiles extends AppCompatActivity {

    Button start, pause, stop;

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

        start = (Button) findViewById(R.id.button1);
        pause = (Button) findViewById(R.id.button2);
        stop = (Button) findViewById(R.id.button3);

        final MediaPlayer mp = new MediaPlayer();

        try {
            mp.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
            mp.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.pause();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.stop();
            }
        });
    }
}

8 个答案:

答案 0 :(得分:1)

试试这个:

stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mp != null) {
                    mp.stop();
                    mp.release();
                }
            }
        });

答案 1 :(得分:0)

解决方案1 ​​

对于此解决方案,我从here

获取了一些信息
public class PlayngUrlFiles extends AppCompatActivity {

    Button start, pause, stop;
    MediaPlayer mp;
    /**
     * remain false till media is not completed, inside OnCompletionListener make it true.
     */
    private boolean initialStage = true;

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

        start = (Button) findViewById(R.id.button1);
        pause = (Button) findViewById(R.id.button2);
        stop = (Button) findViewById(R.id.button3);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (initialStage) {
                    mp = new MediaPlayer();
                    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    new Player()
                            .execute("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
                } else {
                    if (mp && !mp.isPlaying())
                        mp.start();
                }
            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mp && mp.isPlaying())
                    mp.pause();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mp) {
                    mp.stop();
                    mp.release();
                    mp = null;
                    initialStage = true;
                }
            }
        });
    }

    /**
     * preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread.
     * @author piyush
     *
     */

    class Player extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog progress;

        @Override
        protected Boolean doInBackground(String... params) {
            // TODO Auto-generated method stub
            Boolean prepared;
            try {

                mp.setDataSource(params[0]);

                mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    initialStage = true;
                    mp.stop();
                    mp.reset();
                }
            }); 
            mp.prepare();
            prepared = true;
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                Log.d("IllegarArgument", e.getMessage());
                prepared = false;
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                prepared = false;
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                prepared = false;   
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                prepared = false;
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (progress.isShowing()) {
                progress.cancel();
            }
            Log.d("Prepared", "//" + result);
            mp.start();

            initialStage = false;
        }

        public Player() {
            progress = new ProgressDialog(PlayngUrlFiles.this);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            this.progress.setMessage("Buffering...");
            this.progress.show();

        }
    }

}

解决方案2

为了避免数据下载,请尝试此

public class PlayngUrlFiles extends AppCompatActivity {

    Button start, pause, stop;
    boolean prepared;

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

        start = (Button) findViewById(R.id.button1);
        pause = (Button) findViewById(R.id.button2);
        stop = (Button) findViewById(R.id.button3);
        final MediaPlayer mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        prepared = false;

        mp.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                prepared = true;
                mp.start();
            }
        });

        try {
            mp.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
        } catch (Exception e) {
            e.printStackTrace();
        }

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mp) {
                    try {
                        if(!prepared) {
                            mp.prepareAsync();
                            prepared = true;
                        } else {
                            mp.start();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mp) {
                    mp.pause();
                }
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mp) {
                    mp.stop();
                    prepared = false;
                }
            }
        });
    }
}

答案 2 :(得分:0)

就这样检查

if(mdpl.isPlaying()){
    mdpl.pause();
} else {
    mdpl.start();
}

答案 3 :(得分:0)

你无法调用start,媒体播放器处于停止状态。看一下媒体播放器MediaPlayer state diagram的状态图。

当你打电话给停止时,你必须调用release(),然后开始你会得到一个新的媒体播放器,或者只是调用reset。

答案 4 :(得分:0)

试试这个

public void onStopBtnClick(View view) {
    if (mAudioHelper != null) {
        mAudioHelper.stop();
        displayMessage("Stopping!");
    }
}

答案 5 :(得分:0)

以下是设置声音文件的代码。

if (mPlayer != null) {
     mPlayer.stop();
     mPlayer.release();
}
mPlayer = new MediaPlayer();
AppLog.d("Created new Media player");
try {
     mPlayer.setDataSource(fileName);
} catch (IOException e) {
     AppLog.e("Can't open " + fileName + " file", e);
}

在播放之前你应该调用mPlayer.prepareAsync()函数。 如果你想暂停/玩游戏,你必须使用这段代码:

if (mediaPlayer.isPlaying()){
    mediaPlayer.pause();
} else {
    mediaPlayer.start();
}

如果你想停止玩家使用它:

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

如果您想再次启动歌曲,请使用第一个功能,然后执行此步骤

答案 6 :(得分:0)

此代码可以帮助您..

 @Override
    protected void onDestroy() {
        super.onDestroy();
        destroyMediaPlayer();
    }

    private void destroyMediaPlayer() {
        if (mediaPlayer != null) {
            try {

                mediaPlayer.release();
                Log.d("here", "destroy");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } 

答案 7 :(得分:0)

@MRodrigues对于媒体播放器国家是正确的。但是,不要在停止后调用release(),而是修改你的代码,如下所示。

目前,

try {
            mp.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
            mp.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
            }
        });

修改为,

try {
            mp.setDataSource("http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3");
        } catch (Exception e) {
            e.printStackTrace();
        }

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mp.isPlaying()) {
                    mp.prepare();
                }
                mp.start();
            }
        });