У меня есть кнопки oferton, atrape, smiders, imbecil, billetes и tonto, что я хочу сделать для примера: я хочу нажать кнопку oferton из фрагмента Frag1, а затем запустить медиаплеер momo1, который находится вMainActivity Я знаю, что это неправильно, но я хочу знать, как запустить Media Player из MainActivity, когда я нажимаю кнопку из фрагмента.
Это MainActivity
package com.example.mauri.miapp;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
public MediaPlayer momo1;
public MediaPlayer momo2;
public MediaPlayer momo3;
public MediaPlayer momo4;
public MediaPlayer momo5;
public MediaPlayer momo6;
Frag1 f1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
f1 = new Frag1();
momo1 = MediaPlayer.create(this, R.raw.oferton);
momo2 = MediaPlayer.create(this, R.raw.atrape);
momo3 = MediaPlayer.create(this, R.raw.smiders);
momo4 = MediaPlayer.create(this, R.raw.imbecil);
momo5 = MediaPlayer.create(this, R.raw.billetes);
momo6 = MediaPlayer.create(this, R.raw.tonto);
android.app.FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.LayoutContenedor, f1).commit();
}
}
, и это мойФрагмент: Frag1
package com.example.mauri.miapp;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Frag1 extends Fragment {
public Frag1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("fragmento01", "onCreateView");
View vista = inflater.inflate(R.layout.fragment_frag1, container, false);
ImageButton oferton = vista.findViewById(R.id.btn_oferton);
ImageButton atrape = vista.findViewById(R.id.btn_atrape);
ImageButton smiders = vista.findViewById(R.id.btn_smiders);
ImageButton imbecil = vista.findViewById(R.id.btn_imbecil);
ImageButton billetes = vista.findViewById(R.id.btn_billetes);
ImageButton tonto = vista.findViewById(R.id.btn_tonto);
\\This is bad, but is a Example for what i whant to do
oferton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
momo1.seekTo(0);
momo1.start();
}
});
atrape.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
momo2.seekTo(0);
momo2.start();
}
});
smiders.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
momo3.seekTo(0);
momo3.start();
}
});
imbecil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
momo4.seekTo(0);
momo4.start();
}
});
billetes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
momo5.seekTo(0);
momo5.start();
}
});
tonto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
momo6.seekTo(0);
momo6.start();
}
});
return vista;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("fragmento01", "onCreate");
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.i("fragmento01", "onAttach");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("fragmento01", "onActivityCreated");
}
@Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
Log.i("fragmento01", "onViewStateRestored");
}
@Override
public void onStart() {
super.onStart();
Log.i("fragmento01", "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.i("fragmento01", "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.i("fragmento01", "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.i("fragmento01", "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i("fragmento01", "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("fragmento01", "Destroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.i("fragmento01", "onDetach");
}
}