Exoplayer keeps playing the audio even when video is stopped after - android

I am using exoplayer and I noticed when I play a video in one activity and then switch to another by pressing on a button the video itself stops playing in order to switch to the new activity, but the audio keeps playing. If I return to the old activity and play the video again the audio is still going + I hear the audio from playing the video again and hear the audio two times now simulteanously at different times of the song.
String url = model.getVideo();
try {
BandwidthMeter bandwidthMeter= new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory( bandwidthMeter));
holder.exoPlayer= ExoPlayerFactory.newSimpleInstance( LiFit.getContext(),trackSelector );
Uri videouri = Uri.parse(model.getVideo());
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");
ExtractorsFactory extractorsFactory= new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videouri,dataSourceFactory,extractorsFactory,null,null);
holder.videoView.setPlayer(holder.exoPlayer);
holder.exoPlayer.prepare(mediaSource);
//holder.exoPlayer.setPlayWhenReady(true);
} catch (Exception e) {
//Nichts
}
This is the code I am using does somebody know a solution for this problem ?

exoplayer has a method setPlayWhenReady which can be used to play/pause the media, and a release method when the player is no longer needed.
You can use these methods when switching to another activity.

Related

Is it possible to play a rtmp livestream in android app

Does anyone know if it’s possible to play a rtmp stream with exoplayer.
I’ve tried this but didn’t manage to get it working:
ExoPlayer player = new ExoPlayer.Builder(this).build();
playerView.setPlayer(player);
Uri uri = Uri.parse("rtmp://192.168.109.84/live");
MediaItem mediaItem = MediaItem.fromUri(uri);
RtmpDataSource.Factory rtmpDataSourceFactory = new RtmpDataSource.Factory();
MediaSource mediaSource = new ProgressiveMediaSource.Factory(rtmpDataSourceFactory).createMediaSource(mediaItem);
player.setMediaSource(mediaSource);
player.prepare();
player.setPlayWhenReady(true);

Exoplayer video stop when audio is muted (encoder is flash)

Hi I am creating an app that will broadcast live and another user can view it
I am currently using flash to push it into WOWZA and I am using exoplayer for viewing it
I am using RTMP because it's the only one supported by exoplayer which has low latency
everything works perfect however when I mute the microphone in the live side the video in exoplayer will freeze and will go to status
STATE_BUFFERING but it will not proceed to STATE_READY
and if I unmute it will go back to normal and can view it again from
STATE_BUFFERING to STATE_READY
And anyway I tried the stream to view in VLC and it does work it doesn't freeze
I tried other solution but doesn't work
my code is this
//initiate Player
//Create a default TrackSelector
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
//Create the player
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
PlayerView playerView = findViewById(R.id.simple_player);
playerView.setPlayer(player);
RtmpDataSourceFactory rtmpDataSourceFactory = new RtmpDataSourceFactory();
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ExtractorMediaSource.Factory(rtmpDataSourceFactory)
.createMediaSource(Uri.parse(URL));
player.prepare(videoSource);
//auto start playing
player.setPlayWhenReady(true);
I hope someone helps me!!!
thank you in advance

How to implement seekTo() when using HLS with the ExoPlayer

I am trying to implement a basic radio player that can pause the live stream, rewind it and then fast forward it again.
I think that this functionality should be natively supported from version 2.1 of the ExoPlayer.
However, the rewind and fast-forward controls are grayed out when streaming even though they work when playing local content.
Here is how I am creating the player:
private void initExoPlayer(){
Handler mHandler = new Handler();
String userAgent = "userAgent";
Uri uri = Uri.parse(urlSourceOfStream);
dataSourceFactory = new DefaultHttpDataSourceFactory(
userAgent, null,
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
1800000,
true);
mediaSource = new ExtractorMediaSource(uri,dataSourceFactory, Mp3Extractor.FACTORY,
mHandler, null);
bandwidthMeter = new DefaultBandwidthMeter();
trackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
trackSelector = new DefaultTrackSelector(trackSelectionFactory);
loadControl = new DefaultLoadControl();
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
exoPlayer.prepare(mediaSource);
((SimpleExoPlayerView) findViewById(R.id.exoPlayer)).setPlayer(exoPlayer);
}
The player can pause successfully, and it seems to be caching the content as I can resume the player after it has been paused for minutes. This also indicates that there should be some cache that can allow me to rewind the content.
I've also tried using the OkHttpDataSourceFactory :
OkHttpClient client = new OkHttpClient.Builder().cache(new Cache(getFilesDir() , 1000)).build();
OkHttpDataSourceFactory okHttpDataSourceFactory = new OkHttpDataSourceFactory(client, userAgent, null);
Trying to rewind the player like this makes the player play from the live stream position and does not rewind the content:
exoPlayer.seekTo(Math.max(exoPlayer.getCurrentPosition() - 1000, 0));
My question in a nutshell: How can I rewind and fast-forward an HLS with the ExoPlayer?
Thanks in advance.
It looks like there is a bug, https://github.com/google/ExoPlayer/issues/87 discussing this, and it's been closed. But I'm not sure if its reached the v2 release, and rather it might be in the dev release, here

How to detect HLS video is for Live, VOD or Event in Exoplayer android

I have integrated android exoplayer in my application. I have to detect the HLS (.m3u8) stream received is for Live or VOD or Event, so depending on that controller has to be modified for the player. I have only one instance of player so that should handle all the supported media like vod or live or event.
I am looking for some debug points to know working of m3u8 parser in exoplayer so that I can able to receive this parameters.
You can use below code to play .m3u8 file:-
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
//Create the player using ExoPlayerFactory
videoPlayer = ExoPlayerFactory.newSimpleInstance(context,trackSelector);
Handler mHandler = new Handler();
String userAgent = Util.getUserAgent(context, "Exo Player");
DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory(
userAgent, null,
DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
1800000,
true);
HlsMediaSource mediaSource = new HlsMediaSource(Uri.parse(mediaUrl),dataSourceFactory, 1800000,mHandler, null);
if (mediaUrl != null) {
videoPlayer.prepare(mediaSource);
videoPlayer.setPlayWhenReady(true);
}
Live vs Event differentiation is not supported, yet. For Live vs Event you can check the current Timeline, but that's not HLS specific. For distinguish a live stream from a vod one by querying the player's duration after it is prepared. Live streams will return UNKNOWN_TIME, where-as vod streams will return the known duration of the stream.

Stream AAC on Android

I'm working on an app for a client that requires streaming of an AAC audio stream. Unfortunately, there's nothing I can do about the stream format on the server. I'm working on Android and have discovered that Android's media player does not support raw AAC streams (which is what I'm getting). I found a project on Google Code that supports it (I tested it with the stream) but it's GPL'ed and that doesn't work for my client. I don't have much experience with this sort of thing so forgive me if my ideas aren't great. I know Android can play AAC encoded content if it is in an MP4 wrapper so I had thought about creating an MP4 wrapper on the fly on the client-side or perhaps even just doing some conversion to another format on the fly. Are these reasonable options? Does anybody have better suggestions?
Thanks in advance!
Edit To rephrase, is it possible to put a raw AAC stream from a web server in an MP4 container in real time? If so, does anybody know of resources to help me with the process?
I'm wondering if you ever came up with a solution for this problem? I'm in a similar situation. We have a web service generating AAC files, though not raw, they have the ADTS header. Android 2.2 can't play these files, though android 2.1 can, oddly enough. It seems to be a problem rooted to android switching to stagefright libraries for media playback in 2.2.
The AAC files do play fine when wrapped in an MP4 container. We've accomplish this on the server side very easily using FAAC (I realize this doesn't help you in your situation). We are still investigating licensing issues with using an mp4 container though. Does this require royalties?
To answer my own question...a lawyer has confirmed that using an mp4 container for an AAC file does not require licensing.
I TRIED ACCDECODER by Google for 4 motnhs it did work well. I found it and in 2 days I could did it works! :) I found it and saw it on the last Google I/O 2017
I can suggest to use EXOPLAYER INSTEAD MEDIAPLAYER. It is a better and improve way. I had the same problem using just MediaPlayer, but short time ago I found Exoplayer, now I do not have problem to Play audio streaming Acc, mp3, mpeg, etc. I can give a couple links to check it.
ExoPlayer is an open source project that is not part of the Android framework and is distributed separately from the Android SDK. ExoPlayer’s standard audio and video components are built on Android’s MediaCodec API, which was released in Android 4.1 (API level 16). Because ExoPlayer is a library, you can easily take advantage of new features as they become available by updating your app.
ExoPlayer supports features like Dynamic adaptive streaming over HTTP (DASH), SmoothStreaming and Common Encryption, which are not supported by MediaPlayer. It's designed to be easy to customize and extend.
This is almost the same like MediaPlayer because Exoplayer extend from it. You can see a lot of differences and an easy way to implement it. If you need more help let me know :)
For example you have this:
Media Player mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("http://your.url.com"); //add you url address
...
mediaPlayer.prepare();
mediaPlayer.start();
In Exoplayer you need it:
// Create the player
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
simpleExoPlayerView = new SimpleExoPlayerView(this);
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);
//Set media controller
simpleExoPlayerView.setUseController(true);
simpleExoPlayerView.requestFocus();
// Bind the player to the view.
simpleExoPlayerView.setPlayer(player);
ExoPlayer example code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private SimpleExoPlayerView simpleExoPlayerView;
private SimpleExoPlayer player;
private ExoPlayer.EventListener exoPlayerEventListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG,"portrait detected...");
setContentView(R.layout.activity_main);
// 1. Create a default TrackSelector
Handler mainHandler = new Handler();
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveVideoTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory);
// 2. Create a default LoadControl
LoadControl loadControl = new DefaultLoadControl();
// 3. Create the player
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
simpleExoPlayerView = new SimpleExoPlayerView(this);
simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);
//Set media controller
simpleExoPlayerView.setUseController(true);
simpleExoPlayerView.requestFocus();
// Bind the player to the view.
simpleExoPlayerView.setPlayer(player);
//CHOOSE CONTENT: Livestream links may be out of date so find any m3u8 files online and replace:
//VIDEO FROM SD CARD:
// String urimp4 = "/FileName.mp4";
// Uri mp4VideoUri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+urimp4);
//yachts livestream m3m8 file:
Uri mp4VideoUri =Uri.parse("http://your.url.com");
//Random livestream file:
// Uri mp4VideoUri =Uri.parse("http://your.url.com");
//Sports livestream file:
// Uri mp4VideoUri =Uri.parse("http://your.url.com");
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();
//Produces DataSource instances through which media data is loaded.
// DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), bandwidthMeterA);
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), bandwidthMeterA);
//Produces Extractor instances for parsing the media data.
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
//This is the MediaSource representing the media to be played:
//FOR SD CARD SOURCE:
// MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri, dataSourceFactory, extractorsFactory, null, null);
//FOR LIVESTREAM LINK:
MediaSource videoSource = new HlsMediaSource(mp4VideoUri, dataSourceFactory, 1, null, null);
final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);
// Prepare the player with the source.
player.prepare(loopingSource);
player.addListener(new ExoPlayer.EventListener() {
#Override
public void onLoadingChanged(boolean isLoading) {
Log.v(TAG,"Listener-onLoadingChanged...");
}
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
Log.v(TAG,"Listener-onPlayerStateChanged...");
}
#Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
Log.v(TAG,"Listener-onTimelineChanged...");
}
#Override
public void onPlayerError(ExoPlaybackException error) {
Log.v(TAG,"Listener-onPlayerError...");
player.stop();
player.prepare(loopingSource);
player.setPlayWhenReady(true);
}
#Override
public void onPositionDiscontinuity() {
Log.v(TAG,"Listener-onPositionDiscontinuity...");
}
});
player.setPlayWhenReady(true);
}//End of onCreate
#Override
protected void onStop() {
super.onStop();
Log.v(TAG,"onStop()...");
}
#Override
protected void onStart() {
super.onStart();
Log.v(TAG,"onStart()...");
}
#Override
protected void onResume() {
super.onResume();
Log.v(TAG,"onResume()...");
}
#Override
protected void onPause() {
super.onPause();
Log.v(TAG,"onPause()...");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.v(TAG,"onDestroy()...");
player.release();
}
}
It was a short example, If you are looking for something else or need more help let me know! I am here! I would like to help because I know how it feels when you are googling and searching and anything it is showing up! :) Take care!!

Categories

Resources