I am trying to play a video by starting it when the Activity starts.
However,I am just getting a black screen when the Activity starts.
I do not understand why this is occurring.
Is this not the proper way to configure and play a video?
HelloMoonFragment:
public class HelloMoonFragment extends Fragment {
private AudioPlayer mPlayer = new AudioPlayer();
private Button mPlayButton;
private Button mStopButton;
private Button mPauseButton;
private Button mResumeButton;
private Button mStartVideo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup parent,Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon,parent,false);
mPlayButton = (Button)v.findViewById(R.id.hellomoon_playButton);
mPlayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.play(getActivity());
}
});
mStopButton = (Button)v.findViewById(R.id.hellomoon_stopButton);
mStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.stop();
}
});
mResumeButton = (Button)v.findViewById(R.id.helloMoon_startButton);
mResumeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.resume();
}
});
mPauseButton = (Button)v.findViewById(R.id.hellomoon_pauseButton);
mPauseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlayer.pause();
}
});
mStartVideo = (Button)v.findViewById(R.id.hellomoon_startVideo);
mStartVideo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),VideoActivity.class);
startActivity(i);
}
});
return v;
}
#Override
public void onDestroy(){
super.onDestroy();
mPlayer.stop();
}
}
VideoActivity:
public class VideoActivity extends Activity {
private String mVideoPath = "android.resource://" + "com.tubbs.android.hellomoon" + "/" + "R.raw/apollo_17_stroll";
private Uri mVideo;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
mVideo = Uri.parse(mVideoPath);
VideoView videoView = (VideoView)findViewById(R.id.hellomoon_startVideo);
MediaController mc = new MediaController(VideoActivity.this);
mc.setAnchorView(videoView);
videoView.setMediaController(mc);
videoView.start();
}
}
activity_video.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="#+id/hellomoon_startVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Related
This is my second activity and when open this activity threw button activity take load and open after some time and some times UI Freeze and some time black screen open for some seconds
public class AartiActivity extends AppCompatActivity {
final MediaPlayer player1 = new MediaPlayer();
private Button btn_playarti, btn_pausearti, btn_stoparti;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aarti);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
btn_playarti = (Button) findViewById(R.id.btn_playarti);
btn_pausearti = (Button) findViewById(R.id.btn_pausearti);
btn_stoparti = (Button) findViewById(R.id.btn_stoparti);
try {
player1.setDataSource(STRING_URL);
player1.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btn_playarti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer player) {
player.stop();
}
});
player1.start();
}
});
btn_pausearti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (player1.isPlaying()) {
player1.pause();
}
}
});
btn_stoparti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.stop();
}
});
}
#Override
protected void onStop() {
super.onStop();
player1.stop();
}
}
I am trying to make a sound wait to play if another sound is currently playing. But, the code I wrote doesn't execute.
public class Transportation extends AppCompatActivity {
private MediaPlayer sound,sound1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transportation);
sound = MediaPlayer.create(this,R.raw.vatunabus);
final ImageView imageView= (ImageView)findViewById(R.id.vatunabussound);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound1.isPlaying()){
sound.stop();
sound.release();
}
else if(sound.isPlaying()){
sound.pause();
}
else {
sound.start();
}
}
});
sound1 = MediaPlayer.create(this,R.raw.varowlsanabus);
final ImageView imageView1 = (ImageView)findViewById(R.id.firstbussound);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sound.isPlaying()){
sound1.stop();
sound1.release();
}
else if(sound1.isPlaying()){
sound1.pause();
}
else {
sound1.start();
}
}
});
}
}
I am trying to add a toggle button, so that my image button switches the direction that it translates based on the status of the toggle button. However, I am running into problems when trying to add it to the media player, and when using the if statement.
public class MainActivity extends AppCompatActivity {
TextToSpeech tts1;
ToggleButton toggleButton;
public void changeTranslation(View view){
boolean on = ((ToggleButton)view).isChecked();
if(on){
toggleButton.setText("Speech-to-Farts");
Toast.makeText(getApplicationContext(),"Translating speech to farts.",Toast.LENGTH_SHORT);
} else{
toggleButton.setText("Farts-to-Speech");
Toast.makeText(getApplicationContext(),"Translating farts to speech.",Toast.LENGTH_SHORT);
}
}
private static final String TAG = "MainActivity";
private MediaPlayer mMediaPlayer;
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer mediaPlayer){
releaseMediaPlayer();
}
};
private void releaseMediaPlayer(){
if(mMediaPlayer != null){
mMediaPlayer.release();
mMediaPlayer=null;
}
}
TextView tv1;
ImageButton b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(textView);
b1 = (ImageButton) findViewById(R.id.recordButton);
final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggle_button);
toggleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (toggleButton.isChecked()) {
toggleButton.setText("Farts-to-Speech");
} else {
toggleButton.setText("Speech-to-Farts");
}
}
});
tts1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts1.setLanguage(Locale.US);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
#Override
public void onClick(View v) {
int[] fartLibrary={};
String[] speechLibrary={};
if(toggleButton==true) {
releaseMediaPlayer();
String randomTranslation = (speechLibrary[new Random().nextInt(speechLibrary.length)]);
tts1.speak(randomTranslation, TextToSpeech.QUEUE_FLUSH, null);
} else {
releaseMediaPlayer();
mMediaPlayer=MediaPlayer.create(this,fartLibrary);
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
}
}
This is my Translations.java. I'm not sure if this is necessary anymore though
public class Translations{
private String mSpeechTranslation;
private int mFartTranslation;
public Translations(String speechTranslation, int fartTranslation){
mFartTranslation = fartTranslation;
mSpeechTranslation = speechTranslation;
}
public String getmSpeechTranslation(){ return mSpeechTranslation;}
public int getmFartTranslation(){ return mFartTranslation;}
}
I have made successfully my Imagebutton to open anther activity but the issue is by using the same method on another ImageButton it comes up with an error saying the method is already used in "Main Activity".
public class MainActivity extends ActionBarActivity {
private static ImageButton ImageButton_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickImageButtonListener();
}
public void OnClickImageButtonListener() {
ImageButton_sbm = (ImageButton)findViewById(R.id.imageButton);
ImageButton_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.event");
startActivity(intent);
}
}
);
}
private static ImageButton ImageButton2_sbm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickImageButtonListener();
}
public void OnClickImageButtonListener() {
ImageButton_sbm = (ImageButton)findViewById(R.id.imageButton2);
ImageButton_sbm.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.news");
startActivity(intent);
}
}
);
}
How, if anything, can I apply another method for the "saintbedeslytham.saintbedes.news"
Your problem here is that you cannot create two method with the same definition.
You have two:
protected void onCreate(Bundle savedInstanceState);
and two:
public void OnClickImageButtonListener();
You can't have 2 methods with the same definition for 2 simple reasons.Imagine 2 people with the same name, call for their name.
Who will answer?
Who are you calling?
Edit:
So you can:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareClicks();
}
private void prepareClicks() {
findViewById(R.id.imageButton).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.event");
startActivity(intent);
}
}
);
findViewById(R.id.imageButton2).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.news");
startActivity(intent);
}
}
);
}
}
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareClicks();
}
private void prepareClicks() {
findViewById(R.id.imageButton).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.event");
startActivity(intent);
}
}
);
findViewById(R.id.imageButton2).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("saintbedeslytham.saintbedes.news");
startActivity(intent);
}
}
);
}
}
Yup the method worked.
Thanks for the replies,
(Done by Gorcyn)
I have a music which I want it to be as a background music for the game I made.
I want it to stop when score becomes empty in order to start another one.
But it does not stop by using mp.stop(); !
My code:
public class countgame extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countgame);
final MediaPlayer mp = MediaPlayer.create(countgame.this, R.raw.game);
mp.start();
final ImageView i1 = (ImageView)findViewById(R.id.imageView1);
final ImageView i2 = (ImageView)findViewById(R.id.imageView2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
incorrect();
}
});
}
public void incorrect(){
final ImageView score = (ImageView) findViewById(R.id.i3);
if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score3).getConstantState())){
score.setBackgroundResource(R.drawable.score2);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score2).getConstantState())){
score.setBackgroundResource(R.drawable.score1);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score1).getConstantState())){
score.setBackgroundResource(R.drawable.empty);
MediaPlayer m = MediaPlayer.create(countgame.this, R.raw.game);
m.stop();//here is the problem
MediaPlayer mp=MediaPlayer.create(countgame.this, R.raw.gameover);
mp.start();
}
}
Change your code to this:
public class countgame extends Activity implements OnClickListener{
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countgame);
mp = MediaPlayer.create(countgame.this, R.raw.game);
mp.start();
final ImageView i1 = (ImageView)findViewById(R.id.imageView1);
final ImageView i2 = (ImageView)findViewById(R.id.imageView2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
incorrect();
}
});
}
public void incorrect(){
final ImageView score = (ImageView) findViewById(R.id.i3);
if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score3).getConstantState())){
score.setBackgroundResource(R.drawable.score2);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score2).getConstantState())){
score.setBackgroundResource(R.drawable.score1);
}
else if ( score.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.score1).getConstantState())){
score.setBackgroundResource(R.drawable.empty);
mp.stop();//here is the problem
mp=MediaPlayer.create(countgame.this, R.raw.gameover);
mp.start();
}
}
}