/*
*
* Copyright (c) 2010-2014 EVE GROUP PTE. LTD.
*
*/
package veg.mediaplayer.sdk.test.slidingviews;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.view.inputmethod.InputMethodManager;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.*;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView.OnEditorActionListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
import android.preference.PreferenceManager;
import veg.mediaplayer.sdk.MediaPlayer;
import veg.mediaplayer.sdk.MediaPlayer.PlayerNotifyCodes;
import veg.mediaplayer.sdk.MediaPlayer.PlayerState;
import veg.mediaplayer.sdk.MediaPlayerConfig;
import veg.mediaplayer.sdk.test.slidingviews.SurfacePageFragment.SurfacePageFragmentCallback;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
public class MainActivity extends FragmentActivity implements SurfacePageFragmentCallback, MediaPlayer.MediaPlayerCallback
{
public static AutoCompleteTextView edtIpAddress;
public static ArrayAdapter<String> edtIpAddressAdapter;
public static Set<String> edtIpAddressHistory;
private Button btnConnect;
private Button btnHistory;
private SharedPreferences settings;
private SharedPreferences.Editor editor;
//private boolean playing = false;
private MainActivity mthis = null;
//private Surface surface = null;
//private boolean isFullScreen = false;
//private static final int NUM_PAGES = 5;
private MediaPlayer[] players = new MediaPlayer[4];
private ViewPager mPager = null;
private PagerAdapter mPagerAdapter = null;
private Switch switchDecoderType = null;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
PlayerNotifyCodes status = (PlayerNotifyCodes) msg.obj;
if (status == PlayerNotifyCodes.CP_CONNECT_STARTING)
{
//player1.setVisibility(View.VISIBLE);
}
if (status == PlayerNotifyCodes.PLP_CLOSE_STARTING)
{
//player1.setVisibility(View.INVISIBLE);
}
SurfacePageFragment page = (SurfacePageFragment)mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if (page == null)
return;
int position = page.getPageNumber();
if (players[position].getState() == PlayerState.Closed)
btnConnect.setText("Connect");
}
};
// callback from Native Player
@Override
public int Status(int arg)
{
Log.e(TAG, "Form Native Player status: " + arg);
Message msg = new Message();
msg.obj = PlayerNotifyCodes.forValue(arg);
if (handler != null)
handler.sendMessage(msg);
return 0;
}
@Override
public int OnReceiveData(ByteBuffer buffer, int size, long pts)
{
return 0;
}
@Override
public void onSurfaceCreated(int position, Surface surface)
{
Log.d(TAG, "onSurfaceCreated: pos:" + position + ", surface: " + surface);
SurfacePageFragment page = (SurfacePageFragment)mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if (page == null)
return;
players[position].setSurface(surface);
}
@Override
public void onSurfaceChanged(int position, Surface surface, int newWidth, int newHeight)
{
Log.d(TAG, "onSurfaceChanged: pos:" + position + ", surface: " + surface + ", newWidth: " + newWidth + ", newHeight: " + newHeight);
SurfacePageFragment page = (SurfacePageFragment)mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if (page == null)
return;
players[position].setSurface(surface, newWidth, newHeight);
players[position].UpdateView();
}
@Override
public void onSurfaceDestroyed(int position, Surface surface)
{
Log.d(TAG, "onSurfaceDestroyed: pos:" + position + ", surface: " + surface);
SurfacePageFragment page = (SurfacePageFragment)mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if (page == null)
return;
players[position].setSurface(null);
}
@Override
public void onSurfaceTouched(int position)
{
Log.d(TAG, "onSurfaceTouched: pos:" + position);
SurfacePageFragment page = (SurfacePageFragment)mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if (page == null)
return;
Log.d(TAG, "onSurfaceTouched state = " + players[position].getState());
// if (players[position].getState() == PlayerState.Started)
// {
// players[position].Pause();
// }
// else
// if (players[position].getState() == PlayerState.Paused)
// {
// players[position].Play();
// }
}
protected float pxFromDp(float dp)
{
return (dp * getResources().getDisplayMetrics().density);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onCreate(Bundle savedInstanceState)
{
String strUrl;
setTitle(R.string.app_name);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mthis = this;
for (int i = 0; i < players.length; i++) {
players[i] = new MediaPlayer(this, false);
}
Log.e(TAG, "MediaPlayer instances: " + players.length);
settings = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
strUrl = settings.getString("connectionUrl", "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov");
HashSet<String> tempHistory = new HashSet<String>();
tempHistory.add("http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8");
tempHistory.add("rtmp://184.72.239.149/vod/mp4:bigbuckbunny_450.mp4");
tempHistory.add("rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov");
tempHistory.add("rtsp://rtmp.infomaniak.ch/livecast/latele");
Set<String> savedHistory = settings.getStringSet("connectionHistory", tempHistory);
edtIpAddressHistory = new HashSet<String>();
edtIpAddressHistory.addAll(tempHistory);
edtIpAddressHistory.addAll(savedHistory);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
edtIpAddress = (AutoCompleteTextView)findViewById(R.id.edit_ipaddress);
edtIpAddress.setText(strUrl);
edtIpAddress.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
{
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(edtIpA