我开始像这样的媒体播放器:
if (mp != null) {
mp.stop();
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.background);
mp.start();
如何停止其他活动?它继续在另一项活动中发挥作用。如何在其他活动中使用onDestroy
?
答案 0 :(得分:6)
在项目中使用如下所示的Separate类。
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
public class AudioPlay {
public static MediaPlayer mediaPlayer;
private static SoundPool soundPool;
public static boolean isplayingAudio=false;
public static void playAudio(Context c,int id){
mediaPlayer = MediaPlayer.create(c,id);
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
if(!mediaPlayer.isPlaying())
{
isplayingAudio=true;
mediaPlayer.start();
}
}
public static void stopAudio(){
isplayingAudio=false;
mediaPlayer.stop();
}
}
播放歌曲
`AudioPlay.playAudio(mContext, R.raw.audiofile);` // play it from your preferred activity. and you can change raw file to your path also its depends upon your requirement.
然后
从任何活动中使用此行AudioPlay.stopAudio();
停止音频。
希望这可以帮助。
答案 1 :(得分:0)
在第一个活动中覆盖onPause
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.stop();
}
答案 2 :(得分:0)
你不能叫停止活动,而是从活动本身到
这样您就可以在服务中发送媒体播放器并绑定到您要访问它的活动中的服务
答案 3 :(得分:0)
由于您已经在第一个活动中启动了媒体播放器并希望停止在另一个活动中,只需使用布局inflater self在第一个活动中调用您的第二个布局而不是创建另一个活动..并且在第二个布局文件上停止媒体播放器按下按钮
public class FirstAvtivity extends Activity
{
MediaPlayer mPlayer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity_layoutfile);
Button b=(Button)findViewById(R.id.button1);
//start the media player like how you were starting in your activity
// then after clicking button you will be navigated to new layout , there
// you can stop media player
mPlayer.start();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateLayout();
}
});
}
private void newUpdateLayout() {
LayoutInflater inflater = LayoutInflater.from(this);
setContentView(inflater.inflate(R.layout.second_disapr_scr, null));
finalDismiss=(Button)findViewById(R.id.final_dismiss);
finalDismiss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"welcome to second
avtivity",Toast.LENGTH_SHORT).show();
mPlayer.stop();
finish();
}
});
}
}
答案 4 :(得分:0)
这是我的科特林解决方案:
<iframe class="_3dpJe0xLKraiOf" sandbox="allow-scripts allow-same-origin allow-popups allow-forms" src="/contact?compact=1&trello=1&url=https%3A%2F%2Ftrello.com%2F*[profilename]*%2Fboards&source=from%20in%20app" style="height: 448px;" xpath="1"></iframe>
我从itsrajesh4uguys的Java答案开始,然后应用了以下更改:
最后我以这种方式使用它:
在package com.programacionymas.myapp.services
import android.content.Context
import android.media.MediaPlayer
object AudioPlay {
var mediaPlayer: MediaPlayer? = null
var lastResource: Int? = null
fun playAudio(c: Context, id: Int, isLooping: Boolean = true) {
createMediaPlayer(c, id)
mediaPlayer?.let {
it.isLooping = isLooping
if (!it.isPlaying) {
it.start()
}
}
}
private fun createMediaPlayer(c: Context, id: Int) {
// in case it's already playing something
mediaPlayer?.stop()
mediaPlayer = MediaPlayer.create(c, id)
lastResource = id
}
// usually used inside the Activity's onResume method
fun continuePlaying(c: Context, specificResource: Int? = null) {
specificResource?.let {
if (lastResource != specificResource) {
createMediaPlayer(c, specificResource)
}
}
mediaPlayer?.let {
if (!it.isPlaying) {
it.start()
}
}
}
fun pauseAudio() {
mediaPlayer?.pause()
}
}
方法中:
onCreate
在AudioPlay.playAudio(this, R.raw.background_music)
方法中:
onResume
在我的情况下,我必须指定资源,因为我的一些活动开始播放另一种音乐声音。