I'm now trying something with a youtube custom player. I'm trying to have it play a video containing a playlist of a certain game's original soundtracks.
The thing is, everything was done by the book (again), yet I still have this concurring problem of force stopping every time I tried to open the APK. Here are the code :
XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context=".MainActivity">
<view
android:id="#+id/YoutubePlayView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/buttonPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Play Video" />
</LinearLayout>
Java :
package my.videoplayerapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
YouTubePlayerView mYoutubePlayerView;
Button PlayVidButton;
YouTubePlayer.OnInitializedListener mOnInitializedListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Starting.");
PlayVidButton = findViewById(R.id.buttonPlay);
mYoutubePlayerView = findViewById(R.id.YoutubePlayView);
mOnInitializedListener = new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
Log.d(TAG, "onClick: Initialization Done.");
youTubePlayer.loadVideo("DBToMwdYBME");
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.d(TAG, "onClick: Initialization Failed.");
}
};
PlayVidButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Initializing Youtube Player.");
mYoutubePlayerView.initialize((YoutubeVidPlayerConfig.getApiKey()), mOnInitializedListener);
Log.d(TAG, "onClick: Initialization Done.");
}
});
}
}
Logcat, linked : https://drive.google.com/open?id=1w5TfCIfyCWCrOZmrjb0lzTpndoQc2SJ0
I'm trying to actually be able to play the video through the button, I know the method of playing the next video or even playing sequence of videos, I just don't know why it keeps on crashing as I speak right now...
Any help would be appreciated in advance!
You have a view with the id of "YoutubePlayView" but your class is called "YoutubePlayerView". Change one of them and it should work (well it shouldn't force close at this spot).
Related
Following a tutorial, I did set up onClick-Listeners before.
Now I am trying to make a simple app for listening to one stream, but my onClickListener doesn't do anything.
I created a new very simple app in android studio 3.3.2 to find out what is happening. I guess it is something very basic.
package com.example.musicplay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
final Button play_button = findViewById(R.id.play_button);
Log.i("Button found","this one: " + play_button.getText());
play_button.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the play button is clicked on.
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "A click happened!", Toast.LENGTH_SHORT).show();
}
});
setContentView(R.layout.activity_main);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/play_button"/>
</LinearLayout>
The toast message should appear when the button is clicked, but nothing happens.
You are calling setContentView twice. This will replace the view hierarchy set up by the first call. Remove the second call to setContentView.
I want to make a video view running at my started_activity background. But it fails to load.
The screen is black and if I navigate or push my page to another page 2 times, it will show app is not responding alert.
I follow this video tutorial: https://www.youtube.com/watch?v=tPeDn18FrGY&t=210s
started_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartedActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true" />
<Button
android:id="#+id/btnToLoginPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="52dp"
android:text="Button"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
ActiviyStarted.java
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class StartedActivity extends AppCompatActivity {
private VideoView videoview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_started);
videoview = (VideoView) findViewById(R.id.videoView);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video_background);
videoview.setVideoURI(uri);
videoview.start();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setLooping(true);
}
});
Button btn = (Button) findViewById(R.id.btnToLoginPage);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(StartedActivity.this, LoginActivity.class);
StartedActivity.this.startActivity(i);
}
});
}
}
Code with video view
Code without video view
Video Path and error message
Try this way:
videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
videoview.stopPlayback();
videoview.setVideoURI(uri);
videoview.start();
}
});
videoview.setVideoURI(uri);
videoview.start();
You can also set setOnErrorListener to check for the error:
videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
//check error here
return false;
}
});
im trying to make a button play a sound for a language learning app.. so when the user clicks the button it will say something like "How are you"
anyway, im getting errors "cannot resolve method" on (Tab1Fragment.this, R.raw.howareyou); , setContentView and findViewById
here is my code..
Tab1Fragment.java
package com.storey.user.storeytom.fragments;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import com.demo.slidingmenu_tabhostviewpager.R;
public class Tab1Fragment extends Fragment {
#Override
public View onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1fragment);
final MediaPlayer buttonSound = MediaPlayer.create(Tab1Fragment.this, R.raw.howareyou);
Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent("com.storey.user.storeytom"));}
});
and here is my .xml file if you need that also...
tab1fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Conversation" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
sorry for the "nooby" question... thanks in advance
final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.sound));
Button play_button = (Button)this.findViewById(R.id.button);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Playing sound...");
mp.start();
}
});
OR Check this :android - how to make a button click play a sound file every time it been pressed?
First of all let me tell you that I am a beginner at this... I started to try developing apps about a week ago.
When I start my app on a real device it starts normally But when I click the button "oneplus" it says that app isn't responding...
this app adds one number to the number in TextView .Earlier I faced same problem and thought it was because of fragments.So i removed them and made a very simple app But still the problem persists
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="0"
android:id="#+id/num"
android:textSize="70sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="100dp"
android:text=" + 1 "
android:textSize="50sp"
android:id="#+id/plusone"
android:onClick="onClick"
/>
</RelativeLayout>`
mainactivity.java
package com.example.tjain.oneup;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
final TextView number = (TextView) findViewById(R.id.num);
int original = Integer.parseInt(number.getText().toString());
int newnum = original + 1;
number.setText(newnum);
}
}
I am facing this error in other apps too. Maybe I am repeating my mistake . Please point it out. Thanks in advance
As I doubt for two scenario in your onClick for Exception
NumberFormat Exception
ResourceNotFound Exception
For First,
try
{
int original = Integer.parseInt(number.getText().toString());
}catch(Exception ex)
{
ex.printStacktrace();
}
For Second,
Because int is not a argument for TextView's setText (only if its not a part of String resource id), as it will find with resource id not accepting as String value. and obvious Resource Not Found exception
you have to convert int to String,
number.setText(String.valueOf(newnum));
What I would recommend doing is adding your UI elements at the class level of your activity so you have one TextView object and one Button object, that way you only have to search for the resource once and you can add a click listener to the button itself like this:
public class MainActivity extends Activity{
private TextView mNumberTextView;
private Button mOnePlusButton;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNumberTextView = findViewbyId(R.id.num);
mOnePlusButton = findViewbyId(R.id.plusone);
mOnePlusButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
int original = Integer.parseInt(mNumberTextView.getText().toString());
mNumberTextView.setText(original + 1);
}
});
}
}
Edit: Since you're still new at this, it's always a good idea to check the documentation.
You should use
number.setText(+newnum);
instead
number.setText(newnum);
I just started learning android development using android studio, and i finished the "Starting another activity" , I tried practicing on what I've learned by changing the code a bit and messing with it.
(The tutorial was about displaying text sent by another activity)
So i added a seekBar in order to change the text size. After i send the text to another activity using an intent it crashes, but if i remove the seekBar listener , it works fine.
DisplayMessageActivity.java:
package com.keddy.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class DisplayMessageActivity extends ActionBarActivity {
public SeekBar _seekbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Getting the intent received by some other activity
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Text view initialization
final TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//Showing text on screen
setContentView(textView);
_seekbar = (SeekBar)findViewById(R.id.seekBar1);
_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Toast.makeText(getApplicationContext(),"Progress Changing",Toast.LENGTH_SHORT).show();
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(),"Started Tracking",Toast.LENGTH_SHORT).show();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(),"Progress Stopped Changing",Toast.LENGTH_SHORT).show();
}
});
}
I included the onCreate() method only , because that was the only thing I've changed.
activity_display_message.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.keddy.myfirstapp.DisplayMessageActivity">
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/seekBar1"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:indeterminate="false"
android:max="100"
/>
</RelativeLayout>
I've searched around and saw the reason was NullPointerException , but i dont know how to fix it , since i have no experience with Java nor Android Development.
if you find a solution please provide:
Code Example
How to avoid it again
Why did it happen
Any help whatsoever will be appreciated.
Change
setContentView(textView);
to
setContentView(R.layout.activity_display_message);
You must setContentView(...) and after add _seekbar = (SeekBar)findViewById(R.id.seekBar1);
The reason is you are adding textview in your Content
i.e. setContentView(textview);
Your activity is unable to find seekbar1 view. This is the reason why your app is crashing.
Instead of adding textview in Javacode add in xml and use it same way you are using seekbar, and change setContentView(textView) to setContentView(R.layout.your_xml_file_name);