我正在尝试使用Mediaplayer
MP1 播放流式广播作为 Mediaplayer 的变量我想要播放所有Fragments
应用,预计会有一个activity
( ActivityOne )包含另一个Mediaplayer
MP2 ,所以我想停止 MP1 当我进入( ActivityOne )activity
,玩 MP2 时,以及当我从( ActivityOne)返回时)我想恢复 MP1 ,当我点击button
中存在的fragment
时,我的主要问题是( ActivityOne )被调用}
我的代码只适用于一个方向:
当我从( ActivityOne )活动返回时,音乐会停止。
应用程序的结构: MainAcitivty > 片段>的 ActivityOne
MainActivity.java
MediaPlayer MP1;
boolean prepared = false;
boolean started = false;
PlayerTask playerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
playerTask = new PlayerTask();
playerTask.execute(stream);
/**/
MusicButton = findViewById(R.id.toggleButton);
MusicButton.setVisibility(View.INVISIBLE);
MusicButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (started && MusicButton.isChecked()) {
started = false;
MP1.pause();
MusicButton.setChecked(true);
} else {
started = true;
MP1.start();
MusicButton.setChecked(false);
}
}
});
}
@SuppressLint("StaticFieldLeak")
public class PlayerTask extends AsyncTask<String, Void, Boolean> {
ProgressBar loadingRL = findViewById(R.id.progressBar);
@Override
protected void onPreExecute() {
super.onPreExecute();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes attribs = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
MP1.setAudioAttributes(attribs);
} else {
MP1.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
loadingRL.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(String... strings) {
try {
MP1.setDataSource(strings[0]);
MP1.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
MP1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer MP1) {
MP1.start();
}
});
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
MusicButton.setVisibility(View.VISIBLE);
MusicButton.setChecked(true);
loadingRL.setVisibility(View.VISIBLE);
}
ActivityOne.java
MediaPlayer MP2;
boolean prepared = false;
boolean started = false;
ToggleButton music;
PlayerTask playerTask = null;
CoordinatorLayout coordinatorLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_for_ringtone);
coordinatorLayout = findViewById(R.id.coord);
MP2 = new MediaPlayer();
playerTask = new PlayerTask();
playerTask.execute(url);
}
@SuppressLint("StaticFieldLeak")
public class PlayerTask extends AsyncTask<String, Void, Boolean> {
ProgressBar pb = findViewById(R.id.progress);
@Override
protected void onPreExecute() {
super.onPreExecute();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes attribs = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
MP2.setAudioAttributes(attribs);
} else {
MP2.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
}
@Override
protected Boolean doInBackground(String... strings) {
if (!isCancelled()) {
try {
MP2.setDataSource(strings[0]);
MP2.prepare();
prepared = true;
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
MP2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer MP2) {
MP2.start();
}
});
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
music.setEnabled(true);
music.setVisibility(View.VISIBLE);
music.setChecked(true);
all.setVisibility(View.VISIBLE);
}
@Override
protected void onCancelled(Boolean aBoolean) {
if (isCancelled() && MP2.isPlaying()) {
MP2.stop();
}
}
}
@Override
public void onBackPressed() {
if (playerTask != null && playerTask.getStatus() == AsyncTask.Status.FINISHED) {
if (MP2.isPlaying()) {
MP2.stop();
}
} else if (playerTask != null && playerTask.getStatus() != AsyncTask.Status.FINISHED) {
playerTask.cancel(true);
}
super.onBackPressed();
}
我花了2天的时间来解决这个问题而没有任何结果,请有人帮助我,我会感激他
答案 0 :(得分:0)
您是否尝试过使用startActivityForResult()?
答案 1 :(得分:0)
您可以使用Otto
库来解决此问题。首先创建一个新的Java
课程,但选择enum
代替enum
,然后在public enum PlaybackEvent {
PLAY, PAUSE
}
内添加:PLAY和PAUSE,例如:
Application
然后,如果您没有使用自定义Application
类,请创建一个并在override
方法中扩展onCreate
和app gradle
。在compile 'com.squareup:otto:1.3.8'
添加Bus
内,然后在Application
类中创建public class MApplication extends Application {
public static Bus sBus = new Bus(ThreadEnforcer.MAIN);
@Override
public void onCreate() {
super.onCreate();
sBus.register(this);
}
的实例并注册。例如,这将是这样的:
<application
android:name="com.packagename.MApplication"
不要忘记在清单默认应用程序类中替换您的新应用程序类
MainActivity
在override
班onResume
之后,在onPause
和 @Override
protected void onResume() {
super.onResume();
try {
MApplication.sBus.register(this);
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
MApplication.sBus.unregister(this);
}
catch(Exception e){
e.printStackTrace();
}
}
注册/取消注册您的活动总线。
MainActivity
在public void
之后创建PlayBackEvent
方法作为参数Subscribe
和fragment
传递,以便您可以收听将从 @Subscribe
public void handlePlaybackEvent(PlaybackEvent event) {
switch (event) {
case PLAY:
if(MP1.isPlaying())
MP1.pause();
break;
case PAUSE:
if(!MP1.isPlaying())
MP1.play();
break;
}
}
发送的消息类。例如:
fragment
你要做的最后一件事是在开始第二项活动时从你的 MApplication.sBus.post(PlaybackEvent.PAUSE);
发送信息,这将是:
overriding onBackPressed
当然你也可以发送一条消息,让第二个活动 MApplication.sBus.post(PlaybackEvent.PLAY);
的MP1再次播放到内线代码:
@HostListener("window:resize", ["$event"])
onResize(event) {
// some code
}
希望这可以帮助您解决问题。