Initial commit: Complete project setup
Add all project files and configuration 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
157
app/src/main/java/com/streamplayer/MainActivity.java
Normal file
157
app/src/main/java/com/streamplayer/MainActivity.java
Normal file
@@ -0,0 +1,157 @@
|
||||
package com.streamplayer;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.StrictMode;
|
||||
import android.view.View;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.google.android.exoplayer2.ExoPlayer;
|
||||
import com.google.android.exoplayer2.MediaItem;
|
||||
import com.google.android.exoplayer2.PlaybackException;
|
||||
import com.google.android.exoplayer2.Player;
|
||||
import com.google.android.exoplayer2.ui.PlayerView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private ExoPlayer player;
|
||||
private PlayerView playerView;
|
||||
private ProgressBar loadingIndicator;
|
||||
private TextView errorMessage;
|
||||
|
||||
private static final String STREAM_PAGE_URL = "https://streamtpmedia.com/global2.php?stream=espn";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Configurar política de red para allow cleartext traffic
|
||||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
||||
StrictMode.setThreadPolicy(policy);
|
||||
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
initViews();
|
||||
|
||||
// Configurar DNS de Google para streaming
|
||||
DNSSetter.configureDNSToGoogle(this);
|
||||
|
||||
initializePlayer();
|
||||
}
|
||||
|
||||
private void initViews() {
|
||||
playerView = findViewById(R.id.player_view);
|
||||
loadingIndicator = findViewById(R.id.loading_indicator);
|
||||
errorMessage = findViewById(R.id.error_message);
|
||||
}
|
||||
|
||||
private void initializePlayer() {
|
||||
showLoading(true);
|
||||
new Thread(() -> {
|
||||
try {
|
||||
String resolvedUrl = StreamUrlResolver.resolve(STREAM_PAGE_URL);
|
||||
runOnUiThread(() -> startPlayback(resolvedUrl));
|
||||
} catch (Exception e) {
|
||||
runOnUiThread(() -> showError("Error al obtener stream: " + e.getMessage()));
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void startPlayback(String streamUrl) {
|
||||
try {
|
||||
releasePlayer();
|
||||
player = new ExoPlayer.Builder(this).build();
|
||||
playerView.setPlayer(player);
|
||||
|
||||
player.addListener(new Player.Listener() {
|
||||
@Override
|
||||
public void onPlaybackStateChanged(int playbackState) {
|
||||
switch (playbackState) {
|
||||
case Player.STATE_BUFFERING:
|
||||
showLoading(true);
|
||||
break;
|
||||
case Player.STATE_READY:
|
||||
showLoading(false);
|
||||
break;
|
||||
case Player.STATE_ENDED:
|
||||
// Video terminado
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerError(PlaybackException error) {
|
||||
showError("Error al reproducir: " + error.getMessage());
|
||||
}
|
||||
});
|
||||
|
||||
MediaItem mediaItem = MediaItem.fromUri(streamUrl);
|
||||
player.setMediaItem(mediaItem);
|
||||
player.prepare();
|
||||
player.setPlayWhenReady(true);
|
||||
|
||||
} catch (Exception e) {
|
||||
showError("Error al inicializar reproductor: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void showLoading(boolean show) {
|
||||
loadingIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
|
||||
errorMessage.setVisibility(View.GONE);
|
||||
playerView.setVisibility(show ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
|
||||
private void showError(String message) {
|
||||
loadingIndicator.setVisibility(View.GONE);
|
||||
playerView.setVisibility(View.GONE);
|
||||
errorMessage.setVisibility(View.VISIBLE);
|
||||
errorMessage.setText(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
if (player != null) {
|
||||
playerView.onResume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (player != null) {
|
||||
playerView.onResume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (player != null) {
|
||||
playerView.onPause();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
if (player != null) {
|
||||
playerView.onPause();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
releasePlayer();
|
||||
}
|
||||
|
||||
private void releasePlayer() {
|
||||
if (player != null) {
|
||||
player.release();
|
||||
player = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user