Reputation: 33
Android Studio newbie and I can't find anything that makes sense to me to get the menu to work the way that I want.
My app streams videos from my website to the device. The menu has a list of 3 videos to watch. I would like for users to be able to open the menu, select the video they want to watch and then have that video load into the player. I have everything working, except that when you click on a video from the menu nothing happens and I know that this is because I have no code for it in my MainActivity.java
file.
Here's what MainActivity.java
looks like:
package com.mywebsite.videostreamer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.net.Uri;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView vidView = (VideoView) findViewById(R.id.myVideo);
String vidAddress = "http://www.mywebsite.com/vids/vidOne.mp4";
Uri vidUri = Uri.parse(vidAddress);
vidView.setVideoURI(vidUri);
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
vidView.start();
vidView.seekTo(100);
vidView.pause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.vidOne:
return true;
case R.id.vidTwo:
return true;
case R.id.vidThree:
return true;
}
return super.onOptionsItemSelected(item);
}
public void showPopup(MenuItem item) {
}
}
With this and the code in my menu_main.xml
file, I'm able to launch the app, click the menu button and view the list of available videos and then of course, when I click a video, nothing happens because I don't have the code to tell it what to do.
I'm assuming that I will need to create another instance of @Override
and have the int id = item.getItemId();
, the id variable that contains the selection, to put together the url string + the id variable. So something like String vidAddress = "http://www.mywebsite.com/vids/ + id;
and that will be contained in the new @Override
section and will handle loading the new video once selected.
Am I in the ballpark? Can someone help me find my seat?
Upvotes: 0
Views: 46
Reputation: 25491
Firstly, its worth saying that the '@override' is not an object or thing you create instances of, just in case this is what you meant.
What it means is that the super class, i.e. the class that is being subclassed, has a method of the same name and this method is being 'over written' in the current class.
So for your case above, the super class 'AppCompatActivity' already has a method 'onOptionsItemSelected' (for example) and this is being over written in your 'MainActivity' class.
For you specific question, there are different approaches you could take, but if you want to check it is working quickly you can simply change the video directly in the 'onOptionsItemSelected' code. In other words, assuming that the video playback code in your onCreate works, the following should change the video when a user clicks the first option:
public class MainActivity extends AppCompatActivity {
private VideoView vidView;
.
.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.vidOne: {
vidView.stopPlayback();
videoUri = Uri.parse("http://www.mywebsite.com/vids/NewVideo.mp4");
vidView.setVideoURI(videoUri);
vidView.start();
return true;
case R.id.vidTwo:
return true;
case R.id.vidThree:
return true;
}
return super.onOptionsItemSelected(item);
}
Note the definition of vidView is taken outside onCreate so you can use it elsewhere also.
You don't necessarily want to do the actual work within the call back itself always, but trying the above will at least give you a feel for how it works and you can decide if you want to modify it then.
Upvotes: 1