- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我花了几个小时在 android 中开发一个合适的流媒体视频播放器,并且非常成功地创建了一个可以很好地播放诸如歌曲、预告片等小内容的播放器。但是播放器对于电影和电视节目等大型内容表现出一些异常行为,因为它需要大量流式传输,播放器开始滞后于此类数据。谁能帮我破解这样一个问题的解决方案。
提前致谢...
这是来源:
public class player extends Activity implements OnErrorListener,
OnPreparedListener {
/** Called when the activity is first created. */
private static final int UPDATE_FREQUENCY = 500;
private static final int STEP_VALUE = 4000;
private static final int DIALOG_KEY = 0;
private TextView currentTime, duration;
private VideoView videoView;
private SeekBar seekbar = null;
private View mediacontroller;
private ProgressDialog progressDialog = null;
private ImageButton playButton = null;
private ImageButton prevButton = null;
private ImageButton nextButton = null;
private boolean isMoveingSeekBar = false;
private boolean isMediaCtrlShown = false;
private final Handler handler = new Handler();
private boolean isStarted = true;
private String currentFile = "singham_320b";
private boolean isCustomSeekButtonClick = false;
private boolean isPauseButtonClick = false;
private static boolean isMyDialogShowing = false;
private static int percentageBuffer = 0;
private int mpCurrentPosition;
int hh = 00, mm = 00, ss = 00, ms = 00;
int i = 0;
int previouPosition = 0;
private Runnable onEverySecond=new Runnable() {
public void run() {
if (videoView!=null) {
seekbar.setProgress(videoView.getCurrentPosition());
}
if (!isPauseButtonClick) {
mediacontroller.postDelayed(onEverySecond, 1000);
}
}
};
private final Runnable updatePositionRunnable = new Runnable()
{
public void run()
{
updatePosition();
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpMyDialog();
showMyDialog();
videoView = (VideoView) findViewById(R.id.videoview);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
seekbar = (SeekBar) findViewById(R.id.seekbar);
currentTime = (TextView) findViewById(R.id.currentTime);
playButton = (ImageButton) findViewById(R.id.play);
prevButton = (ImageButton) findViewById(R.id.prev);
nextButton = (ImageButton) findViewById(R.id.next);
duration = (TextView) findViewById(R.id.duration);
mediacontroller = findViewById(R.id.mediacontroller);
videoView.setOnErrorListener(this);
videoView.setOnPreparedListener(this);
videoView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (!isMediaCtrlShown)
{
mediacontroller.setVisibility(View.GONE);
isMediaCtrlShown = true;
}
else
{
mediacontroller.setVisibility(View.VISIBLE);
isMediaCtrlShown = false;
}
return false;
}
});
Uri video = Uri.parse("http://wpc.1B42.edgecastcdn.net/001B42/mobile/songs/pyaar_ka_punchnama/life_sahi_hai_320b.mp4");
videoView.setVideoURI(video);
seekbar.setOnSeekBarChangeListener(seekBarChanged);
playButton.setOnClickListener(onButtonClick);
nextButton.setOnClickListener(onButtonClick);
prevButton.setOnClickListener(onButtonClick);
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
return false;
}
public void calculateTime(int ms) {
ss = ms / 1000;
mm = ss / 60;
ss %= 60;
hh = mm / 60;
mm %= 60;
hh %= 24;
}
@Override
public void onPrepared(MediaPlayer mp)
{
dismissMyDialog();
videoView.start();
mediacontroller.setVisibility(View.VISIBLE);
isMediaCtrlShown = false;
seekbar.setProgress(0);
seekbar.setMax(videoView.getDuration());
ms = videoView.getDuration();
calculateTime(ms);
duration.setText("" + hh + ":" + mm + ":" + ss);
ms = videoView.getCurrentPosition();
calculateTime(ms);
currentTime.setText("" + hh + ":" + mm + ":" + ss);
playButton.setImageResource(android.R.drawable.ic_media_pause);
updatePosition();
isStarted = true;
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener()
{
// show updated information about the buffering progress
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
Log.d(this.getClass().getName(), "percent: " + percent);
percentageBuffer = percent;
secondarySeekBarProgressUpdater(percent);
// progress.setSecondaryProgress(percent);
if (i == 0)
{
i = i + 1;
previouPosition = mp.getCurrentPosition();
}
else if (i == 1)
{
if (mp.getCurrentPosition() == previouPosition)
{
if (!isPauseButtonClick)
{
showMyDialog();
if (percent == 100)
{
dismissMyDialog();
}
}
}
else
{
i = 0;
previouPosition = 0;
dismissMyDialog();
}
}
else if (isCustomSeekButtonClick)
{
isCustomSeekButtonClick = false;
if (mpCurrentPosition == mp.getCurrentPosition())
{
showMyDialog();
if (percent == 100)
{
dismissMyDialog();
}
}
else
{
dismissMyDialog();
}
}
}
});
mp.setOnSeekCompleteListener(new OnSeekCompleteListener()
{
public void onSeekComplete(MediaPlayer mp)
{
if (mp.isPlaying())
{
}
else
{
onStart();
onPause();
onStart();
}
}
});
}
private SeekBar.OnSeekBarChangeListener seekBarChanged = new SeekBar.OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
isMoveingSeekBar = false;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
isMoveingSeekBar = true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
{
Log.e("",""+progress+""+percentageBuffer);
if (fromUser)
{
isCustomSeekButtonClick = fromUser;
videoView.seekTo(progress);
mpCurrentPosition = progress;
Log.e("OnSeekBarChangeListener", "onProgressChanged");
}
if (isMoveingSeekBar)
{
videoView.seekTo(progress);
Log.i("OnSeekBarChangeListener", "onProgressChanged");
}
}
};
private View.OnClickListener onButtonClick = new View.OnClickListener() {
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.play:
{
if (videoView.isPlaying())
{
handler.removeCallbacks(updatePositionRunnable);
isPauseButtonClick = true;
videoView.pause();
playButton.setImageResource(android.R.drawable.ic_media_play);
}
else
{
if (isStarted)
{
videoView.start();
isPauseButtonClick = false;
playButton.setImageResource(android.R.drawable.ic_media_pause);
updatePosition();
}
else
{
startPlay(currentFile);
isPauseButtonClick = false;
videoView.start();
}
}
break;
}
case R.id.next:
{
int seekto = videoView.getCurrentPosition() + STEP_VALUE;
if (seekto > videoView.getDuration())
seekto = videoView.getDuration();
videoView.pause();
videoView.seekTo(seekto);
/*
* try { Thread.sleep(15000); } catch (InterruptedException e) {
* // TODO Auto-generated catch block e.printStackTrace(); }
*/
// player.pause();
videoView.start();
break;
}
case R.id.prev: {
int seekto = videoView.getCurrentPosition() - STEP_VALUE;
if (seekto < 0)
seekto = 0;
videoView.pause();
videoView.seekTo(seekto);
// player.pause();
videoView.start();
break;
}
}
}
};
private void updatePosition()
{
handler.removeCallbacks(updatePositionRunnable);
seekbar.setProgress(videoView.getCurrentPosition());
ms = videoView.getCurrentPosition();
calculateTime(ms);
currentTime.setText("" + hh + ":" + mm + ":" + ss);
handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY);
}
private void startPlay(String file)
{
Log.i("Selected: ", file);
// selelctedFile.setText(file);
seekbar.setProgress(0);
videoView.stopPlayback();
videoView.start();
seekbar.setMax(videoView.getDuration());
playButton.setImageResource(android.R.drawable.ic_media_pause);
updatePosition();
isStarted = true;
}
void setUpMyDialog()
{
if (progressDialog == null)
{
progressDialog = (ProgressDialog) onCreateDialog(DIALOG_KEY);
progressDialog = new ProgressDialog(player.this);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
}
void showMyDialog()
{
Log.e("showMyDialog***", "****" + isMyDialogShowing);
if (!isMyDialogShowing)
{
isMyDialogShowing = true;
Log.e("showMyDialog: true***", "****" + isMyDialogShowing);
if (progressDialog != null && !progressDialog.isShowing())
{
Log.e("showMyDialog: true***", "****progressDialog" + progressDialog.isShowing());
progressDialog.show();
}
else if(progressDialog == null)
{
setUpMyDialog();
progressDialog.show();
}
}
}
void dismissMyDialog()
{
Log.e("dismissMyDialog***", "****");
if (progressDialog != null && progressDialog.isShowing())
{
progressDialog.dismiss();
progressDialog = null;
isMyDialogShowing = false;
}
}
void killMyDialog()
{
isMyDialogShowing = false;
}
private void secondarySeekBarProgressUpdater(int percent){
seekbar.setSecondaryProgress(percent);
}
最佳答案
我的直觉告诉您,您可能会因为专注于播放器而忽略了一些东西,特别是因为它适用于较小的内容。您是否考虑过检查流式传输服务器?如果服务器不能胜任传输较大文件的任务,那么播放器就无能为力了。此外,您可以调整来自服务器的数据包大小,以帮助播放器一次使用较小的“咬合”(请原谅双关语)媒体来管理播放。尝试使用免费的 Apple Darwin 流媒体服务器。有一个适用于 Apache、Windows 和其他版本的版本,而且它的配置性很强。给自己准备一组不同大小的文件,并尝试确定在什么大小播放开始失败。该数字将为您提供有关问题所在的重要线索,无论是传输的服务器数据包大小、Android 环境中的可用内存还是其他地方。无论数字如何,请尝试从服务器设置较小的数据包大小。这应该可以让您的播放器减少工作量,并有望改善播放效果。
您可以从网上的许多地方获得 Darwin 服务器。 Here is one such link .维基百科也有一些有用的信息和此服务器的一些链接,find them here .祝你在这方面的研究顺利。
弗兰克。
关于android - 如何为大型内容创建流媒体视频播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7230382/
谁能详细说明以下问题? 蓝牙堆栈如何处理音频数据? 如何处理音频命令? 我们需要任何服务来处理音频数据吗? 提前致谢。 最佳答案 基本上,通过 BLE 的语音命令需要: 一些用于减少所需带宽的音频编解
我正在使用Player库以实现全屏视频播放。我相信它在幕后使用 AVFoundation。 我可以使用Float(self.player.maximumDuration)来实现视频的完整持续时间。但是
我正在制作一种宏记录器/播放器 我已经使用 java.awt.Robot() 等实用程序完成播放器部分,它模拟基本的人类鼠标/键盘输出命令,读取 XML 文件。 我卡在了必须记录该 XML 文件的部分
我目前有以下代码可以在页面上播放 youtube 视频。 //Load player api asynchronously. var tag = document.createElement('s
我需要提供音频内容(但不是音乐,更像是播客;人类语音),我正在考虑使用基于 Flash 的播放器让用户无需下载即可收听内容。 我需要一个免费的可嵌入 Flash 的 mp3 播放器。有什么建议? 因为
html5 player/api 更新了吗?事件 SC.Widget.Events.PLAY, SC.Widget.Events.PAUSE, SC.Widget.Events.FINISH, htm
我想在 Lubuntu VMware 中自动打开和关闭 vlc 播放器。我试过一个shell脚本代码,如: vlc rtmp://code sleep(5) exit 0 or vl
我有一个只支持纵向模式的应用程序,它有一个表格,每个单元格包含一个标题和一个带有 YouTube 视频的 web View 。 现在您将如何让 Youtube 播放器同时处于横向和纵向模式?
我正在尝试在我的应用程序中使用 YouTube 播放器 API,但我不知道如何确定视频是否为直播。如果有人知道如何获得视频的真实持续时间。 更新: 我想出了一种方法来确定内容是否是实时的,我使用我的后
我想创建一个能够播放 YouTube 视频的音频并将下载的内容保存在本地缓存中的应用程序,因此当用户决定恢复或再次播放视频时,它不必再次下载部分视频而只需下载剩余部分(用户可以决定如何处理缓存,以及如
我希望我的页面将 div 显示为模态,然后播放 YouTube 视频。我能够按预期播放视频(下面的代码),但是当我在过滤操作时切换到隐藏的 div 时,页面加载时隐藏的 div 不会将 data-sr
我正在尝试使用 AngularJS 和 WP API 构建 SPA。我使用部分在 ng-view 中加载我需要通过路由显示的所有内容。在此基础上,我添加了 Plangular,它是一个使用 Sound
我找到了一个不错的 HTML 5 音频播放器,它带有基于 plyr 的播放列表和艺术品。它在我的桌面浏览器上运行良好,但在我的移动设备 (iOS) 上,按播放后无法播放。有一个codepen来演示:
我正在尝试通过pyglet在Python 3中播放歌曲。我可以播放和停止播放一首歌曲,但是当我尝试播放下一首歌曲时会产生错误。 I followed these instructions.我将在tki
如何将嵌入的 Vimeo 视频重置为播放完毕后的加载状态? Vimeo API 提供了卸载方法 player.api("unload") 但它不适用于非 Flash 播放器。 最佳答案 使用Vimeo
我有一个用于音频录制和播放的网络应用程序。为此,我正在使用 html5 播放器。 现在我必须开发 Phonegap Android 应用程序。我已将插件(org.apache.cordova.medi
有人知道如何像 SuperFlix 一样将自己的字幕加载到 Netflix 播放器吗?关于 Netflix HTML5 播放器的信息很少,其中之一是我应该可以使用 操作播放器 netflix.cadm
如何将新的黑色 YouTube 播放器嵌入到我的网站(刚刚推出的网站)中? 我以前曾问过这个问题,但它已关闭,因为在投票否决和关闭之前没有人愿意真正阅读该问题。不,我没有问如何嵌入V2或V3播放器,我
几个小时以来,我一直在尝试添加一种打开我的 mp3 文件的方法并在队列中一一打开它们。但我不知道该怎么做。当涉及到单个文件时,我打开并播放不是问题。所以我正在考虑 Media(JavaFX) 类中的线
我知道这个函数 (setFullscreen) 只适用于 HTML5,但它对我不起作用。这是我使用的方式: setFullscreen: true 我希望 JW Player 在页面加载后立即以全屏模
我是一名优秀的程序员,十分优秀!