How to keep playing YouTubePlayer in Android - android

Now I'm creating an Android application witch use YouTube api to play Video.
But the player stop when I change Activity.
I want to keep Playing like a Music Player even when Activity will change.
I try to solve that by adding Static on YouTubePlayer.(But cannot solve.)
I mean I want to Keep it's playing when the Activity or the Application itself move to Background.
So if you have any ideas, please share with me :)
-----------------Code-----------------
public class PlayActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener,YouTubePlayer.PlayerStateChangeListener{
public static final String DEVELOPER_KEY = "My_Dev_Key";
private LinearLayout yPos;
private static YouTubePlayer yt;
private YouTubePlayerView ytp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
yPos = (LinearLayout)findViewById(R.id.youtube_pos);
try{
ytp = new YouTubePlayerView(this);
ytp.initialize(DEVELOPER_KEY, this);
}catch(Exception e){
finish();
}
yPos.addView(ytp);
}
#Override
public void onAdStarted() {
// TODO Auto-generated method stub
}
#Override
public void onError(ErrorReason arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLoaded(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLoading() {
// TODO Auto-generated method stub
}
#Override
public void onVideoEnded() {
// TODO Auto-generated method stub
}
#Override
public void onVideoStarted() {
// TODO Auto-generated method stub
}
#Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
#Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer arg1,
boolean arg2) {
// TODO Auto-generated method stub
if(!arg2){
if(yt == null){
yt = arg1;
yt.setPlayerStateChangeListener(this);
yt.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
yt.loadVideo("u0v5A6cQOhs");
}
}
}
}

Related

How to get the context from a function of a class that extends Activity in android?

I have a class that extends Activity, When I try to access the context from the onCreate() method it gets printed but when I save it in a variable context and try to access it from the committext() function as shown below, It prints null. I also tried using "this" and getBaseContext() directly from the committext() function as it is a part of the same class, I get null pointer exception. Please help me figure out what is going wrong.
public class MainKeyboardActionListener extends Activity implements KeyboardView.OnKeyboardActionListener, View.OnTouchListener {
private Context context;
public static boolean active = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mode_keyboard, false);
context=getBaseContext();
System.out.println("contexxtt1"+context);
}
#Override
public void onStart() {
super.onStart();
active = true;
}
#Override
protected void onResume() {
super.onResume();
}
private static class myHandler extends Handler {
}
;
public void setInputConnection(InputConnection ic) {
}
#Override
public void onKey(int arg0, int[] arg1) {
// TODO Auto-generated method stub
}
#Override
public void onPress(int keyCode) {
}
#Override
public void onRelease(int keyCode) {
commitText(String.valueOf(keyCode));
}
private void handleException(int keyCode) {
}
private void removeHalantMode() {
}
private void commitText(String text) {
if(active==true) {
System.out.println("contexxtt"+context);
}
}
#Override
public void onText(CharSequence arg0) {
// TODO Auto-generated method stub
}
#Override
public void swipeDown() {
// TODO Auto-generated method stub
}
#Override
public void swipeLeft() {
// TODO Auto-generated method stub
}
#Override
public void swipeRight() {
// TODO Auto-generated method stub
}
#Override
public void swipeUp() {
// TODO Auto-generated method stub
}
#Override
protected void onPause() {
super.onPause();
}
#Override
public void onStop() {
super.onStop();
active = false;
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
Make your myHandler class not static.

Unable to create Session in Quickblox in Android

I am new to Quickblox. I am trying to work on webrtc. I followed the tutorial here. I have also referred to QuickBlox Error with Custom Object (Android). I am getting the following errors when I try to create a session:
Authentication error: Unable to respond to any of these challenges: {}
D/QBASDK(31816): STATUS : 401
D/QBASDK(31816): '{"errors":["Unauthorized"]}'
My full code is pasted below:
public class MainActivity extends ActionBarActivity implements OnClickListener,
QBRTCClientSessionCallbacks, QBRTCClientConnectionCallbacks,
QBRTCClientVideoTracksCallbacks {
QBChatService chatService;
QBUser user1, user2;
QBGLVideoView localVideoVidew;
Button btn_session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_session = (Button) findViewById(R.id.btn_session);
btn_session.setOnClickListener(MainActivity.this);
/*
* user1 = new User(1, "User1", "user1", "1234", 1); user2 = new User(2,
* "User2", "user2", "1234", 2);
*/
if (!QBChatService.isInitialized()) {
QBChatService.init(MainActivity.this);
chatService.getInstance();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_session:
create_session();
// create_SignallingManager();
break;
default:
break;
}
}
public void create_SignallingManager() {
// TODO Auto-generated method stub
QBChatService
.getInstance()
.getVideoChatWebRTCSignalingManager()
.addSignalingManagerListener(
new QBVideoChatSignalingManagerListener() {
#Override
public void signalingCreated(
QBSignaling qbSignaling, boolean arg1) {
// TODO Auto-generated method stub
if (!arg1) {
QBRTCClient.getInstance().addSignaling(
(QBWebRTCSignaling) qbSignaling);
}
}
});
QBRTCClient.getInstance().setCameraErrorHendler(
new VideoCapturerAndroid.CameraErrorHandler() {
#Override
public void onCameraError(final String s) {
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this, s,
Toast.LENGTH_LONG).show();
}
});
}
});
QBRTCClient.getInstance()
.addSessionCallbacksListener(MainActivity.this);
QBRTCClient.getInstance().addConnectionCallbacksListener(
MainActivity.this);
QBRTCClient.getInstance().addVideoTrackCallbacksListener(
MainActivity.this);
QBRTCClient.getInstance().prepareToProcessCalls(MainActivity.this);
}
private void create_session() {
QBSettings.getInstance().fastConfigInit("XXXX", "XXXXXXX", "XXXXX");//Have put ids properly in code
final QBUser user = new QBUser("user1", "1234");
QBAuth.createSession(user, new QBCallback() {
#Override
public void onComplete(Result arg0, Object arg1) {
// TODO Auto-generated method stub
QBUsers.signIn(user,new QBCallback() {
#Override
public void onComplete(Result arg0, Object arg1) {
// TODO Auto-generated method stub
}
#Override
public void onComplete(Result arg0) {
// TODO Auto-generated method stub
if(arg0.isSuccess())
{
Log.i("working","fine");
}
}
});
}
#Override
public void onComplete(Result arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onLocalVideoTrackReceive(QBRTCSession arg0,
QBRTCVideoTrack videoTrack) {
// TODO Auto-generated method stub
localVideoVidew = (QBGLVideoView) findViewById(R.id.localVideoView);
Log.d("TAG", "localVideoVidew is " + localVideoVidew);
if (localVideoVidew != null) {
videoTrack.addRenderer(new VideoRenderer(new VideoCallBacks(
localVideoVidew, QBGLVideoView.Endpoint.LOCAL)));
localVideoVidew.setVideoTrack(videoTrack,
QBGLVideoView.Endpoint.LOCAL);
Log.d("TAG", "onLocalVideoTrackReceive() is raned");
}
}
#Override
public void onRemoteVideoTrackReceive(QBRTCSession arg0,
QBRTCVideoTrack arg1, Integer arg2) {
// TODO Auto-generated method stub
}
#Override
public void onConnectedToUser(QBRTCSession arg0, Integer arg1) {
// TODO Auto-generated method stub
}
#Override
public void onConnectionClosedForUser(QBRTCSession arg0, Integer arg1) {
// TODO Auto-generated method stub
}
#Override
public void onConnectionFailedWithUser(QBRTCSession arg0, Integer arg1) {
// TODO Auto-generated method stub
}
#Override
public void onDisconnectedFromUser(QBRTCSession arg0, Integer arg1) {
// TODO Auto-generated method stub
}
#Override
public void onDisconnectedTimeoutFromUser(QBRTCSession arg0, Integer arg1) {
// TODO Auto-generated method stub
}
#Override
public void onError(QBRTCSession arg0, QBRTCException arg1) {
// TODO Auto-generated method stub
}
#Override
public void onStartConnectToUser(QBRTCSession arg0, Integer arg1) {
// TODO Auto-generated method stub
}
#Override
public void onCallRejectByUser(QBRTCSession arg0, Integer arg1,
Map<String, String> arg2) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveHangUpFromUser(QBRTCSession arg0, Integer arg1) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveNewSession(QBRTCSession arg0) {
// TODO Auto-generated method stub
}
#Override
public void onSessionClosed(QBRTCSession arg0) {
// TODO Auto-generated method stub
}
#Override
public void onSessionStartClose(QBRTCSession arg0) {
// TODO Auto-generated method stub
}
#Override
public void onUserNotAnswer(QBRTCSession arg0, Integer arg1) {
// TODO Auto-generated method stub
}
}
401 Unauthorized
The request requires user authentication. Basic and Digest Access Authentication"
You are missing onSuccessMethod().
QBSettings.getInstance().fastConfigInit("XXXX", "XXXXXXX", "XXXXX");//Have put ids properly in code
final QBUser user = new QBUser("user1", "1234");
// then the user will be logged in automatically
QBAuth.createSession(login, password, new QBEntityCallbackImpl<QBSession>() {
#Override
public void onSuccess(QBSession session, Bundle bundle) {
user.setId(session.getUserId());
// INIT CHAT SERVICE
if (!QBChatService.isInitialized()) {
QBChatService.init(this);
chatService = QBChatService.getInstance();
}
// LOG IN CHAT SERVICE
chatService.login(user, new QBEntityCallbackImpl<QBUser>() {
#Override
public void onSuccess() {
// success
}
#Override
public void onError(List errors) {
//error
}
});
}
#Override
public void onError(List<String> errors) {
//error
}
});
Unauthorized means that user's login or password are wrong, you should check this first
Use this method as per notes here
http://quickblox.com/developers/Android#How_to:_add_SDK_to_IDE_and_connect_to_the_cloud
Note there is no "User" field for this create session method
QBAuth.createSession(new QBEntityCallbackImpl() {}

Unable to play Video in Android LsitView

I have a listView in which
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int pos, long id) {
player.cueVideo("KJ9KzGQq0w");
}
});
private YouTubePlayer player= new YouTubePlayer() {
#Override
public void setShowFullscreenButton(boolean arg0) {
// TODO Auto-generated method stub
}
#Override
public void setPlaylistEventListener(PlaylistEventListener arg0) {
// TODO Auto-generated method stub
}
#Override
public void setPlayerStyle(PlayerStyle arg0) {
// TODO Auto-generated method stub
}
#Override
public void setPlayerStateChangeListener(PlayerStateChangeListener arg0) {
// TODO Auto-generated method stub
}
#Override
public void setPlaybackEventListener(PlaybackEventListener arg0) {
// TODO Auto-generated method stub
}
#Override
public void setOnFullscreenListener(OnFullscreenListener arg0) {
// TODO Auto-generated method stub
}
#Override
public void setManageAudioFocus(boolean arg0) {
// TODO Auto-generated method stub
}
#Override
public void setFullscreenControlFlags(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void setFullscreen(boolean arg0) {
// TODO Auto-generated method stub
}
#Override
public void seekToMillis(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void seekRelativeMillis(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void release() {
// TODO Auto-generated method stub
}
#Override
public void previous() {
// TODO Auto-generated method stub
}
#Override
public void play() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void next() {
// TODO Auto-generated method stub
}
#Override
public void loadVideos(List<String> arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void loadVideos(List<String> arg0) {
// TODO Auto-generated method stub
}
#Override
public void loadVideo(String arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void loadVideo(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void loadPlaylist(String arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void loadPlaylist(String arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean isPlaying() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean hasPrevious() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
#Override
public int getFullscreenControlFlags() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getDurationMillis() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getCurrentTimeMillis() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void cueVideos(List<String> arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void cueVideos(List<String> arg0) {
// TODO Auto-generated method stub
}
#Override
public void cueVideo(String arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void cueVideo(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void cuePlaylist(String arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void cuePlaylist(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void addFullscreenControlFlag(int arg0) {
// TODO Auto-generated method stub
}
};
I am using YouTubeAPI for doing this
sample code in API
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,boolean wasRestored) {
if (!wasRestored) {
//player.cueVideo("wKJ9KzGQq0w");
}
}
I want to play a video as soon as an element in listView is clicked.
Change your player.cueVideo("wKJ9KzGQq0w"); to player.loadVideo("wKJ9KzGQq0w");.
loadVideo will automatically play when it has finished loading.
Made few changes
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player1,boolean wasRestored) {
player=player1;
}
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int pos, long id) {
video_URL="eF9AC2Ce2ow";
video_player.loadVideo(video_URL);
}});

AdListner not working properly

I wish to remove the ad if they are clicked once.
I implemented the following code.
I also added android:launchMode="singleInstance"
in the manifest and android:alwaysRetainTaskState="true" in the activity section of the manifest.
But when i click on the ad and return to the app the ad is still showing .
My code is as follows.
public class MainActivity extends Activity implements AdListener{
adView = (AdView)findViewById(R.id.ad);
adView.loadAd(new AdRequest());
// my code
#Override
public void onDismissScreen(Ad arg0) {
if (adView != null) {
adView.destroy();
}
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}
adView.setAdListener(this); /* first you should add listener register */
and for dismiss screen
public void onDismissScreen(Ad p1)
{
yourlayout.removeView(adView); // your layout should me remove here
}

LibGdx - need help on Screen class

I've been using libgdx for the past 3 weeks..
I created two screens referencing to a game:
public class Game extends Game {
TitleScreen titleScreen;
SplashScreen splashScreen;
#Override
public void create() {
// TODO Auto-generated method stub
titleScreen = new TitleScreen(this);
splashScreen = new SplashScreen(this);
setScreen(splashScreen);
}
}
and
public class SplashScreen implements Screen {
Game game;
public SplashScreen(Game game) {
// TODO Auto-generated constructor stub
this.game = game;
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(0.5f, 1f, 1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if(Gdx.input.justTouched()){
game.splashScreen.dispose();
game.setScreen(game.titlescreen);
}
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
How do I dispose the splashscreen entirely? When I called the game.setscreen(game.splashscreen) in titlescreen class, it resume the splashscreen;. I'm expecting to set a new splashscreen..
Create a new splash screen every time you want to show a splash from the beginning:
game.setScreen(new SplashScreen(game));

Categories

Resources