'android'에 해당되는 글 47건

* activity_mythumbnail.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="wrap_content"
    android:orientation="vertical">

    <com.google.android.youtube.player.YouTubeThumbnailView
        android:id="@+id/thumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:adjustViewBounds="true" />

</LinearLayout>

* MyThumbnail.java

public class MyThumbnail extends AppCompatActivity implements YouTubeThumbnailView.OnInitializedListener {

    public static final String API_KEY = "API_KEY";
    private YouTubeThumbnailLoader youTubeThumbnailLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);
		
        YouTubeThumbnailView thumbnail = findViewById(R.id.thumbnail);
        thumbnail.initialize(API_KEY, this);
	}

    @Override
    public void onInitializationFailure(YouTubeThumbnailView thumbnailView, YouTubeInitializationResult errorReason) {

    }

    @Override
    public void onInitializationSuccess(YouTubeThumbnailView thumbnailView, YouTubeThumbnailLoader thumbnailLoader) {
        youTubeThumbnailLoader = thumbnailLoader;
        thumbnailLoader.setOnThumbnailLoadedListener(new ThumbnailListener());

        youTubeThumbnailLoader.setVideo(VIDEO_ID);
    }

    private final class ThumbnailListener implements YouTubeThumbnailLoader.OnThumbnailLoadedListener {
        @Override
        public void onThumbnailLoaded(YouTubeThumbnailView thumbnail, String videoId) {

        }

        @Override
        public void onThumbnailError(YouTubeThumbnailView thumbnail, YouTubeThumbnailLoader.ErrorReason reason) {

        }
    }

}

'android' 카테고리의 다른 글

가로세로 전환시 새로고침 안되게 유지  (0) 2020.02.21
YouTube API 플레이어  (0) 2020.02.19
WebView 자바스크립트 실행하기  (0) 2020.02.17
WebView 사용하기  (0) 2020.02.13
ExoPlayer 사용법  (0) 2020.02.13
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,
public class WebViewActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

		String url = "http://www.test.com";

        webView = findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url); //웹뷰 실행
        webView.setWebChromeClient(new WebChromeClient()); // 웹뷰에 크롬 사용 허용, 이 부분이 없으면 크롬에서 alert 뜨지 않음
        webView.setWebViewClient(new WebViewClientClass()); // 새창열기 없이 웹뷰 내에서 다시 열기, 페이지 이동 원활히 하기 위해 사용
        webView.addJavascriptInterface(new WebViewJavascriptInterface(), "Android");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) { // 뒤로가기 버튼 이벤트
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // 웹뷰에서 뒤로가기 버튼을 누르면 뒤로 이동
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class WebViewClientClass extends WebViewClient { // 페이지 이동시 새창으로 안뜨게
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            view.loadUrl("javascript:window.Android.getHtml(document.getElementsByTagName('html')[0].innerHTML);");
        }
    }

    public class WebViewJavascriptInterface {
        @JavascriptInterface
        public void getHtml(String html) {
            Log.d(TAG, html);
        }
    }

}

'android' 카테고리의 다른 글

YouTube API 플레이어  (0) 2020.02.19
YouTube 썸네일(Thumbnail)  (0) 2020.02.19
WebView 사용하기  (0) 2020.02.13
ExoPlayer 사용법  (0) 2020.02.13
Fragment에서 Intent 호출시  (0) 2020.02.12
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,

WebView 사용하기

android 2020. 2. 13. 15:08
public class PlayerActivity extends AppCompatActivity {

    private WebView webView;
    private String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        Intent intent = getIntent();
        String url = intent.getStringExtra("url");

        webView = findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.clearCache(true);
        webView.loadUrl(url); //웹뷰 실행
        webView.setWebChromeClient(new WebChromeClient()); // 웹뷰에 크롬 사용 허용, 이 부분이 없으면 크롬에서 alert 뜨지 않음
        webView.setWebViewClient(new WebViewClientClass()); // 새창열기 없이 웹뷰 내에서 다시 열기, 페이지 이동 원활히 하기 위해 사용
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) { // 뒤로가기 버튼 이벤트
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // 웹뷰에서 뒤로가기 버튼을 누르면 뒤로 이동
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class WebViewClientClass extends WebViewClient { // 페이지 이동시 새창으로 안뜨게
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

}

'android' 카테고리의 다른 글

YouTube 썸네일(Thumbnail)  (0) 2020.02.19
WebView 자바스크립트 실행하기  (0) 2020.02.17
ExoPlayer 사용법  (0) 2020.02.13
Fragment에서 Intent 호출시  (0) 2020.02.12
GestureDetector Swipe  (0) 2020.02.12
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,

ExoPlayer 사용법

android 2020. 2. 13. 00:31

* Gradle

android {
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    google()
    jcenter()
}

dependencies {
    implementation 'com.google.android.exoplayer:exoplayer:2.8.4'
}

* AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<application android:usesCleartextTraffic="true" />

* PlayerActivity.java

public class PlayerActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static class EventListener extends Player.DefaultEventListener {
        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            String stateString;
            switch (playbackState) {
                case Player.STATE_IDLE:
                    stateString = "ExoPlayer.STATE_IDLE";
                    break;
                case Player.STATE_BUFFERING:
                    stateString = "ExoPlayer.STATE_BUFFERING";
                    break;
                case Player.STATE_READY:
                    stateString = "ExoPlayer.STATE_READY";
                    break;
                case Player.STATE_ENDED:
                    stateString = "ExoPlayer.STATE_ENDED";
                    break;
                default:
                    stateString = "UNKNOWN_STATE";
                    break;
            }
            Log.d(TAG, "changed state to " + stateString + ", playWhenReady: " + playWhenReady);
        }
    }

    private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
    private SimpleExoPlayer exoPlayer;
    private long playbackPosition;
    private int currentWindow;
    private boolean playWhenReady = true;

    private EventListener eventListener = new EventListener();

    String url = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);

        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        initPlayer(url);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initPlayer(url);
    }

    @Override
    protected void onPause() {
        super.onPause();
        releasePlayer();
    }

    @Override
    protected void onStop() {
        super.onStop();
        releasePlayer();
    }

    private void initPlayer(String url) {
        if (url.isEmpty()) {
            Log.d(TAG, "url empty");
            return;
        }

        PlayerView playerView = findViewById(R.id.playerView);

        TrackSelection.Factory adaptiveTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
        exoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector(adaptiveTrackSelectionFactory), new DefaultLoadControl());
        exoPlayer.addListener(new EventListener());


        MediaSource mediaSource = url.endsWith(".m3u8") ? buildMediaSourceHLS(Uri.parse(url)) : buildMediaSourceVideo(Uri.parse(url));
        exoPlayer.prepare(mediaSource, true, false);
        exoPlayer.setPlayWhenReady(playWhenReady);
        exoPlayer.seekTo(currentWindow, playbackPosition);
        playerView.setPlayer(exoPlayer);
    }

    private void releasePlayer() {
        if (exoPlayer != null) {
            playbackPosition = exoPlayer.getCurrentPosition();
            currentWindow = exoPlayer.getCurrentWindowIndex();
            playWhenReady = exoPlayer.getPlayWhenReady();
            exoPlayer.removeListener(eventListener);
            exoPlayer.release();
            exoPlayer = null;
        }
    }

    private MediaSource buildMediaSourceHLS(Uri uri) {
        String userAgent = System.getProperty("http.agent");
        Log.d(TAG, "UserAgent : " + userAgent);
        DataSource.Factory manifestDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
        HlsMediaSource hlsMediaSource = new HlsMediaSource.Factory(manifestDataSourceFactory).createMediaSource(uri);

        return new ConcatenatingMediaSource(hlsMediaSource);
    }

    private MediaSource buildMediaSourceVideo(Uri uri) {
        String userAgent = System.getProperty("http.agent");
        Log.d(TAG, "UserAgent : " + userAgent);
        ExtractorMediaSource videoSource = new ExtractorMediaSource.Factory(new DefaultHttpDataSourceFactory(TextUtils.isEmpty(userAgent) ? "DefaultHttpDataSourceFactory" : userAgent)).createMediaSource(uri);

        return new ConcatenatingMediaSource(videoSource);
    }

}

* activity_main.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"
    android:background="#000000">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

'android' 카테고리의 다른 글

WebView 자바스크립트 실행하기  (0) 2020.02.17
WebView 사용하기  (0) 2020.02.13
Fragment에서 Intent 호출시  (0) 2020.02.12
GestureDetector Swipe  (0) 2020.02.12
ProgressDialog  (0) 2020.02.10
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,

* Fragment에서는 Activity를 호출하지 못하므로 부모 Acitivity를 이용하여 호출

Intent intent = new Intent(getActivity(), NextActivity.class);
startActivity(intent);

'android' 카테고리의 다른 글

WebView 사용하기  (0) 2020.02.13
ExoPlayer 사용법  (0) 2020.02.13
GestureDetector Swipe  (0) 2020.02.12
ProgressDialog  (0) 2020.02.10
연락처 가져오기  (0) 2020.02.05
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,

GestureDetector Swipe

android 2020. 2. 12. 12:37
GestureDetector gestureDetector;
question.setOnTouchListener(new View.OnTouchListener() {
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		gestureDetector.onTouchEvent(event);
		return true;
	}
});
gestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
    
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (velocityX < -2000) {
			//
		} if (velocityX > 2000) {
			//
		}

		return true;
	}
});

'android' 카테고리의 다른 글

ExoPlayer 사용법  (0) 2020.02.13
Fragment에서 Intent 호출시  (0) 2020.02.12
ProgressDialog  (0) 2020.02.10
연락처 가져오기  (0) 2020.02.05
갤러리 이미지 가져오기  (0) 2020.02.05
블로그 이미지

디츠

“말은 쉽지, 코드를 보여줘.” “Talk is cheap. Show me the code.” – 리누스 토르발스(Linus Torvalds)

,